【问题标题】:Create your own Winston logger files创建您自己的 Winston 记录器文件
【发布时间】:2016-08-18 11:51:19
【问题描述】:

这是我设置的一个基本的 winston 记录器:

var winston = require('winston')

var systemLogger = new (winston.Logger)({
  transports: [
    new (winston.transports.File)({
      name: 'system-file',
      // log which stores all the errors
      filename: './test.log',
      json: true,
      timestamp: true
    })
  ]
})

systemLogger.log('info', 'hello')
systemLogger.log('error', '-----hello2')

这会创建一个这样的日志文件:

{"level":"info","message":"hello","timestamp":"2016-08-18T11:48:22.081Z"}
{"level":"error","message":"-----hello2","timestamp":"2016-08-18T11:48:22.083Z"}

但我想让它在文件中看起来像这样:

INFO: 2016-08-18T11:48:22.081Z hello 
ERROR: 2016-08-18T11:48:22.083Z *** -----hello2 *** 

这可能吗?

我读过这篇文章 - https://github.com/winstonjs/winston#custom-log-format 但我实际上无法找到如何做到这一点 - 如果可能的话。

我已尝试添加格式化程序:

  formatter: function(options) {
    // Return string will be passed to logger.
    if (options.level === 'error') {
      return options.timestamp() +' ******** '+ options.level.toUpperCase() +' ******** '+ (undefined !== options.message ? options.message : '') 
    }
    else if (options.level === 'info') {
      return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') 
    }
  }

但这似乎只适用于控制台打印,不适用于文件。由于文件的保存方式与没有格式化程序的方式相同。

【问题讨论】:

  • 我的答案有文件和控制台的例子。

标签: javascript node.js logging winston


【解决方案1】:

在您的记录器设置代码中,您必须放置格式化程序以及从格式化程序返回的任何字符串,它将显示在您的日志文件中。

formatter: function(options) {
        // Return string will be passed to logger.
        return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') +
          (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
      }

因此,上述格式化程序将首先打印时间戳,然后是日志级别,然后是您记录的实际消息。

希望这会有所帮助....

【讨论】:

  • 我试过了,但文件没有正确保存。请检查更新的问题
【解决方案2】:

你需要创建一个格式化函数:

$ cat logger.js 
var winston = require('winston');

module.exports = (function(){

    function formatter (args){
            var timestamp = new Date().toISOString(),
                level =  args.level.toUpperCase(),
                message = args.message;

        return level+": "+timestamp+" "+message ;
    }

    return new (winston.Logger)({
        transports: [
            new (winston.transports.Console)({
                formatter: formatter,
            }),

            new (winston.transports.File)({
                formatter: formatter,
                filename : "./logs/custom-logger.log",
                maxsize : 1024 * 1024 * 500,
                maxFiles : 10,
                json : false,
                tailable : true,
            })
        ]
    });
}()); 

【讨论】:

    【解决方案3】:

    你需要做的就是添加这个:json : false

    所以它看起来像这样:

    var winston = require('winston')
    
    var systemLogger = new (winston.Logger)({
      transports: [
        new (winston.transports.File)({
          name: 'system-file',
          json : false,
          // log which stores all the errors
          filename: './test.log',
          json: true,
          timestamp: true
        })
      ]
    })
    

    你已经很接近了,做得很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 2012-04-14
      • 1970-01-01
      相关资源
      最近更新 更多