Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use level value instead of name in winston log messages?

I'm trying to make an app that writes logs with winston.js in json format work with hekad. After I set winston.default.transports.console.json to true, invocations like w.info('test', {val: 123}) produce:

{"val":123,"level":"info","message":"test"}

The problem is that according to heka docs, severity must be numeric, e.g. (for syslog levels) 6 instead of info, 5 instead of notice, etc. Is it possible to instruct winston to write level value instead of name to logs?

like image 561
Fluffy Avatar asked Nov 26 '25 04:11

Fluffy


1 Answers

The simplest way is to change log format. This allow you to create almost identical output, but for example you can map your levels names to numbers.

You have to disable json in transport. Below in example I disabled it, but I manually formatted it as json.

example:

this.logger.add(winston.transports.File, {
  name:'log.info',
  level: 'info',
  filename: path.join(logPath, 'info.log'),
  json: false, // this is important
  maxsize: 5242880,
  maxFiles: 5,
  colorize: false,
  formatter: function(options) { //formatter function
    var map = { //add rest of levels with values
      info: 6,
      notice: 5
    }
    //to check all options
    console.log(options); //then you know what props you can use to create desired output
    //because we turn off json we have to manually create it
    return JSON.stringify({ 
      level: map[options.level],//I map level
      message: options.message
    });
  }
});

this will return in log file:

{"level":6,"message":"some message"}

You can read more about it in winston documentation (at the bottom of page in section 'Custom Log Format') link

like image 178
Krzysztof Sztompka Avatar answered Dec 01 '25 18:12

Krzysztof Sztompka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!