pino: Logging an Error along with both an object and a string

I’d like to be able to log an error along with a string and an object without pino merging the object and the string together. Is this possible without using the child feature?

My first attempt at doing this was:

pino.info(new Error('foo'), {bar:42}, 'baz')

The result unfortunately is that msg becomes {\"bar\":42} \"baz\":

{"pid": 83039,
 "hostname": "...",
 "level": 30,
 "time": 1483531681798,
 "msg": "{\"bar\":42} \"baz\"",
 "type": "Error",
 "stack": "...",
 "v": 1}

It looks a little nicer if I reverse the string and the object:

pino.info(new Error('foo'), 'baz', {bar:42})

Then msg becomes baz {\"bar\":42}:

{"pid": 83039,
 "hostname": "...",
 "level": 30,
 "time": 1483531743505,
 "msg": "baz {\"bar\":42}",
 "type": "Error",
 "stack": "...",
 "v": 1}

But what I really want is the the same output as if I did:

var child = pino.child({bar:42})
child.info(new Error('foo'), 'baz')

This produces the expected result with bar as a separate property:

{"pid": 83039,
 "hostname": "...",
 "level": 30,
 "time": 1483531573985,
 "msg": "baz",
 "type": "Error",
 "stack": "...",
 "bar": 42,
 "v": 1}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (11 by maintainers)

Most upvoted comments

logger.info({'event': payload}, 'Message')
``` 

Works as expected.