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"}
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:
You can see what's missing here:
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}"