iamolegga / nestjs-pino

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

[BUG] Pino logger unavailable in nestjs shutdown functions #1859

Open dilshans2k opened 6 months ago

dilshans2k commented 6 months ago

What is the current behavior? Using nestjs pino logger in beforeApplicationShutdown and onApplicationShutdown callbacks doesn't log in the stdout. However in onModuleDestroy using logger, logs the event. console.log works in all three callbacks.

What is the expected behavior? Nestjs pino logger should be available in all three shutdown callbacks.

Please provide minimal example repo, not code snippet. Without example repo this issue will be closed. https://github.com/dilshans2k/nestjs-pino-issue/tree/feature/pino-test-with-nestjspino4-pino8.18-pinohttp9 In this repo, i have app module, then a nested module inside app module and a provider 'GracefulShutdownEventLogger'. Now what i have seen is if i put a sleep in the provider 'GracefulShutdownEventLogger' in onApplicationShutdown cb (since this provider will be called the last upon application destroy), all other services application shutdown callbacks will be able to log using pino logger, but i don't know why this happens. And why do i need to wait in the 'GracefulShutdownEventLogger'.

Please mention other relevant information such as Node.js version and Operating System. Node@16.20.2 Ubuntu 22.04

dilshans2k commented 6 months ago

I learned that pino uses asynchronous logging, and their docs mention that some buffered logs might get lost on sudden system failure. I get that this can happen if the process receives a SIGKILL, but a SIGTERM or SIGINT is not a sudden failure, so pino should flush the logs properly on those signals. Maybe there is something wrong with my code. Can you please check it?

iamolegga commented 6 months ago

Thanks for reporting this. Unfortunately, I haven't had a chance to check this yet, I will get back to this as soon as possible

clintonb commented 2 months ago

I just came across this issue, too.