How can I add timestamp to logs using Node.js libr

2019-03-14 05:30发布

I want to add timestamp to logs. What is the best way to achieve this?

7条回答
Melony?
2楼-- · 2019-03-14 06:07

Sometimes default timestamp format can be not convenient for you. You can override it with your implementation.

Instead of

var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
  new (winston.transports.Console)({'timestamp':true})
]
});

you can write

var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
  new (winston.transports.Console)({
     'timestamp': function() {
        return <write your custom formatted date here>;
     }
  })
]
});

See https://github.com/winstonjs/winston#custom-log-format for the details

查看更多
登录 后发表回答