-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlogger.test.js
191 lines (147 loc) · 4.83 KB
/
logger.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const test = require('ava');
const createLogger = require('./logger');
const parseJSONStream = require('./utils/parse-json-stream');
const streamToGenerator = require('./utils/stream-to-generator');
const loggerMethodUseCases = [
{ input: { method: 'trace' }, expected: { level: 10 } },
{ input: { method: 'debug' }, expected: { level: 20 } },
{ input: { method: 'info' }, expected: { level: 30 } },
{ input: { method: 'warn' }, expected: { level: 40 } },
{ input: { method: 'error' }, expected: { level: 50 } },
{ input: { method: 'fatal' }, expected: { level: 60 } },
];
async function testMethod(t, input = {}, expected = {}) {
const { method } = input;
const { level } = expected;
const stream = parseJSONStream();
const logger = createLogger({ level: 'trace' }, stream);
const gen = streamToGenerator(stream);
logger[method]('foo');
const data = await gen.next().value;
t.is(data.level, level);
}
loggerMethodUseCases.forEach(({ input, expected }) => {
test(
`Logger methods: ${JSON.stringify(input)} - ${JSON.stringify(expected)}`,
testMethod,
input,
expected
);
});
test(`Properly logs message with context object`, async t => {
const stream = parseJSONStream();
const gen = streamToGenerator(stream);
const logger = createLogger({}, stream);
const context = { dummy: 'value' };
const message = 'foo';
logger.info(context, message);
const entry = await gen.next().value;
t.deepEqual(entry.dummy, context.dummy);
t.deepEqual(entry.msg, message);
});
test(`Exposes a CLS namespace`, t => {
const logger = createLogger();
t.truthy(logger.cls);
});
test(`2 different loggers don't share the same namespace`, t => {
const logger = createLogger();
const anotherLogger = createLogger();
t.notDeepEqual(logger.cls, anotherLogger.cls);
});
test.cb(`Should properly log message with cls context`, t => {
const stream = parseJSONStream();
const gen = streamToGenerator(stream);
const logger = createLogger({}, stream);
const msg = 'foo';
const clsValues = {
dummy: 'value',
another: 'another value',
};
logger.cls.run(() => {
logger.cls.set('dummy', clsValues.dummy);
logger.cls.set('another', clsValues.another);
process.nextTick(async () => {
logger.info(msg);
const entry = await gen.next().value;
t.is(entry.dummy, clsValues.dummy);
t.is(entry.another, clsValues.another);
t.end();
});
});
});
test.cb(
`Should properly log message with both cls and local context,
And local context should have precedence over cls context`,
t => {
const stream = parseJSONStream();
const gen = streamToGenerator(stream);
const logger = createLogger({}, stream);
const msg = 'foo';
const clsValues = {
dummy: 'value',
precedence: 'will be overwitten',
};
logger.cls.run(() => {
logger.cls.set('dummy', clsValues.dummy);
logger.cls.set('another', clsValues.another);
process.nextTick(async () => {
const localValues = { precedence: 'local' };
logger.info(localValues, msg);
const entry = await gen.next().value;
t.is(entry.precedence, localValues.precedence);
t.end();
});
});
}
);
test.cb(`Should properly propagate cls context to child logger`, t => {
const stream = parseJSONStream();
const gen = streamToGenerator(stream);
const logger = createLogger({}, stream);
const msg = 'foo';
const clsValues = {
dummy: 'value',
another: 'another value',
};
const childContext = { foo: 'bar' };
const child = logger.child(childContext);
logger.cls.run(() => {
logger.cls.set('dummy', clsValues.dummy);
logger.cls.set('another', clsValues.another);
process.nextTick(async () => {
child.info(msg);
const entry = await gen.next().value;
t.is(entry.dummy, clsValues.dummy);
t.is(entry.another, clsValues.another);
t.is(entry.foo, childContext.foo);
t.end();
});
});
});
test.cb(`Should properly propagate cls context to grand child logger`, t => {
const stream = parseJSONStream();
const gen = streamToGenerator(stream);
const logger = createLogger({}, stream);
const msg = 'foo';
const clsValues = {
dummy: 'value',
another: 'another value',
};
const childContext = { foo: 'bar' };
const child = logger.child(childContext);
const grandChildContext = { fizz: 'buzz' };
const grandChild = child.child(grandChildContext);
logger.cls.run(() => {
logger.cls.set('dummy', clsValues.dummy);
logger.cls.set('another', clsValues.another);
process.nextTick(async () => {
grandChild.info(msg);
const entry = await gen.next().value;
t.is(entry.dummy, clsValues.dummy);
t.is(entry.another, clsValues.another);
t.is(entry.foo, childContext.foo);
t.is(entry.fizz, grandChildContext.fizz);
t.end();
});
});
});