2013-01-19 00:27:53 +00:00
|
|
|
/*
|
|
|
|
* A long-running process that does some periodic logging. Use bunyan with
|
|
|
|
* it some of these ways:
|
|
|
|
*
|
|
|
|
* 1. Direct piping:
|
|
|
|
* node long-running.js | bunyan
|
|
|
|
* 2. Logging to file (e.g. if run via a service system like upstart or
|
|
|
|
* illumos' SMF that sends std output to a log file), then tail -f that
|
|
|
|
* log file.
|
|
|
|
* node long-running.js > long-running.log 2>&1
|
|
|
|
* tail -f long-running.log | bunyan
|
|
|
|
* 3. Dtrace to watch the logging. This has the bonus of being able to watch
|
|
|
|
* all log levels... even if not normally emitted.
|
|
|
|
* node long-running.js > long-running.log 2>&1
|
|
|
|
* bunyan -p $(head -1 long-running.log | json pid)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var bunyan = require('../lib/bunyan');
|
|
|
|
|
|
|
|
|
|
|
|
function randint(n) {
|
2013-03-29 00:42:32 +00:00
|
|
|
return Math.floor(Math.random() * n);
|
2013-01-19 00:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function randchoice(array) {
|
2013-03-29 00:42:32 +00:00
|
|
|
return array[randint(array.length)];
|
2013-01-19 00:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---- mainline
|
|
|
|
|
2013-04-02 00:21:01 +00:00
|
|
|
var words = fs.readFileSync(
|
|
|
|
__dirname + '/long-running.js', 'utf8').split(/\s+/);
|
2013-01-19 00:27:53 +00:00
|
|
|
var levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
|
|
|
|
var timeout;
|
|
|
|
|
|
|
|
var log = bunyan.createLogger({name: 'lr', level: 'debug'});
|
|
|
|
|
|
|
|
// We're logging to stdout. Let's exit gracefully on EPIPE. E.g. if piped
|
|
|
|
// to `head` which will close after N lines.
|
|
|
|
process.stdout.on('error', function (err) {
|
2013-03-29 00:42:32 +00:00
|
|
|
if (err.code === 'EPIPE') {
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2013-01-19 00:27:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function logOne() {
|
2013-03-29 00:42:32 +00:00
|
|
|
var level = randchoice(levels);
|
|
|
|
var msg = [randchoice(words), randchoice(words)].join(' ');
|
|
|
|
var delay = randint(300);
|
|
|
|
//console.warn('long-running about to log.%s(..., "%s")', level, msg)
|
|
|
|
log[level]({'word': randchoice(words), 'delay': delay}, msg);
|
|
|
|
timeout = setTimeout(logOne, delay);
|
2013-01-19 00:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.info('hi, this is the start');
|
|
|
|
timeout = setTimeout(logOne, 1000);
|