node-bunyan-lite/test/raw-stream.test.js

118 lines
2.6 KiB
JavaScript
Raw Normal View History

2012-06-20 23:04:23 +00:00
/*
* Copyright (c) 2012 Trent Mick. All rights reserved.
*
* Test `type: 'raw'` Logger streams.
2012-06-20 23:04:23 +00:00
*/
var format = require('util').format;
var Logger = require('../lib/bunyan');
2013-01-07 19:18:12 +00:00
// node-tap API
if (require.cache[__dirname + '/tap4nodeunit.js'])
delete require.cache[__dirname + '/tap4nodeunit.js'];
var tap4nodeunit = require('./tap4nodeunit.js');
var after = tap4nodeunit.after;
var before = tap4nodeunit.before;
var test = tap4nodeunit.test;
2012-06-20 23:04:23 +00:00
function CapturingStream(recs) {
this.recs = recs;
}
CapturingStream.prototype.write = function (rec) {
this.recs.push(rec);
}
test('raw stream', function (t) {
var recs = [];
var log = new Logger({
name: 'raw-stream-test',
2012-06-20 23:27:41 +00:00
streams: [
{
stream: new CapturingStream(recs),
type: 'raw'
2012-06-20 23:27:41 +00:00
}
]
2012-06-20 23:04:23 +00:00
});
log.info('first');
log.info({two: 'deux'}, 'second');
t.equal(recs.length, 2);
2012-06-20 23:27:41 +00:00
t.equal(typeof (recs[0]), 'object', 'first rec is an object');
2012-06-20 23:04:23 +00:00
t.equal(recs[1].two, 'deux', '"two" field made it through');
t.end();
});
test('raw streams and regular streams can mix', function (t) {
var rawRecs = [];
var nonRawRecs = [];
var log = new Logger({
name: 'raw-stream-test',
streams: [
{
stream: new CapturingStream(rawRecs),
type: 'raw'
2012-06-20 23:04:23 +00:00
},
{
stream: new CapturingStream(nonRawRecs)
}
]
});
log.info('first');
log.info({two: 'deux'}, 'second');
t.equal(rawRecs.length, 2);
2012-06-20 23:27:41 +00:00
t.equal(typeof (rawRecs[0]), 'object', 'first rawRec is an object');
2012-06-20 23:04:23 +00:00
t.equal(rawRecs[1].two, 'deux', '"two" field made it through');
t.equal(nonRawRecs.length, 2);
2012-06-20 23:27:41 +00:00
t.equal(typeof (nonRawRecs[0]), 'string', 'first nonRawRec is a string');
2012-06-20 23:04:23 +00:00
t.end();
});
test('child adding a non-raw stream works', function (t) {
var parentRawRecs = [];
var rawRecs = [];
var nonRawRecs = [];
var logParent = new Logger({
name: 'raw-stream-test',
streams: [
{
stream: new CapturingStream(parentRawRecs),
type: 'raw'
2012-06-20 23:04:23 +00:00
}
]
});
var logChild = logParent.child({
child: true,
streams: [
{
stream: new CapturingStream(rawRecs),
type: 'raw'
2012-06-20 23:04:23 +00:00
},
{
stream: new CapturingStream(nonRawRecs)
}
]
});
logParent.info('first');
logChild.info({two: 'deux'}, 'second');
t.equal(rawRecs.length, 1,
format('rawRecs length should be 1 (is %d)', rawRecs.length));
2012-06-20 23:27:41 +00:00
t.equal(typeof (rawRecs[0]), 'object', 'rawRec entry is an object');
2012-06-20 23:04:23 +00:00
t.equal(rawRecs[0].two, 'deux', '"two" field made it through');
t.equal(nonRawRecs.length, 1);
2012-06-20 23:27:41 +00:00
t.equal(typeof (nonRawRecs[0]), 'string', 'first nonRawRec is a string');
2012-06-20 23:04:23 +00:00
t.end();
});