pinojs / pino-pretty

🌲Basic prettifier for Pino log lines
MIT License
1.25k stars 147 forks source link

Extention for --messageFormat option to support go template control structures #441

Closed timlohse1104 closed 1 year ago

timlohse1104 commented 1 year ago

Hey there,

we use the --messageFormat in a .pino-prettyrc file to format pino logs of a nestjs fastify service to be more readable.

The issue here would be that not every log emitted from the nestjs service is indeed based upon a http request (for example startup messages or logs emitted from a bull processor).

Let's say we want to format logs like this without req/res objects:

{"level":"DEBUG","time":"21.7.2023, 12:01:08","pid":32678,"hostname":"tlohse-ThinkPad-P53","context":"OcrConsumer","msg":"Job #482: Synchronous OCR analysis took 5.52 seconds."}

and http logs like this with req/res objects:

{"level":"INFO","time":"21.7.2023,` 12:00:56","pid":32678,"hostname":"tlohse-ThinkPad-P53","req":{"id":"req-1","method":"OPTIONS","url":"/ocr","query":{},"headers":{"host":"localhost:3005","connection":"keep-alive","accept":"*/*","access-control-request-method":"POST","access-control-request-headers":"authorization,content-type","origin":"http://localhost:4200","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82","sec-fetch-mode":"cors","sec-fetch-site":"same-site","sec-fetch-dest":"empty","referer":"http://localhost:4200/","accept-encoding":"gzip, deflate, br","accept-language":"de,de-DE;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"},"remoteAddress":"127.0.0.1","remotePort":42528},"res":{"statusCode":204,"headers":{}},"responseTime":4,"msg":"request completed"}

with a --messageFormat like this:

"[Nest] ({req.id}) - {time} (UTC) | {level} | [{context}] {msg}"

The output of the both logs would be:

  1. [Nest] () - 21.7.2023, 12:01:08 (UTC) | DEBUG | [OcrConsumer] Job #482: Synchronous OCR analysis took 5.52 seconds.
  2. [Nest] (req-1) - 21.7.2023, 12:00:56 (UTC) | INFO | [] request completed

You can see what's missing here:

  1. Misses the req.id because it was emitted in an async queue processor context
  2. Misses the context

Would'nt it be nice if --messageFormat would support conditions and other control structures like go templating language?

https://gowebexamples.com/templates/#:~:text=Go%E2%80%99s%20html%2Ftemplate%20package%20provides%20a%20rich%20templating%20language,all%20inputs%20before%20displaying%20it%20to%20the%20browser.

e.g. a --messageFormat like this could do the trick and remove leftover parts like braces around undefined values:

"[Nest] {if req.id}({req.id}){end} - {time} (UTC) | {level} | {if context}[{context}] {end}{msg}"

mcollina commented 1 year ago

It would be awesome. Would you like to implement it?

timlohse1104 commented 1 year ago

Will try to do so. First time doing open source things.

timlohse1104 commented 1 year ago

Please consider my PR for this issue: https://github.com/pinojs/pino-pretty/pull/442.