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

[BUG] getReqId or any function within pinoHttp.options error "could not be cloned" #800

Closed deeeed closed 2 years ago

deeeed commented 2 years ago

What is the current behavior? throws an error when trying to implement custon pino http options.

What is the expected behavior? works as per the documentation :)

Please provide minimal example repo. Without it this issue will be closed

import { Module, RequestMethod } from '@nestjs/common';
import { ReservationsService } from './reservations.service';
import { ReservationsController } from './reservations.controller';
import { LoggerModule } from 'nestjs-pino';
import { environment } from '../environments/environment';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    LoggerModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          // forRoutes: [{ path: '*', method: RequestMethod.ALL }],
          pinoHttp: {
            level: environment.production ? 'info' : 'debug',
            // install 'pino-pretty' package in order to use the following option
            transport: environment.production
              ? undefined
              : {
                  target: 'pino-pretty',
                  options: {
                    genReqId: (req) => {
                      return req.id;
                    },
                    colorize: true,
                    singleLine: true,
                    levelFirst: true,
                    ignore: 'hostname',
                    translateTime: 'UTC:yyyy/mm/dd HH:MM:ss TT Z',
                    sync: true,
                  },
                },
          },
        };
      },
    }),
  ],
  controllers: [ReservationsController],
  providers: [ReservationsService],
})
export class ReservationsModule {}

The error is:

[Nest] 20946  - 02/01/2022, 12:54:55 PM     LOG [NestFactory] Starting Nest application...
[Nest] 20946  - 02/01/2022, 12:54:55 PM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +14ms
[Nest] 20946  - 02/01/2022, 12:54:55 PM     LOG [InstanceLoader] ReservationsModule dependencies initialized +0ms
[Nest] 20946  - 02/01/2022, 12:54:55 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 20946  - 02/01/2022, 12:54:55 PM   ERROR [ExceptionHandler] (req) => {
                                            return req.id;
                                        } could not be cloned.
DataCloneError: (req) => {
                                            return req.id;
                                        } could not be cloned.
    at new Worker (node:internal/worker:235:17)

Not sure what I am missing?

iamolegga commented 2 years ago

As there is only a sample code of a single file it's impossible to deep dive into this, there could be literally anything that can lead to a bug. Could you provide a minimal example repo for this?