Winston: 文件的自定义日志格式似乎不起作用

创建于 2015-04-02  ·  4评论  ·  资料来源: winstonjs/winston

你好,

我使用您的格式化程序功能为我的日志消息提供自定义格式。 它适用于 transport.Console 但 transport.File 接缝忽略自定义格式化程序。

这是我的自定义文件格式化程序:

function customFileFormatter (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) : '' );
}

您的示例中给出的记录器必不可少。 我这样应用它:

winston.add(winston.transports.File, { 
    name: 'info-file',
    filename: logPath + '/info.log',
    level: 'info',
    maxsize: 15000000,
    formatter: customFileFormatter
});

但日志文件中的输出保持不变:
{"level":"info","message":"INITIALISEING","timestamp":"2015-04-02T06:29:57.982Z"}
它应该是:
{"level":"[INFO]","message":"INITIALISEING","timestamp":"2015-04-02T06:29:57.982Z"}

我不知道出了什么问题,因为同样适用于 transport.Console。

请帮忙

你好

托纳卡特

最有用的评论

好吧,那算了。
忘记设置了
json: false

但如果您可以只更改 json 日志中的某些字段,那就太酷了 :)

所有4条评论

好吧,那算了。
忘记设置了
json: false

但如果您可以只更改 json 日志中的某些字段,那就太酷了 :)

我花了一点时间弄清楚在哪里设置“json:false”。 将其设置在文件传输上。 这是我的例子:

const logFormatter = function(options) {
    // Return string will be passed to logger.

    return options.timestamp() +` `+ options.level.toUpperCase() +
        ` `+ (options.message ? options.message : ``) +
        (options.meta && Object.keys(options.meta).length ?
            `\n\t`+ JSON.stringify(options.meta) : `` );
};

const timestamp = function() {
    const d = new Date();
    return d.getHours() + `:` + d.getMinutes() + `:` +
        d.getSeconds() + `m` + d.getMilliseconds();
};

const logger = new (winston.Logger)({
    transports: [
        new (winston.transports.File)({
            timestamp: timestamp,
            formatter: logFormatter,
            level: `silly`,
            name: `fileAll`,
            filename: `fileAll.log`,
            json: false,                                 // set json:false on the file transport(s)
        })
    ]
});

格式化程序对我来说很好用,但时间戳总是打印一个文字字符串

功能 () {
const d = 新日期();
返回 d.getHours() + : + d.getMinutes() + : +
d.getSeconds() + m + d.getMilliseconds();
}|DEBUG|调试消息
功能 () {
const d = 新日期();
返回 d.getHours() + : + d.getMinutes() + : +
d.getSeconds() + m + d.getMilliseconds();
}|紧急情况|调试消息

好吧,那算了。
忘记设置了
json: false

但如果您可以只更改 json 日志中的某些字段,那就太酷了 :)

你节省了我的时间! 我想知道这是在哪里记录的..

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

mohanen picture mohanen  ·  4评论

alditis picture alditis  ·  3评论

Buzut picture Buzut  ·  3评论

Nepoxx picture Nepoxx  ·  4评论

anks333 picture anks333  ·  3评论