我使用 Winston 作为 NodeJS 项目的记录器。我尝试向日志消息添加时间戳并配置 Winston 以非 Json 方式写入控制台日志消息,但未成功。我的配置如下
const appRoot = require('app-root-path');
const winston = require('winston');
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
timestamp: true,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 15,
colorize: false,
},
console: {
level: 'debug',
timestamp: true,
handleExceptions: true,
json: false,
colorize: true,
},
};
const logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false,
});
module.exports = logger;
这是我在其他文件中导入 Winston 的方式(winston 配置文件位于我的项目的根目录):
const winston = require('../winston');
知道它为什么起作用吗?
最佳答案
我在使用 Winston 3 时遇到了此错误。请查看 this section 中的文档。指定记录器的格式。这将解决问题。
关于node.js - winston - 未添加时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51916520/