logger icon indicating copy to clipboard operation
logger copied to clipboard

feat: automatic error formatting in json layout

Open EinfachHans opened this issue 1 year ago • 4 comments

Informations

Type  Version
Improvement 6.6.3

Description

I'm currently in process of understanding the logger and how it works. I have problems with a usefull error formatting:

For production i enabled the json layout. See the following comparison, between what i log and what the output is:

Log Response
this.logger.error('some error', exception) {"startTime":"2023-11-14T14:30:30.024Z","categoryName":"ExceptionsFilter","level":"ERROR","data":["some error"]}
this.logger.error(exception) {"startTime":"2023-11-14T14:31:21.042Z","categoryName":"ExceptionsFilter","level":"ERROR","data":[]}
this.logger.error({ message: 'some error', exception }) {"startTime":"2023-11-14T14:32:23.111Z","categoryName":"ExceptionsFilter","level":"ERROR","message":"some error","exception":{},"data":[]}

In all of this variants the information about the exception (new Error('test') in my case) is missing.

What would i expect? Good question. When a error ocours the default context logger logs something like this:

{"startTime":"2023-11-14T14:32:23.113Z","categoryName":"Default","level":"ERROR","method":"GET","url":"/healthz","route":"/healthz","headers":{...},"body":{},"query":{},"params":{},"reqId":"f3dc11e567e24389b9847d7321643533","time":"2023-11-14T14:32:23.113Z","duration":9,"event":"request.end","status":500,"status_code":"500","state":"KO","error_name":"Error","error_message":"test","error_stack":"...","data":[]}

This information is explicit added by the PlatformLogMiddleware of @tsed. Maybe it would make sense something similar to the logger itself? Maybe here? Where when an error is detected within the data it could be handled separately.

Or is this not a good solution for some reasons? Then i would have to implement something in my application.

EinfachHans avatar Nov 14 '23 14:11 EinfachHans

Hey @Romakita , did you see this issue? 😃 Just noticed that you are not automatically assigned here

EinfachHans avatar Nov 17 '23 12:11 EinfachHans

Hello @EinfachHans I haven't found this issue.

The problem is around the error object itself (detecting will be a huge cost, because we need to introspect object and nested object), JSON.stringify isn't able to serialize the error instance because Error.prototype haven't a toJSON method. We can see that here:

Capture d’écran 2023-11-18 à 09 41 39

Reading this article explain how it's possible to serialize error: https://zirkelc.dev/posts/stringify-and-parse-errors-in-javascript.

The problem is how to support that correctly for all possible Error type (and custom Error) without exposing critical information of our application (Error.stack ??). This is actually why I haven't fixed this problem on logger level.

Note: @tsed/logger code base was originally based on log4js, and the team haven't fixed that ^^

Romakita avatar Nov 18 '23 08:11 Romakita

Hey @Romakita,

thanks for the explanation! Yeah already thought it is going to be a larger thing 😩

For know i will write a custom logic in my app the works for me 👍🏼

EinfachHans avatar Nov 18 '23 09:11 EinfachHans

You can monkey patch the Exception.prototype.toJSON until we haven’t a good solution ;)

Romakita avatar Nov 18 '23 11:11 Romakita