How can I add timestamp to logs using Node.js library Winston?

后端 未结 9 633
清歌不尽
清歌不尽 2021-01-31 12:55

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

9条回答
  •  长发绾君心
    2021-01-31 13:43

    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 ;
         }
      })
    ]
    });
    

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

提交回复
热议问题