pinojs / pino-pretty

🌲Basic prettifier for Pino log lines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

egargan opened this issue · 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.

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 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.

The implementation here recognizes serialized errors from Pino.

Yes, exactly. This is not a serialized error.

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?

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

A PR would be awesome.

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 Hi, did you find the solution?