简体   繁体   English

Winston - 将日志数据附加到JSON数组中

[英]Winston - append log data into JSON array

I'm using Winston for logging like this: 我正在使用Winston进行日志记录:

const logger = createLogger({
format: format.json(),
transports: [
    new transports.File({
        level: 'error',
        format: format.combine(filterOnly('error'),
            format.timestamp({
                format: 'YYYY-MM-DD HH:mm:ss'
            }),
            format.json()),
        filename: './audit_log/error.json',
    })]
});

I would like my log dato to end up in a JSON file with a structure like this: 我希望我的日志dato最终在一个JSON文件中,结构如下:

{
"log": [
    {
        "message": "",
        "level": "",
        "timestamp": ""
    },
    {
        "message": "",
        "level": "",
        "timestamp": ""
    }

Instead of: 代替:

{"message":"Bundle uploaded file","level":"error","timestamp":"2019-02- 
28T07:48:59.821Z"}

The logger writes entries to the file as it goes, how would it close the file for every statement? 记录器将条目写入文件,如何为每个语句关闭文件? You can easily format a standard file into a proper json object if you want at any point: 如果您想在任何时候,您可以轻松地将标准文件格式化为适当的json对象:

const readline = require('readline').createInterface({
  input: require('fs').createReadStream('logfile')
})

const log = []
readline.on('line', line => log.push(JSON.parse(line)))

You can easily do something similar using bash 你可以使用bash轻松做类似的事情

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

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