【问题标题】:Adding different formatters to Winston transports向 Winston 传输添加不同的格式化程序
【发布时间】:2023-03-13 02:47:01
【问题描述】:

我需要为每种传输使用不同类型的格式化程序。

例子:

logger = new (winston.Logger)({
    transports: [
        new (winston.transports.LogstashUDP)({
            // some config here. Do noting on formatting
        }),
        new (winston.transports.Mail)({
            // do formatting one way
        }),
        new (winston.transports.File)({
            // write to file as json (maybe format it here)
        }),
        new (winston.transports.Console)({
            // do another formatting
        })
    ]
});

从winston transports docs 可以看出,只有控制台支持自定义格式化程序。

我正在使用winston-mailer 邮件模块和winston-logstash-upd

Winston 有什么方法可以解决这个问题吗?或者也许如何围绕这些模块之一创建包装器以支持格式化?

【问题讨论】:

标签: javascript node.js logging formatting winston


【解决方案1】:

这是解决方案posted to GitHub by dandv

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'error.log', level: 'error',
      format: winston.format.simple(),
    }),
    new winston.transports.File({
      filename: 'combined.log', level: 'debug',
      format: winston.format.printf(info => `${new Date().toISOString(), ${info.message}`),
    }),
  ],
});

logger.error('prefixed by the timestamp only in `combined.log`');

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2020-01-03
    • 2017-03-24
    相关资源
    最近更新 更多