Flint
Flint

Reputation: 1711

Node.js Winston logging : arrays output

I guess something is missing in my configuration of winston 2.2.0 since I'm having trouble to make it correctly output arrays. By correct, I mean exactly as the console.log would output. My configuration holds only a default transports.console.

If only one argument is given to the logger, then the arrays are displayed with indexes first, while it's correctly printed if two are given.

Examples :

logger.debug ([ 1,2 ])
> 0=1, 1=2

logger.debug (  [ 1,2], '')
> [ 1, 2 ] ''

logger.debug ({x:1,y:2,z:{i:3}})
> x=1, y=2, i=3

turning on prettyPrint:true makes the json objects to display correctly, but add extra colors, carriage return and still displays arrays with indexes.

Upvotes: 1

Views: 1639

Answers (2)

robertklep
robertklep

Reputation: 203359

Not ideal, but perhaps still useful:

var logger  = new winston.Logger({
  transports : [ new winston.transports.Console({}) ],
  rewriters  : [
    function (level, msg, meta) {
      return meta ? JSON.stringify(meta) : meta;
    }
  ]
});

Upvotes: 1

Flint
Flint

Reputation: 1711

This workaround looks nasty and exclude calls to the logger with multiple arguments, but it works :

logger.dbg = function() {
    if(arguments.length>1) this.warn("more than one arg given to dbg()")
    if(Array.isArray(arguments[0]))
        logger.debug('%j',arguments[0])
    else
        logger.debug(arguments[0],'')
}

Results :

logger.dbg([ 1, 2, {x:1} ])
logger.dbg({x:1,z:2, s:[1,]})
logger.dbg("AAA")
logger.dbg(undefined)
logger.dbg(null)
logger.dbg([])

output :

debug: [log.js:131] [1,2,{"x":1}]
debug: [log.js:132] { x: 1, z: 2, s: [ 1 ] } 
debug: [log.js:133] AAA 
debug: [log.js:134] undefined 
debug: [log.js:135] null 
debug: [log.js:136] []

Upvotes: 0

Related Questions