UNPKG

5.57 kBJavaScriptView Raw
1// Copyright (c) 2015 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21
22var test = require('tape');
23
24var captureStdio = require('./lib/capture-stdio.js');
25
26var Logger = require('../logger.js');
27var ConsoleBackend = require('../backends/console.js');
28
29function createLogger() {
30 return Logger({
31 meta: {},
32 backends: {
33 console: ConsoleBackend()
34 },
35 transforms: [
36 pathPrefixTransform
37 ]
38 });
39}
40
41function 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
50test('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
60test('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
71test('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
83test('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
94test('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
111test('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
121test('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
141test('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
159test('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});