node-bunyan-lite/test/child-behaviour.test.js

137 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

/*
* Copyright (c) 2012 Trent Mick. All rights reserved.
*
* Test some `<Logger>.child(...)` behaviour.
*/
var bunyan = require('../lib/bunyan');
2013-01-07 19:18:12 +00:00
// node-tap API
if (require.cache[__dirname + '/tap4nodeunit.js'])
2013-03-29 00:42:32 +00:00
delete require.cache[__dirname + '/tap4nodeunit.js'];
2013-01-07 19:18:12 +00:00
var tap4nodeunit = require('./tap4nodeunit.js');
var after = tap4nodeunit.after;
var before = tap4nodeunit.before;
var test = tap4nodeunit.test;
function CapturingStream(recs) {
2013-03-29 00:42:32 +00:00
this.recs = recs || [];
}
CapturingStream.prototype.write = function (rec) {
2013-03-29 00:42:32 +00:00
this.recs.push(rec);
}
test('child can add stream', function (t) {
2013-03-29 00:42:32 +00:00
var dadStream = new CapturingStream();
var dad = bunyan.createLogger({
name: 'surname',
streams: [ {
type: 'raw',
stream: dadStream,
level: 'info'
} ]
});
var sonStream = new CapturingStream();
var son = dad.child({
component: 'son',
streams: [ {
type: 'raw',
stream: sonStream,
level: 'debug'
} ]
});
dad.info('info from dad');
dad.debug('debug from dad');
son.debug('debug from son');
var rec;
t.equal(dadStream.recs.length, 1);
rec = dadStream.recs[0];
t.equal(rec.msg, 'info from dad');
t.equal(sonStream.recs.length, 1);
rec = sonStream.recs[0];
t.equal(rec.msg, 'debug from son');
t.end();
});
test('child can set level of inherited streams', function (t) {
2013-03-29 00:42:32 +00:00
var dadStream = new CapturingStream();
var dad = bunyan.createLogger({
name: 'surname',
streams: [ {
type: 'raw',
stream: dadStream,
level: 'info'
} ]
});
// Intention here is that the inherited `dadStream` logs at 'debug' level
// for the son.
var son = dad.child({
component: 'son',
level: 'debug'
});
dad.info('info from dad');
dad.debug('debug from dad');
son.debug('debug from son');
var rec;
t.equal(dadStream.recs.length, 2);
rec = dadStream.recs[0];
t.equal(rec.msg, 'info from dad');
rec = dadStream.recs[1];
t.equal(rec.msg, 'debug from son');
t.end();
});
test('child can set level of inherited streams and add streams', function (t) {
2013-03-29 00:42:32 +00:00
var dadStream = new CapturingStream();
var dad = bunyan.createLogger({
name: 'surname',
streams: [ {
type: 'raw',
stream: dadStream,
level: 'info'
} ]
});
// Intention here is that the inherited `dadStream` logs at 'debug' level
// for the son.
var sonStream = new CapturingStream();
var son = dad.child({
component: 'son',
level: 'trace',
streams: [ {
type: 'raw',
stream: sonStream,
level: 'debug'
} ]
});
dad.info('info from dad');
dad.trace('trace from dad');
son.trace('trace from son');
son.debug('debug from son');
t.equal(dadStream.recs.length, 3);
t.equal(dadStream.recs[0].msg, 'info from dad');
t.equal(dadStream.recs[1].msg, 'trace from son');
t.equal(dadStream.recs[2].msg, 'debug from son');
t.equal(sonStream.recs.length, 1);
t.equal(sonStream.recs[0].msg, 'debug from son');
t.end();
});