Name: js-handler/node_modules/restify/node_modules/bunyan/examples/long-running.js 
1:
/*
2:
 * A long-running process that does some periodic logging. Use bunyan with
3:
 * it some of these ways:
4:
 *
5:
 * 1. Direct piping:
6:
 *        node long-running.js | bunyan
7:
 * 2. Logging to file (e.g. if run via a service system like upstart or
8:
 *    illumos' SMF that sends std output to a log file), then tail -f that
9:
 *    log file.
10:
 *        node long-running.js > long-running.log 2>&1
11:
 *        tail -f long-running.log | bunyan
12:
 * 3. Dtrace to watch the logging. This has the bonus of being able to watch
13:
 *    all log levels... even if not normally emitted.
14:
 *        node long-running.js > long-running.log 2>&1
15:
 *        bunyan -p $(head -1 long-running.log | json pid)
16:
 *
17:
 */
18:
 
19:
var fs = require('fs');
20:
var bunyan = require('../lib/bunyan');
21:
 
22:
 
23:
function randint(n) {
24:
    return Math.floor(Math.random() * n);
25:
}
26:
 
27:
function randchoice(array) {
28:
    return array[randint(array.length)];
29:
}
30:
 
31:
 
32:
//---- mainline
33:
 
34:
var words = fs.readFileSync(
35:
    __dirname + '/long-running.js', 'utf8').split(/\s+/);
36:
var levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
37:
var timeout;
38:
 
39:
var log = bunyan.createLogger({name: 'lr', level: 'debug'});
40:
 
41:
// We're logging to stdout. Let's exit gracefully on EPIPE. E.g. if piped
42:
// to `head` which will close after N lines.
43:
process.stdout.on('error', function (err) {
44:
    if (err.code === 'EPIPE') {
45:
        process.exit(0);
46:
    }
47:
})
48:
 
49:
function logOne() {
50:
    var level = randchoice(levels);
51:
    var msg = [randchoice(words), randchoice(words)].join(' ');
52:
    var delay = randint(300);
53:
    //console.warn('long-running about to log.%s(..., "%s")', level, msg)
54:
    log[level]({'word': randchoice(words), 'delay': delay}, msg);
55:
    timeout = setTimeout(logOne, delay);
56:
}
57:
 
58:
log.info('hi, this is the start');
59:
timeout = setTimeout(logOne, 1000);