How do I change my node winston JSON output to be single line

前端 未结 5 973
刺人心
刺人心 2021-02-04 01:54

When I create a nodejs winston console logger and set json:true, it always output JSON logs in multiline format. If I pipe these to a file and try to grep that file

相关标签:
5条回答
  • 2021-02-04 02:24
    winston.format.printf(
        info => `${info.timestamp} ${info.level}: ${JSON.stringify(info.message, null, 2)}`))
    

    will pretty print the json object

    0 讨论(0)
  • 2021-02-04 02:27

    "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()]
      });
    }
    

    Output:

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

    0 讨论(0)
  • 2021-02-04 02:38

    On "winston": "^3.2.1"

    This is works great for me

    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
    });
    
    0 讨论(0)
  • 2021-02-04 02:45

    The winston transports provide a way to override the stringify method, so by modifying the config above I got single line JSON output.

    New config:

    winston = require('winston')
    logger = new (winston.Logger)({
      transports: [
        new winston.transports.Console({
         json: true,
         stringify: (obj) => JSON.stringify(obj)
        })
      ]
    })
    
    0 讨论(0)
  • 2021-02-04 02:46

    winston 3.x (current version)

    Default formatter

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

    Example

    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"}
    

    Custom formatter

    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()
      ]
    });
    

    Example:

    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 (legacy version)

    It seems that the accepted answer is outdated. Here is how to do this for current winston version (2.3.1):

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

    Note the parenthesis around winston.transports.Console.

    0 讨论(0)
提交回复
热议问题