node.js 如何更改 winston 日志格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18712680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:35:22  来源:igfitidea点击:

How can I change the winston log format?

javascriptnode.jsformattingwinston

提问by sachin

In my node application I'm using winston module to store my application logs. We can store the the logs in two format one is json and the other one is string. While saving the log as string in winston I'm getting below log format.

在我的节点应用程序中,我使用 winston 模块来存储我的应用程序日志。我们可以以两种格式存储日志,一种是json,另一种是字符串。在 winston 中将日志保存为字符串时,我得到了日志格式。

  2013-09-10T06:51:34.199Z - error: error message!!!
       (timestamp)     -    (level) : (log message)

Now I want change the above log format to the following:

现在我想将上述日志格式更改为以下内容:

    2013-09-10T06:51:34.199Z/error/error message!!!
       (timestamp)    /     (level) / (log message)

How can this be achieved?

如何做到这一点?

My Code:

我的代码

  var winston = require('winston');
  winston.loggers.add('category1', {
   file: {
      filename: '/path/to/some/file',json:false
     }
  });              
  var category1 = winston.loggers.get('category1');
  category1.log('error','error message!!!');

回答by Default

I was wondering the same thing and found an okay solution (though not ideal IMO, so perhaps someone else can weigh in on this).

我想知道同样的事情并找到了一个不错的解决方案(虽然不是理想的 IMO,所以也许其他人可以对此进行权衡)。

You can completely customize the logger output by providing your transport object a formatter function. This might be better for File transports than Console, since you would have to manually colorize your font if that's what you wanted.

您可以通过为传输对象提供格式化程序功能来完全自定义记录器输出。这对于文件传输来说可能比控制台更好,因为如果这是你想要的,你必须手动为字体着色。

Here is a relatively simple formatter function that you can use (and adjust for your needs):

这是您可以使用的相对简单的格式化程序功能(并根据您的需要进行调整):

// Define options for Date#toLocaleTimeString call we will use.
var twoDigit = '2-digit';
var options = {
  day: twoDigit,
  month: twoDigit,
  year: twoDigit,
  hour: twoDigit,
  minute: twoDigit,
  second: twoDigit
};

function formatter(args) {
  var dateTimeComponents = new Date().toLocaleTimeString('en-us', options).split(',');
  var logMessage = dateTimeComponents[0] + dateTimeComponents[1] + ' - ' + args.level + ': ' + args.message;
  return logMessage;
}

And to use this formatter in your transport, simply adjust your code to pass the function in:

要在您的传输中使用此格式化程序,只需调整您的代码以将函数传入:

winston.loggers.add('category1', {
  file: {
    filename: '/path/to/some/file',
    json: false,
    formatter: formatter
  }
});

It's worth mentioning that the property args.metawill be set to any object argument that is passed into a log method call. So you would have to come up with a strategy for handling those objects passed in (or simply print the entire object as JSON). For example:

值得一提的是,该属性args.meta将设置为传递给日志方法调用的任何对象参数。因此,您必须想出一种策略来处理传入的那些对象(或简单地将整个对象打印为 JSON)。例如:

var error = {
  name: 'MongoError',
  code: 11000,
  err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error...'
}
logger.info('Some error ocurred: ', error);

Would result in args.metabeing set to the error variable.

将导致args.meta被设置为错误变量。

As you can see, there is a fair amount to deal with when handling log messages this way. I wouldn't be surprised if there was a better way of doing these things, but hopefully this helps you (or someone else) out.

如您所见,以这种方式处理日志消息时需要处理相当多的问题。如果有更好的方法来做这些事情,我不会感到惊讶,但希望这可以帮助您(或其他人)。