pinojs / pino-abstract-transport

Write Pino transports easily
MIT License
34 stars 13 forks source link

Use pino-abstract-transport synchronously #58

Closed zekefeu closed 2 years ago

zekefeu commented 2 years ago

Hi, I have a custom transport that sends error reports over a webhook.

However, when I have a fatal error, the transport is not even triggered, which I assume is due to the fact that it is asynchronous.

I've seen you can use sync: true in pino.destination() but from what I understand, it only supports writing to a file, and not to an abstract transport.

How can I make it a synchronous transport, or at least, how do I make sure it is properly flushed before exiting ?

mcollina commented 2 years ago

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

If you are using the transport option of pino it should automatically send all the logs before everything is shut down.

zekefeu commented 2 years ago

logger.ts


import { pino, LoggerOptions } from "pino";

// Development transport: prints logs to console, doesn't report errors to the webhook const devOptions: LoggerOptions = { transport: { targets: [ { // Console output target: "pino-pretty", level: "trace", options: { translateTime: true, ignore: "pid,hostname", sync: false, }, }, { // Webhook target: "./test_webhook.js", level: "error", options: {}, }, ], }, };

// Set up the logger with the correct transports export const logger = pino(devOptions);

logger.fatal("Test error");


> test_webhook.ts
```typescript
import build from "pino-abstract-transport";

export default async function (opts) {
    return build(async function (source) {
        for await (let obj of source) {
            console.log("Webhhok transport:", obj);
        }
    });
}

/* Expected: 
[2022-08-07 10:01:35.720 +0000] FATAL: Test error
Webhhok transport: {
    level: 60,
    time: 1659866495720,
    pid: 11849,
    hostname: 'pop-os',
    msg: 'Test error'
}
*/

/* Actual:
[2022-08-07 10:01:35.720 +0000] FATAL: Test error
*/

Node v18.7.0

pino v8.3.0

pino-abstract-transport v1.0.0

mcollina commented 2 years ago

You can't use console.log in workers after the main process event loop shut down :(. Use sonic-boom.

zekefeu commented 2 years ago

I use console.log() for the example, but in the application I send a POST request using the fetch API. I removed it because I thought this wouldn't be the issue. How can I get around that ?