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

时间:2019-03-21 12:36:18

标签: node.js json formatting winston

我正在使用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',
    })]
});

我希望我的日志dato最终存储在具有以下结构的JSON文件中:

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

代替:

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

1 个答案:

答案 0 :(得分:0)

记录器将条目实时写入文件,它将如何为每个语句关闭文件?您随时可以轻松地将标准文件格式化为适当的json对象:

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

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

您可以使用bash轻松执行类似的操作