iamolegga / nestjs-pino

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

[QUESTION] Override error message #1057

Closed EcksDy closed 2 years ago

EcksDy commented 2 years ago

[V] I've read the docs of nestjs-pino

[V] I've read the docs of pino

[V] I couldn't find the same question about nestjs-pino

Question

Hello,

I'm implementing an adapter that will align our internal logging conventions to nestjs-pino api.
Our error messages are looking as follows:

catch (error) {
  this.logger.error('Custom error message', error);
  this.logger.error('Custom error message with context', { error, someExtraContext });
}

Is there a way I can manipulate this input to achieve the following behaviour:

Currently in the adapter I manipulate the input into something along the lines of:

  error(message: string, context: unknown): void {
    if (context instanceof Error) {
      const error = context;
      this.logger.error(error, { message }); // The logger used here is `import { Logger } from 'nestjs-pino';`
      return;
    }

    const { error, ...cleanContext } = context as any;
    this.logger.error(error, { message, ...cleanContext }); // The logger used here is `import { Logger } from 'nestjs-pino';`
  }

All my attempts ended up either losing the error information or being unable to override the message of the output log, as the message on the error object takes precedence.

At this point I'm not sure if this is possible, would love to hear your thoughts, thank you!

iamolegga commented 2 years ago

Didn't get what do you mean by "adapter".

First of all this.logger.error('Custom error message with context', { error, someExtraContext }); does not match with the pino's message format: logger.error([mergingObject], [message], [...interpolationValues]

Second, it has LoggerErrorInterceptor which logs stack trace

Third, for pino to log errors properly it should be passed as .err filed on merging object. Read this.

Fourth, pino has serializers options.

I hope something of this will help, closing