iamolegga / nestjs-pino

Platform agnostic logger for NestJS based on Pino with REQUEST CONTEXT IN EVERY LOG
MIT License
1.28k stars 98 forks source link

Duplicate log error information #2094

Closed hdlinh1808 closed 4 weeks ago

hdlinh1808 commented 4 weeks ago

Question Hi team, I have config of logging in my project like this

LoggerModule.forRoot({
            pinoHttp: {
                level: 'info',
                autoLogging: false,
                transport: {
                    target: 'pino-pretty',
                    options: {
                        colorize: false,
                        translateTime: "yyyy-mm-dd'T'HH:MM:ss.l'Z'", // Customize time format
                        singleLine: true,
                        messageFormat: '{msg} {err}', // Log all details in one line
                    },
                },
                timestamp: true,
                serializers: {
                    err: (error) => {
                        // Replace newlines in the stack trace with spaces
                        const stack = error.stack ? error.stack.replace(/\n/g, ' ') : '';

                        return JSON.stringify({
                            type: error.name, // Include the error type (e.g., BadRequestException)
                            stack: stack, // Full stack trace as a single line
                        });
                    },
                    req: (req) => ({
                        id: req.id,
                        method: req.method,
                        url: req.url,
                    }),
                },
            },
        }),

But when I log like that in the filter:

@Catch(Error)
export class AllExceptionsFilter implements ExceptionFilter {
    private readonly logger = new Logger(AllExceptionsFilter.name);
    catch(exception: Error, host: ArgumentsHost): void {
        this.logger.error('something error', exception.stack);

My output error is duplicated:

something error {"stack":"BadRequestException: user not exist!     at UserService.getAndValidateUserByEmail (/app/dist/modules/user/services/user.service.js:99:19)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)     at async UserService.removeUserByEmail (/app/dist/modules/user/services/user.service.js:119:22)"} {"req":{"id":1,"method":"DELETE","url":"/api/users?email=test@gmail.com"},"context":"AllExceptionsFilter"}
    err: "{\"stack\":\"BadRequestException: user not exist!     at UserService.getAndValidateUserByEmail (/app/dist/modules/user/services/user.service.js:99:19)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)     at async UserService.removeUserByEmail (/app/dist/modules/user/services/user.service.js:119:22)\"}"

How can I remove the second line? Thank you very much! Please mention other relevant information such as Node.js version and Operating System.

iamolegga commented 4 weeks ago

I believe it's duplicated due to messageFormat: '{msg} {err}' as any error also goes to the err field of the logs entry. Also, check out the proper usage of arguments here

hdlinh1808 commented 4 weeks ago

@iamolegga I tried to remove it before, can solve the duplication, but my main purpose is making our log in just one line. If I remove it, my log still span 2 lines that make hard when searching:

something error 
    err: "{\"stack\":\"BadRequestException: user not exist!     at UserService.getAndValidateUserByEmail (/app/dist/modules/user/services/user.service.js:99:19)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)     at async UserService.removeUserByEmail (/app/dist/modules/user/services/user.service.js:119:22)\"}"
iamolegga commented 4 weeks ago

you need to get rid of pino-pretty