【问题标题】:logging with Express Node.js using winston使用 winston 使用 Express Node.js 进行日志记录
【发布时间】:2014-11-19 03:56:08
【问题描述】:

我在我的 express/nodejs Web 应用程序中配置了这样的日志记录。日志消息中的时间戳以 GMT 为单位。有没有办法获取当地时间?

var winston = require('winston');
winston.emitErrs = true;

var logger = new winston.Logger({
  transports: [
    new winston.transports.File({
        level: 'debug',
        filename: './logs/all-logs.log',
        handleExceptions: true,
        json: false,
        maxsize: 5242880, //5MB
        maxFiles: 5,
        colorize: false,
        timestamp:true
    }),
    new winston.transports.Console({
        timestamp :true,
        level: 'debug',
        handleExceptions: true,
        json: false,
        colorize: true
    })
],
exitOnError: false
});

module.exports = logger;
module.exports.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};

这是我得到的一个示例日志输出

2014-11-18T18:30:33.570Z - debug: Authenticated

【问题讨论】:

    标签: node.js express winston


    【解决方案1】:

    你试过了吗:

    function myTimestamp() {
      return new Date().toString();
    };
    
    var logger = new winston.Logger({
      transports: [
        new winston.transports.File({
            level: 'debug',
            filename: './logs/all-logs.log',
            handleExceptions: true,
            json: false,
            maxsize: 5242880, //5MB
            maxFiles: 5,
            colorize: false,
            timestamp: myTimestamp
        }),
        new winston.transports.Console({
            timestamp :true,
            level: 'debug',
            handleExceptions: true,
            json: false,
            colorize: true
        })
    ],
    exitOnError: false
    });
    

    【讨论】:

    • 升级winston 可能吗?正如您在winston.transports.File here使用的common.js#L116 中看到的那样,此功能在最新版本中明显可用。
    • 同时检查 Node.js REPL,new Date().toString() 为您提供所需的输出。
    猜你喜欢
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    相关资源
    最近更新 更多