| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- const winston = require('winston');
- const path = require('path');
- // 定义日志格式
- const logFormat = winston.format.combine(
- winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
- winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
- );
- // 创建日志记录器
- const logger = winston.createLogger({
- level: 'debug', // 日志级别
- format: logFormat,
- transports: [
- // 输出到控制台
- new winston.transports.Console({
- format: winston.format.combine(
- winston.format.colorize(), // 添加颜色
- logFormat
- )
- }),
- // 输出到文件
- new winston.transports.File({
- filename: path.join(__dirname, '../logs', 'app.log'), // 日志文件路径
- maxsize: 10485760, // 10MB
- maxFiles: 5,
- tailable: true
- }),
- // 添加业务日志分类
- new winston.transports.File({
- filename: path.join(__dirname, '../logs', 'business.log'), // 业务日志文件路径
- format: winston.format.combine(
- winston.format.timestamp(),
- winston.format.json()
- ),
- level: 'info' // 业务日志级别
- })
- ]
- });
- module.exports = logger;
|