pino-pretty icon indicating copy to clipboard operation
pino-pretty copied to clipboard

How to render `\n`s in arbitrary data properties

Open egargan opened this issue 1 year ago • 9 comments

I'd like to be able to display just the stack property when logging errors, instead of giving the whole error to Pino when logging them.

In an app of mine, I like to log out only an error's stack property, instead of just giving the entire error to Pino to display. When I do this, the stack is printed in a signle line, without rendering its \ns.

image

If I hand the whole error to Pino, i.e. logger.error(err), the stack is printed "prettily", broken across multiple lines.

image

How do I tell Pino to render my stack property's newlines? I've looked through the docs for pino-pretty's config options, but I can't find anything there that solves my problem.

egargan avatar May 09 '23 17:05 egargan

logger.error(err) gets serialized by the error serializer attached to the Pino instance. If you want a different serialization, you need to provide a serializer that does what you want.

jsumners avatar May 11 '23 12:05 jsumners

@jsumners I don't think that's the case.

This is currently not possible in pino-pretty. I'm not even sure how it could be implemented reliably, but it would be good PR there.

mcollina avatar May 13 '23 12:05 mcollina

The implementation here recognizes serialized errors from Pino.

jsumners avatar May 13 '23 13:05 jsumners

Yes, exactly. This is not a serialized error.

mcollina avatar May 13 '23 16:05 mcollina

Yep you've got it @mcollina, I should have been clearer.

For that first second screencap I'm doing logger.error({ stack: error.stack }). I'd like this output to be nice and newliend and indented, like it is when I just do logger.error({ stack: error.stack }).

@mcollina if it's implemented for the error serializer, surely it's possible? Would it just be a case of extracting the code in this error serializer that renders newlines, and making it available as a config option?

egargan avatar May 16 '23 21:05 egargan

The code is very specific for errors: https://github.com/pinojs/pino-pretty/blob/master/lib/utils.js#L180.

A PR would be awesome.

mcollina avatar May 17 '23 08:05 mcollina

OK I'll have a crack when I find some time!

Still a bit unsure why the \ns aren't being rendered for me though, when it seems like that's all the error serializer uses to produce multi-line output?

https://github.com/pinojs/pino-pretty/blob/master/lib/utils.js#L152

egargan avatar May 17 '23 08:05 egargan

@egargan Hi, did you find the solution?

notmedia avatar Jul 30 '23 23:07 notmedia