简体   繁体   English

温斯顿输出json而不是格式化的字符串

[英]Winston outputs json instead of formatted string

I have set up a simple winston logger in my app like this: 我在我的应用程序中设置了一个简单的Winston记录器,如下所示:

function logger(success, msg) {
    let now = new Date().toUTCString()
    let logger = new (winston.Logger)({
        transports: [
            new (winston.transports.File)({ 
                filename: 'log.log',
                timestamp: function() {
                    return new Date().toUTCString();
                },
                formatter: function(options) {
                    return `>>>>>>>>>> ${options.timestamp()} - ${options.level.toUpperCase} - ${options.message}`;
                }
            })
        ]
    });

    if (success) {
        logger.log('info', msg)
    } else {
        logger.log('error', msg)
    }
}

But instead of logging the formatted string, it outputs the following: 但是,它不会输出格式化的字符串,而是输出以下内容:

{"level":"error","message":"Nothing to upload","timestamp":"Mon, 23 Apr 2018 13:53:01 GMT"}

Ideas? 想法? Am I missing something? 我想念什么吗? (Of course I am) (我当然是)

As you can find out in winston documentation for File you can set property json to false if you don't want information in file to be JSON objects. 正如您可以在Winston文档中找到的File一样 ,如果您不希望文件中的信息成为JSON对象,则可以将属性json设置为false。 By default this property is true . 默认情况下,此属性为true

json: If true, messages will be logged as JSON (default true). json:如果为true,则消息将记录为JSON(默认为true)。

Could you try to change your code like this: 您可以尝试像这样更改代码:

function logger(success, msg) {
    let now = new Date().toUTCString()
    let logger = new (winston.Logger)({
        transports: [
            new (winston.transports.File)({ 
                filename: 'log.log',
                timestamp: function() {
                    return new Date().toUTCString();
                },
                json: false,
                formatter: function(options) {
                    return `>>>>>>>>>> ${options.timestamp()} - ${options.level.toUpperCase} - ${options.message}`;
                }
            })
        ]
    });

    if (success) {
        logger.log('info', msg)
    } else {
        logger.log('error', msg)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM