1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var test = require('tape');
|
23 |
|
24 | var captureStdio = require('./lib/capture-stdio.js');
|
25 |
|
26 | var Logger = require('../logger.js');
|
27 | var ConsoleBackend = require('../backends/console.js');
|
28 |
|
29 | function createLogger() {
|
30 | return Logger({
|
31 | meta: {},
|
32 | backends: {
|
33 | console: ConsoleBackend()
|
34 | },
|
35 | transforms: [
|
36 | pathPrefixTransform
|
37 | ]
|
38 | });
|
39 | }
|
40 |
|
41 | function pathPrefixTransform(entry) {
|
42 | return new entry.constructor(
|
43 | entry.level,
|
44 | (entry.path ? entry.path + ': ' : '') + entry.message,
|
45 | entry.meta,
|
46 | entry.path
|
47 | );
|
48 | }
|
49 |
|
50 | test('root logger paths', function t(assert) {
|
51 | var logger = createLogger();
|
52 |
|
53 | assert.ok(captureStdio('info: hello who=world', function capture() {
|
54 | logger.info('hello', { who: 'world' });
|
55 | }));
|
56 |
|
57 | assert.end();
|
58 | });
|
59 |
|
60 | test('child logger path', function t(assert) {
|
61 | var logger = createLogger();
|
62 | var childLogger = logger.createChild('child', {info: true});
|
63 |
|
64 | assert.ok(captureStdio('info: child: hello who=world', function t() {
|
65 | childLogger.info('hello', { who: 'world' });
|
66 | }));
|
67 |
|
68 | assert.end();
|
69 | });
|
70 |
|
71 | test('child logger path', function t(assert) {
|
72 | var logger = createLogger();
|
73 | var childLogger = logger.createChild('child');
|
74 | var grandchildLogger = childLogger.createChild('child');
|
75 |
|
76 | assert.ok(captureStdio('info: child.child: hello who=world', function t() {
|
77 | grandchildLogger.info('hello', { who: 'world' });
|
78 | }));
|
79 |
|
80 | assert.end();
|
81 | });
|
82 |
|
83 | test('child logger can extend meta', function t(assert) {
|
84 | var logger = createLogger();
|
85 | var childLogger = logger.createChild('child', {info: true}, {extendMeta: true, meta: {foo: 'bar'}});
|
86 |
|
87 | assert.ok(captureStdio('info: child: hello foo=bar, who=world', function t() {
|
88 | childLogger.info('hello', { who: 'world' });
|
89 | }));
|
90 |
|
91 | assert.end();
|
92 | });
|
93 |
|
94 | test('child logger can log filtered meta', function t(assert) {
|
95 | var foo = {bar: 'baz'};
|
96 | var logger = createLogger();
|
97 | var childLogger = logger.createChild('child', {info: true},
|
98 | {extendMeta: true, metaFilter: [{object: foo, mappings:{bar:'fooBar'}}]});
|
99 |
|
100 | assert.ok(captureStdio('info: child: hello fooBar=baz, who=world', function t() {
|
101 | childLogger.info('hello', { who: 'world' });
|
102 | }));
|
103 | foo.bar = 'qux';
|
104 | assert.ok(captureStdio('info: child: hello fooBar=qux, who=world', function t() {
|
105 | childLogger.info('hello', { who: 'world' });
|
106 | }));
|
107 |
|
108 | assert.end();
|
109 | });
|
110 |
|
111 | test('child logger can not be constructed ' +
|
112 | 'without meta or fields when asked to extend meta', function t(assert) {
|
113 |
|
114 | var logger = createLogger();
|
115 | assert.throws(function () {
|
116 | logger.createChild('child', {info: true}, {extendMeta: true});
|
117 | });
|
118 | assert.end();
|
119 | });
|
120 |
|
121 | test('child logger can not be constructed ' +
|
122 | 'with bad field config', function t(assert) {
|
123 | var foo = {bar: 'baz'};
|
124 |
|
125 | var logger = createLogger();
|
126 | assert.throws(function () {
|
127 | logger.createChild('child', {info: true},
|
128 | {extendMeta: true, metaFilter: [{mappings:{bar:'fooBar'}}]});
|
129 | });
|
130 | assert.throws(function () {
|
131 | logger.createChild('child1', {info: true},
|
132 | {extendMeta: true, metaFilter: [{object: foo}]});
|
133 | });
|
134 | assert.throws(function () {
|
135 | logger.createChild('child2', {info: true},
|
136 | {extendMeta: true, metaFilter: [{object: foo, mappings:{bar:{}}}]});
|
137 | });
|
138 | assert.end();
|
139 | });
|
140 |
|
141 | test('child logger can be constructed ' +
|
142 | 'with extra levels without strict', function t(assert) {
|
143 |
|
144 | var logger = createLogger();
|
145 | var childLogger;
|
146 | assert.ok(captureStdio('warn: Child Logger Disabled level level=floop', function t() {
|
147 | childLogger = logger.createChild('child', {info: true, floop: true}, 'Got warning for disabled level');
|
148 | }));
|
149 | assert.ok(captureStdio('info: child: hello who=world', function t() {
|
150 | childLogger.info('hello', { who: 'world' });
|
151 | }), 'Enabled levels can log');
|
152 | assert.notok(captureStdio('floop: child: hello who=world', function t() {
|
153 | childLogger.floop('hello', { who: 'world' });
|
154 | }), 'Disabled levels do not log');
|
155 |
|
156 | assert.end();
|
157 | });
|
158 |
|
159 | test('child logger can not be constructed ' +
|
160 | 'with extra levels with strict', function t(assert) {
|
161 |
|
162 | var logger = createLogger();
|
163 | assert.throws(function () {
|
164 | logger.createChild('child', {info: true, floop:true}, {strict: true});
|
165 | });
|
166 | assert.end();
|
167 | });
|