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

149 lines
3.6 KiB
JavaScript
Raw Normal View History

/*
* Copyright (c) 2012 Trent Mick. All rights reserved.
*
* Test some `<Logger>.child(...)` behaviour.
*/
import { Eltro as t, assert} from 'eltro'
import bunyan from '../lib/bunyan.mjs'
2013-01-07 19:18:12 +00:00
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);
}
t.test('child can add stream', function () {
let dadStream = new CapturingStream();
let dad = bunyan.createLogger({
2013-03-29 00:42:32 +00:00
name: 'surname',
streams: [ {
type: 'raw',
stream: dadStream,
level: 'info'
} ]
});
let sonStream = new CapturingStream();
let son = dad.child({
2013-03-29 00:42:32 +00:00
component: 'son',
streams: [ {
type: 'raw',
stream: sonStream,
level: 'debug'
} ]
});
dad.info('info from dad');
dad.debug('debug from dad');
son.debug('debug from son');
let rec;
assert.equal(dadStream.recs.length, 1);
2013-03-29 00:42:32 +00:00
rec = dadStream.recs[0];
assert.equal(rec.msg, 'info from dad');
assert.equal(sonStream.recs.length, 1);
2013-03-29 00:42:32 +00:00
rec = sonStream.recs[0];
assert.equal(rec.msg, 'debug from son');
2013-03-29 00:42:32 +00:00
});
t.test('child can set level of inherited streams', function () {
let dadStream = new CapturingStream();
let dad = bunyan.createLogger({
2013-03-29 00:42:32 +00:00
name: 'surname',
streams: [ {
type: 'raw',
stream: dadStream,
level: 'info'
} ]
});
// Intention here is that the inherited `dadStream` logs at 'debug' level
// for the son.
let son = dad.child({
2013-03-29 00:42:32 +00:00
component: 'son',
level: 'debug'
});
dad.info('info from dad');
dad.debug('debug from dad');
son.debug('debug from son');
let rec;
assert.equal(dadStream.recs.length, 2);
2013-03-29 00:42:32 +00:00
rec = dadStream.recs[0];
assert.equal(rec.msg, 'info from dad');
2013-03-29 00:42:32 +00:00
rec = dadStream.recs[1];
assert.equal(rec.msg, 'debug from son');
2013-03-29 00:42:32 +00:00
});
t.test('child can set level of inherited streams and add streams', function () {
let dadStream = new CapturingStream();
let dad = bunyan.createLogger({
2013-03-29 00:42:32 +00:00
name: 'surname',
streams: [ {
type: 'raw',
stream: dadStream,
level: 'info'
} ]
});
// Intention here is that the inherited `dadStream` logs at 'debug' level
// for the son.
let sonStream = new CapturingStream();
let son = dad.child({
2013-03-29 00:42:32 +00:00
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');
assert.equal(dadStream.recs.length, 3);
assert.equal(dadStream.recs[0].msg, 'info from dad');
assert.equal(dadStream.recs[1].msg, 'trace from son');
assert.equal(dadStream.recs[2].msg, 'debug from son');
2013-03-29 00:42:32 +00:00
assert.equal(sonStream.recs.length, 1);
assert.equal(sonStream.recs[0].msg, 'debug from son');
2013-03-29 00:42:32 +00:00
});
// issue #291
t.test('child should not lose parent "hostname"', function () {
let stream = new CapturingStream();
let dad = bunyan.createLogger({
name: 'hostname-test',
hostname: 'bar0',
streams: [ {
type: 'raw',
stream: stream,
level: 'info'
} ]
});
let son = dad.child({component: 'son'});
dad.info('HI');
son.info('hi');
assert.equal(stream.recs.length, 2);
assert.equal(stream.recs[0].hostname, 'bar0');
assert.equal(stream.recs[1].hostname, 'bar0');
assert.equal(stream.recs[1].component, 'son');
});