如何将节点winston JSON输出更改为单行

时间:2015-08-21 02:07:18

标签: node.js winston

当我创建nodejs winston控制台记录器并设置json:true时,它总是以多行格式输出JSON日志。如果我将这些文件传输到文件并尝试grep该文件,我的grep命中只包含部分日志行。我希望winston以JSON格式输出我的日志行,但不要打印出JSON

这是我的配置(coffeescript,道歉):

winston = require 'winston'
logger = new (winston.Logger)(
  transports: [
    new winston.transports.Console({
     json: true
    })
  ]
)

一些示例输出:

{
  "name": "User4",
  "level": "info",
  "message": "multi line whyyyyy"
}

5 个答案:

答案 0 :(得分:23)

winston 3.x(当前版本)

默认格式化程序

const winston = require('winston');
const logger = winston.createLogger({
  format: winston.format.json(),
  transports: [
    new winston.transports.Console()
  ]
});

实施例

const test = { t: 'test', array: [1, 2, 3] };
logger.info('your message', test);
// logger output:
// {"t":"test","array":[1,2,3],"level":"info","message":"your message"}

自定义格式化程序

const winston = require('winston');

const { splat, combine, timestamp, printf } = winston.format;

// meta param is ensured by splat()
const myFormat = printf(({ timestamp, level, message, meta }) => {
  return `${timestamp};${level};${message};${meta? JSON.stringify(meta) : ''}`;
});

const logger = winston.createLogger({
  format: combine(
    timestamp(),
    splat(),
    myFormat
  ),
  transports: [
    new winston.transports.Console()
  ]
});

示例:

const test = { t: 'test', array: [1, 2, 3] };
// NOTE: wrapping object name in `{...}` ensures that JSON.stringify will never 
// return an empty string e.g. if `test = 0` you won't get any info if 
// you pass `test` instead of `{ test }` to the logger.info(...)
logger.info('your message', { test });
// logger output:
// 2018-09-18T20:21:10.899Z;info;your message;{"test": {"t":"test","array":[1,2,3]}}

winston 2.x(旧版)

似乎接受的答案已经过时了。以下是如何为当前的winston版本(2.3.1)执行此操作:

var winston = require('winston');
var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({
     json: true,
     stringify: (obj) => JSON.stringify(obj),
    })
  ]
})

请注意winston.transports.Console周围的括号。

答案 1 :(得分:3)

winston传输提供了一种覆盖stringify方法的方法,因此通过修改上面的配置,我获得了单行JSON输出。

新配置:

winston = require('winston')
logger = new (winston.Logger)({
  transports: [
    new winston.transports.Console({
     json: true,
     stringify: (obj) => JSON.stringify(obj)
    })
  ]
})

答案 2 :(得分:2)

"winston": "^3.0.0"

function createAppLogger() {
  const { combine, timestamp, printf, colorize } = format;

  return createLogger({
    level: 'info',
    format: combine(
      colorize(),
      timestamp(),
      printf(info => {
        return `${info.timestamp} [${info.level}] : ${JSON.stringify(info.message)}`;
      })
    ),
    transports: [new transports.Console()]
  });
}

输出:

2018-08-11T13:13:37.554Z [info] : {"data":{"hello":"Hello, World"}}

答案 3 :(得分:0)

winston.format.printf(
    info => `${info.timestamp} ${info.level}: ${JSON.stringify(info.message, null, 2)}`))

会漂亮地打印json对象

答案 4 :(得分:0)

"winston": "^3.2.1"

这对我来说很好

const {createLogger, format} = require('winston');

// instantiate a new Winston Logger with the settings defined above
var logger = createLogger({

    format: format.combine(
      format.timestamp(),
      // format.timestamp({format:'MM/DD/YYYY hh:mm:ss.SSS'}),
      format.json(),
      format.printf(info => {
        return `${info.timestamp} [${info.level}] : ${info.message}`;
      })
  ),
  transports: [
    new winston.transports.File(options.file),
    new winston.transports.Console(options.console)
  ],
  exitOnError: false, // do not exit on handled exceptions
});