const tsFormat = () => (new Date()).toLocaleTimeString();
const logger = new (winston.Logger)({
transports: [
// colorize the output to the console
new (winston.transports.Console)({
timestamp: tsFormat,
colorize: true,
level: 'info'
}),
new (require('winston-daily-rotate-file'))({
filename: `${app_config.logfolder}/-${app_config.application[APP_NAME].logfile}`,
timestamp: tsFormat,
datePattern: 'dd-MM-yyyy',
prepend: true,
handleExceptions: true,
level:app_config.application[APP_NAME].loglevel
})
]
});我已经用我想要的级别配置了记录器。但在此之后,我必须使用logger.info或其他日志级别。那么,有没有什么方法可以在不指定日志级别的情况下执行类似logger.log的操作,因为我已经配置了日志级别。
在我已经配置了日志级别的情况下,每次使用日志级别都没有任何意义。
发布于 2018-01-25 14:19:45
基本上,将您的自定义日志记录方法封装在一个单独的模块中并重用它
// in MyLogger.js
const tsFormat = () => (new Date()).toLocaleTimeString();
const logger = new (winston.Logger)({
transports: [
// colorize the output to the console
new (winston.transports.Console)({
timestamp: tsFormat,
colorize: true,
level: 'info'
}),
new (require('winston-daily-rotate-file'))({
filename: `${app_config.logfolder}/-${app_config.application[APP_NAME].logfile}`,
timestamp: tsFormat,
datePattern: 'dd-MM-yyyy',
prepend: true,
handleExceptions: true,
level:app_config.application[APP_NAME].loglevel
})
]
});
exports.log = function(message) {
logger.log('info', message);
}
// in consumer.js
var log = require('MyLogger');
log('My log message');https://stackoverflow.com/questions/48436729
复制相似问题