3

When my program shuts down via process.exit() (maybe also by some signal), pino loses some log entries because it is not properly flushing them.

Triggering a manual flush does not help.

I am using pino 6.2.0.

How can I prevent log entries from getting lost?

const pino = require("pino");

const p = pino();

process.on("exit", () => {
    console.log("FLUSH");
    p.flush();
});

p.info("A"); // logged
p.info("B"); // NOT logged

process.exit(1);

Back story:
I was missing some dependencies in my NestJS module and configured NestJS to use my pino logger wrapper like this:

const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    logger: new PinoLoggerService(),
});

NestJS was just exiting (using process.exit()) without telling me what was wrong. Removing the logger option made NestJS print the offending module in all details.

Arc
  • 11,143
  • 4
  • 52
  • 75

1 Answers1

3

Searching the net yielded a comment on a GitHub issue Pino 5.8.1 fails to flush log on process exit with a similar problem where GitHub user brandondoran pointed out the solution:

Use pino.destination() which will use synchronous writes unless you disable them, no explicit flushing needed.

Note that this will likely slow down your logging.

const pino = require("pino");

const p = pino({}, pino.destination());

p.info("A"); // logged
p.info("B"); // logged as well

process.exit(1);
Arc
  • 11,143
  • 4
  • 52
  • 75