如何解决带有时间戳的Winson不添加时间戳
我为节点应用程序创建了一个简单的Winston记录器,但是带有时间戳的配置不起作用,这意味着什么?这意味着所有日志都不带有时间戳。
示例:
const { createLogger,format,transports } = require('winston');
const logger = createLogger({
transports: [
new transports.File({
maxsize: 5120000,maxFiles: 20,filename: `logs/logs.log`,colorize: true,json: true,timestamp: true
}),new transports.Console({
level: "debug",timestamp: true,format: format.combine(
format.colorize(),format.simple(),format.timestamp()
)
})
]
});
module.exports = { logger };
解决方法
simple()
格式不输出时间戳。因此您应该使用printf
定义自定义格式:
const { createLogger,format,transports } = require('winston');
const myFormat = format.printf(({ level,message,label,timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});
const logger = createLogger({
transports: [
new transports.File({
maxsize: 5120000,maxFiles: 20,filename: `logs/logs.log`,timestamp: true,json: true,}),new transports.Console({
level: "debug",format: format.combine(
format.timestamp(),format.colorize(),myFormat,)
})
]
});
module.exports = { logger };
我也将timestamp()
格式移到了myFormat之前,因为它应该在到达printf之前填充消息的时间戳。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。