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] why are logs delayed/slow when using pino-pretty? #1918

Closed rhyek closed 6 months ago

rhyek commented 6 months ago

Question I'm setting up my project for local development and I've noticed that when using the pino-pretty transport target, logs during request handling are frequently delayed for up to a second or so. Very few times they are instant. If I remove the transport target, they are instant.

My config:

// main.ts
const app = await NestFactory.create<NestFastifyApplication>(
  AppModule,
  new FastifyAdapter(),
  {
    bufferLogs: true
  }
);
const logger = app.get(Logger);
app.useLogger(logger);
app.flushLogs();

// app.module.ts
@Module({
  imports: [AppLoggerModule, InfrastructureModule, ApplicationModule]
})
export class AppModule {}

// app-logger.module.ts
@Global()
@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        autoLogging: false, // no req, res logging
        transport:
          process.env.NODE_ENV !== 'production'
            ? {
                target: 'pino-pretty',
                // https://github.com/pinojs/pino-pretty?tab=readme-ov-file#options
                options: {
                  ignore: 'pid,hostname',
                  sync: true
                }
              }
            : undefined
      }
    })
  ],
  providers: [AppLogger],
  exports: [AppLogger]
})
export class AppLoggerModule {}

// app-logger.ts
@Injectable({ scope: Scope.TRANSIENT })
export class AppLogger extends PinoLogger {
  constructor(params: Params, @Inject(INQUIRER) parentClass: object) {
    super(params);
    this.setContext(parentClass.constructor.name);
  }
}

Please mention other relevant information such as Node.js version and Operating System. bun 1.1.3 macos m1

iamolegga commented 6 months ago

this is a question about pino-pretty, which is a 3rd party library, the question should be addressed there image

rhyek commented 6 months ago

right, i guess my thinking is it might be an interaction issue between nestjs-pino and pino-pretty. does that make sense?

iamolegga commented 6 months ago

nestjs-pino is just a handy wrapper for pino instance, and config provided here is just passed to the pino instance as is. So, yeah, maybe try to find the answer there. Also, in pino-pretty docs the first example is piping app stdout to pino-pretty https://github.com/pinojs/pino-pretty?tab=readme-ov-file#usage , maybe this could fix your issue?