简体   繁体   English

Winston:如何在最前面使用时间戳记日志

[英]Winston : How to get logging with timestamp at the front

How do configure Winston to output log like 如何配置Winston以输出类似日志

2019-06-30 17:39:07 : error : Cheese has rotten 2019-06-30 17:39:07:错误:奶酪烂了

I tried this but it is outputting timestamp at the end 我尝试了这个,但最后输出时间戳

const logger = winston.createLogger({
    format: winston.format.combine(
        winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
        winston.format.simple()
    )
});

error: Cheese has rotten! 错误:奶酪烂了! {"timestamp":"2019-06-30 23:32:02"} {“ timestamp”:“ 2019-06-30 23:32:02”}

try the below code. 试试下面的代码。 I hope this will solve the issue. 我希望这可以解决问题。 You can customize in your own required way in myFormat custom function 您可以在myFormat自定义函数中以自己所需的方式自定义

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} : [${label}] : ${level}: ${message}`;
});

const logger = createLogger({
  format: combine(
    label({ label: 'right meow!' }),
    timestamp(),
    myFormat
  ),
  transports: [new transports.Console()]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM