Buffer calls to console.log, console.warn, etc. for high performance logging
Description
Calls to console.log, console.error, etc. are synchronous, and as such,
will block the event loop while the data is being written to a file, terminal,
socket, pipe, etc.
This module provides a buffer for all calls to these functions, and flushes
them when the buffers exceed a certain size (8k by default).
See Known Issues for timing concerns with this module.
Example
require('log-buffer');console.log('Hello'); // bufferedconsole.log('world'); // buffered// flushed at exit or 8k of data
Even though there are 2 calls to console.log, this example only writes to a
file descriptor once.
Customization
You can specify an alternative buffer size to use for automatic flushing like
this:
require('log-buffer')(4096); // buffer will flush at 4k
This module also exposes the flush function used to flush all buffers, so
if you would like you can manually invoke a flush. Also, you can
specify an interval to automatically flush all buffers so logs don't get held
in memory indefinitely.
var logbuffer = require('log-buffer');setInteval(function() { logbuffer.flush();}, 5000); // flush every 5 seconds
Benchmark
Counting to a million, logging each iteration, without buffering
$ time node examples/count.js > /dev/nullreal 0m4.658suser 0m4.406ssys 0m0.337s
Counting to a million, logging each iteration, with buffering (8k)
$ time node examples/bcount.js > /dev/nullreal 0m1.903suser 0m1.920ssys 0m0.027s
Install
npm install log-buffer
Tests
npm test
Known Issues
All buffers are flushed when flush is called (whether automatically
or manually). Because of this, calls to different console family functions
may return out of order.