Skip to content

Instantly share code, notes, and snippets.

@Jimbly
Last active December 14, 2015 16:08
Show Gist options
  • Save Jimbly/5112692 to your computer and use it in GitHub Desktop.
Save Jimbly/5112692 to your computer and use it in GitHub Desktop.
Logging to zlib log stream with periodic flushing.
var assert = require('assert');
var zlib = require('zlib');
var gzip = zlib.createGzip();
var fs = require('fs');
var FILENAME = 'log.txt.gz';
var out = fs.createWriteStream(FILENAME);
gzip.pipe(out);
function debug(msg) {
console.log((new Date()).toISOString() + ' DEBUG> ' + msg);
}
var needs_flush = false;
var is_flushing = false;
var FLUSH_DELAY = 2000;
function doFlush() {
needs_flush = false;
debug('caling flush');
gzip.flush(function() {
debug('flush finished');
is_flushing = false;
if (needs_flush) {
is_flushing = true;
setTimeout(doFlush, FLUSH_DELAY);
}
});
}
function writeLine(data) {
debug('calling write');
gzip.write((new Date()).toISOString() + ' ' + data + '\n', function() {
debug('write finished');
});
needs_flush = true;
if (!is_flushing) {
is_flushing = true;
setTimeout(doFlush, FLUSH_DELAY);
}
}
setInterval(function() {
// write some data
writeLine('A line of data');
}, 500);
var last_size = 0;
setInterval(function() {
fs.stat('log.txt.gz', function(err, stats) {
console.log('File size at ' + stats.size);
assert(stats.size > last_size);
last_size = stats.size;
});
}, 5000);
@Jimbly
Copy link
Author

Jimbly commented Mar 7, 2013

This works on older versions of node (0.6.21), but throws an internal assertion on node 0.9.21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment