ts中使用winston打印日志
import * as winston from 'winston'; // 引入config模块,log相关配置放在配置文件下 import config from 'config'; class Logger { private readonly consoleLevel: string; private readonly fileLevel: string; constructor (){ const logConfig = JSON.parse(JSON.stringify(config.get('log-config'))); this.consoleLevel = logConfig.consoleLevel; this.fileLevel = logConfig.fileLevel; this.addLogger('controller'); this.addLogger('service'); } public getLogger (category: string): winston.Logger {return winston.loggers.get(category); } // 添加多个logger private addLogger (category: string){ winston.loggers.add(category, { format: winston.format.combine( winston.format.label({ label: category}), winston.format.timestamp(), winston.format.printf((info) => { return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`; }) ), transports: [ // level设置打印的最低级别 new winston.transports.Console({ level: this.consoleLevel }), new winston.transports.File({ level: this.fileLevel, filename: __dirname + `/logs/${category}.log`, maxsize: 5242880, maxFiles: 20 }) ] }); } } const LogFactory = new Logger(); export {LogFactory};