node-bunyan-lite/examples/raw-stream.js
Trent Mick 37a1447f22 per issue #8 change the way a raw stream is handled
"raw" is now a stream "type" isntead of a separate attribute. The
idea is that writing raw objects (instead of JSON-stringified stings)
to, e.g. a file WriteStream isn't useful. So, lets re-use the stream
"type" field instead of an additional "raw" field.
2012-06-21 14:49:04 -07:00

37 lines
860 B
JavaScript

// Example of a "raw" stream in a Bunyan Logger. A raw stream is one to
// which log record *objects* are written instead of the JSON-serialized
// string.
var Logger = require('../lib/bunyan');
/**
* A raw Bunyan Logger stream object. It takes raw log records and writes
* them to stdout with an added "yo": "yo" field.
*/
function MyRawStream() {}
MyRawStream.prototype.write = function (rec) {
if (typeof (rec) !== 'object') {
console.error('error: raw stream got a non-object record: %j', rec)
} else {
rec.yo = 'yo';
process.stdout.write(JSON.stringify(rec) + '\n');
}
}
// A Logger using the raw stream.
var log = new Logger({
name: 'raw-example',
streams: [
{
level: 'info',
stream: new MyRawStream(),
type: 'raw'
},
]
});
log.info('hi raw stream');
log.info({foo: 'bar'}, 'added "foo" key');