Pino is a very low overhead JavaScript logger. Low overhead is important when you have a lot of logs.
Warning: Synchronous writes block the event loop until the write has completed. This can be near instantaneous in the case of output to a file, but under high system load, pipes that are not being read at the receiving end, or with slow terminals or file systems, it’s possible for the event loop to be blocked often enough and long enough to have severe negative performance impacts. This may not be a problem when writing to an interactive terminal session, but consider this particularly careful when doing production logging to the process output streams.
If you’re not careful, logs can consume the entire event loop utilization.
Process i/o behavior differences between Terminals, Files, and Pipes
On a related note: process.stdout.write
is synchronous when writing to a file or terminal but asynchronous when writing to a pipe. Sim Lijin wrote about disappearing bytes in Node.js which is due to these behaviors. In particular, synchronous writes are used for terminals because process I/O may not complete if you call process.exit()
. This causes what may be unexpected differences in behavior. For example, pipes are async:
node -e "process.stdout.write('@'.repeat(128 * 1024));" | wc -c
131072
But if you call process.exit(0)
while writing to an async pipe, the result is truncated because the async write is interrupted:
node -e "process.stdout.write('@'.repeat(128 * 1024)); process.exit(0);" | wc -c
65536
If you write to a file, process I/O writes are synchronous, so the entire content is written:
node -e "process.stdout.write('@'.repeat(128 * 1024)); process.exit(0);" >node-stdout-test.out && wc -c node-stdout-test.out
131072 node-stdout-test.out
The reason the async write is 65,536 bytes comes from the pipe capacity of 16 pages. According to man pipe(7)
:
Before Linux 2.6.11, the capacity of a pipe was the same as the system page size (e.g., 4096 bytes on i386). Since Linux 2.6.11, the pipe capacity is 16 pages (i.e., 65,536 bytes in a system with a page size of 4096 bytes).
Leave a Reply