-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
58 lines (49 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
var stream = require('stream');
var util = require('util');
var formatRecord = require('./lib/format-record');
var xtend = require('xtend');
var Writable = stream.Writable;
module.exports = BunyanFormatWritable;
util.inherits(BunyanFormatWritable, Writable);
/**
* Creates a writable stream that formats bunyan records written to it.
*
* @name BunyanFormatWritable
* @function
* @param opts {Options} passed to bunyan format function
* - outputMode: short|long|simple|json|bunyan
* - color (true): toggles colors in output
* - colorFromLevel: allows overriding log level colors
* @param out {Stream} (process.stdout) writable stream to write
* @return {WritableStream} that you can pipe bunyan output into
*/
function BunyanFormatWritable (opts, out) {
if (!(this instanceof BunyanFormatWritable)) return new BunyanFormatWritable(opts, out);
opts = opts || {};
opts.objectMode = true;
Writable.call(this, opts);
this.opts = xtend({
outputMode: 'short',
color: true,
colorFromLevel: {
10: 'brightBlack', // TRACE
20: 'brightBlack', // DEBUG
30: 'green', // INFO
40: 'magenta', // WARN
50: 'red', // ERROR
60: 'brightRed', // FATAL
}
}, opts);
this.out = out || process.stdout;
}
BunyanFormatWritable.prototype._write = function (chunk, encoding, cb) {
var rec;
try {
rec = JSON.parse(chunk);
this.out.write(formatRecord(rec, this.opts));
} catch (e) {
this.out.write(chunk);
}
cb();
};