Closed replete closed 2 years ago
If you only need pino-pretty, you could avoid transport and use it directly (see https://github.com/pinojs/pino-pretty#programmatic-integration).
Regarding your example, the problem in your transport is that a single chunk might contain more than one log line and JSON.parse() would convert only the first. Use https://github.com/pinojs/pino-abstract-transport.
You should also not use console.log()
or process.stdout()
but rely on pino.destination()
.
@mcollina I think this has been closed prematurely...
If you only need pino-pretty, you could avoid transport and use it directly (see https://github.com/pinojs/pino-pretty#programmatic-integration).
I did implement it like this in the end after realizing it was easier. The problem happens whether using pino-pretty
or my example custom transport, which I only included as a secondary example to the actual problem
Regarding your example, the problem in your transport is that a single chunk might contain more than one log line and JSON.parse() would convert only the first. Use https://github.com/pinojs/pino-abstract-transport.
That's not true at all, it does split lines (and worked fine) but it's not relevant to this issue, and was only included as a secondary example of the problem, which is going to be a common occurence
const logEvents = chunk.toString().match(/[^\r\n]+/g); // split into lines
You should also not use
console.log()
orprocess.stdout()
but rely onpino.destination()
.
I appreciate that, but this example was given in the documentation here. This issue has nothing to do with the custom transport, I just used it as an example of how using pino-pretty or a custom transport results in missing logs to stdout in this scenario.
I've gone to the effort of isolating this problem, could you please look at this again? Is this expected behaviour? There are loads of express tutorials around that use pino for logging, and they will implement a log on app startup like this, so it seems pretty likely to me that this issue will come up for other people... that was why I opened a ticket, imagine learning a framework but you're not seeing some errors
There is nothing we can fix here :( as it is a limitation of Node.js core. The problem is in how console.log() works inside transports, which sends data to the main thread before printing them out. Due to your shutdown logic, anything logged in same tick before process.exit()
will not be logged.
pino.destination()
does not suffer from this problem. console.log()
should not be included in the transport docs, that's a problem we will fix.
Hopefully now it's clearer, let us know.
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.
Errors don't end up in
stdout
when using pino-pretty or a custom transport after catching en error (e.g. typical express + mongoose app setup)The following is the
README.md
from this issue's repro repository, which explains the problem:Missing log output issue
This repo has three express applications, each identical apart from the configuration passed into
pino()
.The problem is that when using a custom transport, in this scenario, no output is logged.
Install
Run
Pino with default configuration:
Running
npm run appWithPino
results in expected behaviour.Pino configured with pretty-print:
Running
npm run appWithPinoPretty
results in missing logs:Pino configured with custom transport:
Running
npm run appWithPinoCustom
results in even more missing logs: