-
Notifications
You must be signed in to change notification settings - Fork 776
/
layouts-test.js
434 lines (374 loc) · 14.3 KB
/
layouts-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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
'use strict';
const test = require('tap').test;
const os = require('os');
const semver = require('semver');
const EOL = os.EOL || '\n';
// used for patternLayout tests.
function testPattern(assert, layout, event, tokens, pattern, value) {
assert.equal(layout(pattern, tokens)(event), value);
}
test('log4js layouts', (batch) => {
batch.test('colouredLayout', (t) => {
const layout = require('../../lib/layouts').colouredLayout;
t.test('should apply level colour codes to output', (assert) => {
const output = layout({
data: ['nonsense'],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'cheese',
level: {
toString: function () {
return 'ERROR';
}
}
});
assert.equal(
output,
'\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mnonsense'
);
assert.end();
});
t.test('should support the console.log format for the message', (assert) => {
const output = layout({
data: ['thing %d', 2],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'cheese',
level: {
toString: function () {
return 'ERROR';
}
}
});
assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mthing 2');
assert.end();
});
t.end();
});
batch.test('messagePassThroughLayout', (t) => {
const layout = require('../../lib/layouts').messagePassThroughLayout;
t.equal(layout({
data: ['nonsense'],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'cheese',
level: {
colour: 'green',
toString: function () {
return 'ERROR';
}
}
}), 'nonsense', 'should take a logevent and output only the message');
t.equal(layout({
data: ['thing %d', 1, 'cheese'],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'cheese',
level: {
colour: 'green',
toString: function () {
return 'ERROR';
}
}
}), 'thing 1 cheese', 'should support the console.log format for the message');
t.equal(layout({
data: [{ thing: 1 }],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'cheese',
level: {
colour: 'green',
toString: function () {
return 'ERROR';
}
}
}), '{ thing: 1 }', 'should output the first item even if it is not a string');
t.match(
layout({
data: [new Error()],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'cheese',
level: {
colour: 'green',
toString: function () {
return 'ERROR';
}
}
}),
/at Object\.<anonymous>\s+\((.*)test[\\/]tap[\\/]layouts-test\.js:\d+:\d+\)/,
'regexp did not return a match - should print the stacks of a passed error objects'
);
t.test('with passed augmented errors', (assert) => {
const e = new Error('My Unique Error Message');
e.augmented = 'My Unique attribute value';
e.augObj = { at1: 'at2' };
const layoutOutput = layout({
data: [e],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'cheese',
level: {
colour: 'green',
toString: function () {
return 'ERROR';
}
}
});
assert.match(layoutOutput, /Error: My Unique Error Message/, 'should print the contained error message');
assert.match(
layoutOutput,
/augmented:\s'My Unique attribute value'/,
'should print error augmented string attributes'
);
assert.match(layoutOutput, /augObj:\s\{ at1: 'at2' \}/, 'should print error augmented object attributes');
assert.end();
});
t.end();
});
batch.test('basicLayout', (t) => {
const layout = require('../../lib/layouts').basicLayout;
const event = {
data: ['this is a test'],
startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'tests',
level: {
toString: function () {
return 'DEBUG';
}
}
};
t.equal(layout(event), '[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test');
t.test('should output a stacktrace, message if the event has an error attached', (assert) => {
let i;
const error = new Error('Some made-up error');
const stack = error.stack.split(/\n/);
event.data = ['this is a test', error];
const output = layout(event);
const lines = output.split(/\n/);
if (semver.satisfies(process.version, '>=6')) {
assert.equal(lines.length, stack.length);
assert.equal(
lines[0],
'[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test Error: Some made-up error'
);
for (i = 1; i < stack.length; i++) {
assert.equal(lines[i], stack[i]);
}
} else {
assert.equal(lines.length - 1, stack.length);
assert.equal(
lines[0],
'[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]'
);
for (i = 1; i < stack.length; i++) {
assert.equal(lines[i + 2], stack[i + 1]);
}
}
assert.end();
});
t.test('should output any extra data in the log event as util.inspect strings', (assert) => {
event.data = [
'this is a test', {
name: 'Cheese',
message: 'Gorgonzola smells.'
}
];
const output = layout(event);
assert.equal(
output,
'[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test ' +
"{ name: 'Cheese', message: 'Gorgonzola smells.' }"
);
assert.end();
});
t.end();
});
batch.test('patternLayout', (t) => {
const tokens = {
testString: 'testStringToken',
testFunction: function () {
return 'testFunctionToken';
},
fnThatUsesLogEvent: function (logEvent) {
return logEvent.level.toString();
}
};
const event = {
data: ['this is a test'],
startTime: new Date('2010-12-05T14:18:30.045Z'), // new Date(2010, 11, 5, 14, 18, 30, 45),
categoryName: 'multiple.levels.of.tests',
level: {
toString: function () {
return 'DEBUG';
}
},
context: tokens
};
const layout = require('../../lib/layouts').patternLayout;
// override getTimezoneOffset
event.startTime.getTimezoneOffset = function () {
return 0;
};
t.test('should default to "time logLevel loggerName - message"', (assert) => {
testPattern(assert, layout, event, tokens, null, `14:18:30 DEBUG multiple.levels.of.tests - this is a test${EOL}`);
assert.end();
});
t.test('%r should output time only', (assert) => {
testPattern(assert, layout, event, tokens, '%r', '14:18:30');
assert.end();
});
t.test('%p should output the log level', (assert) => {
testPattern(assert, layout, event, tokens, '%p', 'DEBUG');
assert.end();
});
t.test('%c should output the log category', (assert) => {
testPattern(assert, layout, event, tokens, '%c', 'multiple.levels.of.tests');
assert.end();
});
t.test('%m should output the log data', (assert) => {
testPattern(assert, layout, event, tokens, '%m', 'this is a test');
assert.end();
});
t.test('%n should output a new line', (assert) => {
testPattern(assert, layout, event, tokens, '%n', EOL);
assert.end();
});
t.test('%h should output hostname', (assert) => {
testPattern(assert, layout, event, tokens, '%h', os.hostname().toString());
assert.end();
});
t.test('%z should output pid', (assert) => {
testPattern(assert, layout, event, tokens, '%z', process.pid.toString());
assert.end();
});
t.test('%c should handle category names like java-style package names', (assert) => {
testPattern(assert, layout, event, tokens, '%c{1}', 'tests');
testPattern(assert, layout, event, tokens, '%c{2}', 'of.tests');
testPattern(assert, layout, event, tokens, '%c{3}', 'levels.of.tests');
testPattern(assert, layout, event, tokens, '%c{4}', 'multiple.levels.of.tests');
testPattern(assert, layout, event, tokens, '%c{5}', 'multiple.levels.of.tests');
testPattern(assert, layout, event, tokens, '%c{99}', 'multiple.levels.of.tests');
assert.end();
});
t.test('%d should output the date in ISO8601 format', (assert) => {
testPattern(assert, layout, event, tokens, '%d', '2010-12-05 14:18:30.045');
assert.end();
});
t.test('%d should allow for format specification', (assert) => {
testPattern(assert, layout, event, tokens, '%d{ISO8601_WITH_TZ_OFFSET}', '2010-12-05T14:18:30.045-0000');
testPattern(assert, layout, event, tokens, '%d{ISO8601}', '2010-12-05 14:18:30.045');
testPattern(assert, layout, event, tokens, '%d{ABSOLUTE}', '14:18:30.045');
testPattern(assert, layout, event, tokens, '%d{DATE}', '05 12 2010 14:18:30.045');
testPattern(assert, layout, event, tokens, '%d{yy MM dd hh mm ss}', '10 12 05 14 18 30');
testPattern(assert, layout, event, tokens, '%d{yyyy MM dd}', '2010 12 05');
testPattern(assert, layout, event, tokens, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045');
assert.end();
});
t.test('%% should output %', (assert) => {
testPattern(assert, layout, event, tokens, '%%', '%');
assert.end();
});
t.test('should output anything not preceded by % as literal', (assert) => {
testPattern(assert, layout, event, tokens, 'blah blah blah', 'blah blah blah');
assert.end();
});
t.test('should output the original string if no replacer matches the token', (assert) => {
testPattern(assert, layout, event, tokens, '%a{3}', 'a{3}');
assert.end();
});
t.test('should handle complicated patterns', (assert) => {
testPattern(assert, layout, event, tokens,
'%m%n %c{2} at %d{ABSOLUTE} cheese %p%n',
`this is a test${EOL} of.tests at 14:18:30.045 cheese DEBUG${EOL}`
);
assert.end();
});
t.test('should truncate fields if specified', (assert) => {
testPattern(assert, layout, event, tokens, '%.4m', 'this');
testPattern(assert, layout, event, tokens, '%.7m', 'this is');
testPattern(assert, layout, event, tokens, '%.9m', 'this is a');
testPattern(assert, layout, event, tokens, '%.14m', 'this is a test');
testPattern(assert, layout, event, tokens, '%.2919102m', 'this is a test');
assert.end();
});
t.test('should pad fields if specified', (assert) => {
testPattern(assert, layout, event, tokens, '%10p', ' DEBUG');
testPattern(assert, layout, event, tokens, '%8p', ' DEBUG');
testPattern(assert, layout, event, tokens, '%6p', ' DEBUG');
testPattern(assert, layout, event, tokens, '%4p', 'DEBUG');
testPattern(assert, layout, event, tokens, '%-4p', 'DEBUG');
testPattern(assert, layout, event, tokens, '%-6p', 'DEBUG ');
testPattern(assert, layout, event, tokens, '%-8p', 'DEBUG ');
testPattern(assert, layout, event, tokens, '%-10p', 'DEBUG ');
assert.end();
});
t.test('%[%r%] should output colored time', (assert) => {
testPattern(assert, layout, event, tokens, '%[%r%]', '\x1B[36m14:18:30\x1B[39m');
assert.end();
});
t.test('%x{testString} should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, tokens, '%x{testString}', 'testStringToken');
assert.end();
});
t.test('%x{testFunction} should output the result of the function stored in tokens', (assert) => {
testPattern(assert, layout, event, tokens, '%x{testFunction}', 'testFunctionToken');
assert.end();
});
t.test('%x{doesNotExist} should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, tokens, '%x{doesNotExist}', 'null');
assert.end();
});
t.test('%x{fnThatUsesLogEvent} should be able to use the logEvent', (assert) => {
testPattern(assert, layout, event, tokens, '%x{fnThatUsesLogEvent}', 'DEBUG');
assert.end();
});
t.test('%x should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, tokens, '%x', 'null');
assert.end();
});
t.test('%X{testString} should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X{testString}', 'testStringToken');
assert.end();
});
t.test('%X{testFunction} should output the result of the function stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X{testFunction}', 'testFunctionToken');
assert.end();
});
t.test('%X{doesNotExist} should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X{doesNotExist}', 'null');
assert.end();
});
t.test('%X{fnThatUsesLogEvent} should be able to use the logEvent', (assert) => {
testPattern(assert, layout, event, {}, '%X{fnThatUsesLogEvent}', 'DEBUG');
assert.end();
});
t.test('%X should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X', 'null');
assert.end();
});
t.end();
});
batch.test('layout makers', (t) => {
const layouts = require('../../lib/layouts');
t.test('should have a maker for each layout', (assert) => {
assert.ok(layouts.layout('messagePassThrough'));
assert.ok(layouts.layout('basic'));
assert.ok(layouts.layout('colored'));
assert.ok(layouts.layout('coloured'));
assert.ok(layouts.layout('pattern'));
assert.end();
});
t.end();
});
batch.test('add layout', (t) => {
const layouts = require('../../lib/layouts');
t.test('should be able to add a layout', (assert) => {
layouts.addLayout('test_layout', (config) => {
assert.equal(config, 'test_config');
return function (logEvent) {
return `TEST LAYOUT >${logEvent.data}`;
};
});
const serializer = layouts.layout('test_layout', 'test_config');
assert.ok(serializer);
assert.equal(serializer({ data: 'INPUT' }), 'TEST LAYOUT >INPUT');
assert.end();
});
t.end();
});
batch.end();
});