docs/asynchronous.md
Asynchronous logging enables the minimum overhead of Pino. Asynchronous logging works by buffering log messages and writing them in larger chunks.
const pino = require('pino')
const logger = pino(pino.destination({
dest: './my-file', // omit for stdout
minLength: 4096, // Buffer before writing
sync: false // Asynchronous logging
}))
It's always possible to turn on synchronous logging by passing sync: true.
In this mode of operation, log messages are directly written to the
output stream as the messages are generated with a blocking operation.
pino.destinationpino.destination is implemented on sonic-boom ⇗.Asynchronous logging is disabled by default on AWS Lambda or any other environment
that modifies process.stdout. If forcefully turned on, we recommend calling dest.flushSync() at the end
of each function execution to avoid losing data.
Asynchronous logging has a couple of important caveats:
logger.info) and writes to a log filepino-prettyThe logger.flush() method does not work when using pino-pretty because:
Transport Architecture: pino-pretty runs in a separate worker thread via the transport mechanism.
Buffer Flow: When you call logger.flush(), it flushes the SonicBoom destination in the main thread, but the logs remain queued in the thread-stream worker waiting to be processed by pino-pretty.
No Cross-Thread Flush: The flush operation never propagates through to the worker thread where the pretty printer is processing the output.
This means that even with logger.flush(), your formatted logs may not appear immediately, and the flush will only ensure the main thread buffer is written, not the formatted output.
See also: