Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging inside process exit event? #1400

Closed
06000208 opened this issue Apr 8, 2022 · 8 comments · Fixed by #1474
Closed

Logging inside process exit event? #1400

06000208 opened this issue Apr 8, 2022 · 8 comments · Fixed by #1474
Assignees

Comments

@06000208
Copy link

06000208 commented Apr 8, 2022

Before migrating to pino, I used this pattern with various loggers:

process.on("exit", (code) => code == 0 ? console.log("Exiting peacefully") : console.error(`Exiting abnormally with code ${code}`));

This works correctly with pino with the defaults:

import pino from "pino";
import process from "node:process";
const log = pino();
log.info("hello world!");
process.on("exit", (code) => code == 0 ? log.info("Exiting peacefully") : log.fatal(`Exiting abnormally with code ${code}`));
{"level":30,"time":1649412627196,"pid":20108,"hostname":"pc","msg":"hello world!"}
{"level":30,"time":1649412627198,"pid":20108,"hostname":"pc","msg":"Exiting peacefully"}

However, when using a transport (such as pino/file), an error is thrown and the line isn't logged:

import pino from "pino";
import process from "node:process";
const log = pino({
    transport: {
        target: "pino/file",
        options: { destination: `${Date.now()}.log` },
    },
});
log.info("hello world!");
process.on("exit", (code) => code == 0 ? log.info("Exiting peacefully") : log.fatal(`Exiting abnormally with code ${code}`));
{"level":30,"time":1649413220294,"pid":15488,"hostname":"pc","msg":"hello world!"}
G:\example>node index
G:\example\node_modules\thread-stream\index.js:214
      throw new Error('the worker has exited')
      ^

Error: the worker has exited
    at ThreadStream.write (G:\example\node_modules\thread-stream\index.js:214:13)
    at Pino.write (G:\example\node_modules\pino\lib\proto.js:208:10)
    at Pino.LOG [as info] (G:\example\node_modules\pino\lib\tools.js:60:21)
    at process.<anonymous> (file:///G:/example/index.js:9:46)
    at process.emit (node:events:390:28)

I noticed pino.final() in the docs, but it's deprecated, and doesn't seem intended for this anyway.

Is there a way of logging from an exit event, or is this a bug/not possible?

@mcollina
Copy link
Member

mcollina commented Apr 8, 2022

This is a good bug, the good news is that we can totally support it. The problem is in order for transports to work we need to add an exit handler ourselves... in which we close the worker. We might need to change the logic a bit to make sure pino exit handler is always the last.

@mcollina
Copy link
Member

mcollina commented Apr 8, 2022

The bad news is that I might not have time to work on it.

@jsumners
Copy link
Member

jsumners commented Apr 8, 2022

Is this the return of? #1075 (comment)

@mcollina
Copy link
Member

mcollina commented Apr 8, 2022

Same feature, different implementation. The fix is in https://github.com/mcollina/on-exit-leak-free and we must make sure that the specified handler is always the last.

@msainiuk-bb
Copy link

msainiuk-bb commented Jun 16, 2022

I solved it in such way

const flushLogs = ({ logger }) =>
  new Promise((resolve) => {
    const symbol = Object.getOwnPropertySymbols(logger).find(
      (key) => String(key) === 'Symbol(pino.stream)',
    );
    const threadStream = logger[symbol];
    if (threadStream) {
      threadStream.flushSync();
      threadStream.end(resolve);
    } else {
      resolve(void 0);
    }
  });

but in my Nest.js code (works well in Koa project) it leads to throw new Error('the worker has exited')

@mcollina mcollina self-assigned this Jun 19, 2022
@mcollina
Copy link
Member

I debugged this.

The fundamental issue is that on-exit-leak-free listen to both 'beforeExit' and 'exit' and pino is closing the thread stream inside 'beforeExit'. By the time it reaches the 'exit' event, the stream is already closed.

I'm preparing a significant refactor inside on-exit-leak-free to have some APIs to handle this case.

@mcollina
Copy link
Member

Here is the fix: #1474

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants