@villedemontreal/logger 6.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +164 -0
- package/dist/src/config/configs.d.ts +118 -0
- package/dist/src/config/configs.js +167 -0
- package/dist/src/config/configs.js.map +1 -0
- package/dist/src/config/constants.d.ts +60 -0
- package/dist/src/config/constants.js +60 -0
- package/dist/src/config/constants.js.map +1 -0
- package/dist/src/consoleStream.d.ts +8 -0
- package/dist/src/consoleStream.js +29 -0
- package/dist/src/consoleStream.js.map +1 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +16 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lazyLogger.d.ts +21 -0
- package/dist/src/lazyLogger.js +46 -0
- package/dist/src/lazyLogger.js.map +1 -0
- package/dist/src/logger.d.ts +169 -0
- package/dist/src/logger.js +501 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/logger.test.d.ts +8 -0
- package/dist/src/logger.test.js +564 -0
- package/dist/src/logger.test.js.map +1 -0
- package/package.json +67 -0
- package/src/config/configs.ts +195 -0
- package/src/config/constants.ts +78 -0
- package/src/consoleStream.ts +29 -0
- package/src/index.ts +3 -0
- package/src/lazyLogger.ts +50 -0
- package/src/logger.test.ts +673 -0
- package/src/logger.ts +551 -0
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
import { utils } from '@villedemontreal/general-utils';
|
|
2
|
+
import { assert } from 'chai';
|
|
3
|
+
import * as fs from 'fs-extra';
|
|
4
|
+
import * as _ from 'lodash';
|
|
5
|
+
import { LoggerConfigs } from './config/configs';
|
|
6
|
+
import { constants } from './config/constants';
|
|
7
|
+
import { LazyLogger } from './lazyLogger';
|
|
8
|
+
import {
|
|
9
|
+
convertLogLevelToPinoNumberLevel,
|
|
10
|
+
createLogger,
|
|
11
|
+
ILogger,
|
|
12
|
+
initLogger,
|
|
13
|
+
Logger,
|
|
14
|
+
LogLevel,
|
|
15
|
+
setGlobalLogLevel
|
|
16
|
+
} from './logger';
|
|
17
|
+
|
|
18
|
+
const TESTING_CID = 'test-cid';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The "--no-timeouts" arg doesn't work to disable
|
|
22
|
+
* the Mocha timeouts (while debugging) if a timeout
|
|
23
|
+
* is specified in the code itself. Using this to set the
|
|
24
|
+
* timeouts does.
|
|
25
|
+
*/
|
|
26
|
+
export function timeout(mocha: Mocha.Suite | Mocha.Context, milliSec: number) {
|
|
27
|
+
mocha.timeout(process.argv.includes('--no-timeouts') ? 0 : milliSec);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ==========================================
|
|
31
|
+
// Logger tests
|
|
32
|
+
// ==========================================
|
|
33
|
+
describe('Logger tests', () => {
|
|
34
|
+
describe('General tests', () => {
|
|
35
|
+
let loggerConfig: LoggerConfigs;
|
|
36
|
+
let logger: ILogger;
|
|
37
|
+
const stdoutWriteBackup = process.stdout.write;
|
|
38
|
+
let output: string;
|
|
39
|
+
|
|
40
|
+
before(async () => {
|
|
41
|
+
// ==========================================
|
|
42
|
+
// Tweaks the configs for this tests file.
|
|
43
|
+
// ==========================================
|
|
44
|
+
loggerConfig = new LoggerConfigs(() => TESTING_CID);
|
|
45
|
+
loggerConfig.setLogHumanReadableinConsole(false);
|
|
46
|
+
loggerConfig.setLogSource(true);
|
|
47
|
+
loggerConfig.setLogLevel(LogLevel.INFO);
|
|
48
|
+
|
|
49
|
+
// ==========================================
|
|
50
|
+
// Creates the logger
|
|
51
|
+
// ==========================================
|
|
52
|
+
initLogger(loggerConfig, 'default', true);
|
|
53
|
+
logger = new Logger('test');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
let expectTwoCalls = false;
|
|
57
|
+
let keepSecondCallOnly = false;
|
|
58
|
+
let firstCallDone = false;
|
|
59
|
+
|
|
60
|
+
function restartCustomWriter() {
|
|
61
|
+
output = '';
|
|
62
|
+
expectTwoCalls = false;
|
|
63
|
+
keepSecondCallOnly = false;
|
|
64
|
+
firstCallDone = false;
|
|
65
|
+
|
|
66
|
+
// A stadard "function" is required here, because
|
|
67
|
+
// of the use of "arguments".
|
|
68
|
+
// tslint:disable-next-line:only-arrow-functions
|
|
69
|
+
process.stdout.write = function() {
|
|
70
|
+
if (!expectTwoCalls) {
|
|
71
|
+
output += arguments[0];
|
|
72
|
+
process.stdout.write = stdoutWriteBackup;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!firstCallDone) {
|
|
77
|
+
if (!keepSecondCallOnly) {
|
|
78
|
+
output += arguments[0];
|
|
79
|
+
}
|
|
80
|
+
firstCallDone = true;
|
|
81
|
+
} else {
|
|
82
|
+
output += arguments[0];
|
|
83
|
+
process.stdout.write = stdoutWriteBackup;
|
|
84
|
+
}
|
|
85
|
+
} as any;
|
|
86
|
+
|
|
87
|
+
assert.isTrue(utils.isBlank(output));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
beforeEach(async () => {
|
|
91
|
+
restartCustomWriter();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('string message', async () => {
|
|
95
|
+
logger.error('allo');
|
|
96
|
+
assert.isTrue(!utils.isBlank(output));
|
|
97
|
+
|
|
98
|
+
const jsonObj = JSON.parse(output);
|
|
99
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
100
|
+
assert.strictEqual(jsonObj.name, 'test');
|
|
101
|
+
assert.strictEqual(jsonObj.msg, 'allo');
|
|
102
|
+
assert.isNotNull(jsonObj.src);
|
|
103
|
+
assert.isNotNull(jsonObj.src.file);
|
|
104
|
+
assert.isNotNull(jsonObj.src.line);
|
|
105
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
106
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('string message and extra text message', async () => {
|
|
110
|
+
logger.error('allo', 'salut');
|
|
111
|
+
assert.isTrue(!utils.isBlank(output));
|
|
112
|
+
|
|
113
|
+
const jsonObj = JSON.parse(output);
|
|
114
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
115
|
+
assert.strictEqual(jsonObj.msg, 'allo - salut');
|
|
116
|
+
assert.isNotNull(jsonObj.src);
|
|
117
|
+
assert.isNotNull(jsonObj.src.file);
|
|
118
|
+
assert.isNotNull(jsonObj.src.line);
|
|
119
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
120
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('custom object message', async () => {
|
|
124
|
+
logger.error({
|
|
125
|
+
key1: {
|
|
126
|
+
key3: 'val3',
|
|
127
|
+
key4: 'val4'
|
|
128
|
+
},
|
|
129
|
+
key2: 'val2',
|
|
130
|
+
msg: 'blabla'
|
|
131
|
+
});
|
|
132
|
+
assert.isTrue(!utils.isBlank(output));
|
|
133
|
+
|
|
134
|
+
const jsonObj = JSON.parse(output);
|
|
135
|
+
assert.strictEqual(jsonObj.key2, 'val2');
|
|
136
|
+
assert.strictEqual(jsonObj.msg, 'blabla');
|
|
137
|
+
assert.deepEqual(jsonObj.key1, {
|
|
138
|
+
key3: 'val3',
|
|
139
|
+
key4: 'val4'
|
|
140
|
+
});
|
|
141
|
+
assert.isNotNull(jsonObj.src);
|
|
142
|
+
assert.isNotNull(jsonObj.src.file);
|
|
143
|
+
assert.isNotNull(jsonObj.src.line);
|
|
144
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
145
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('custom object message and extra text message', async () => {
|
|
149
|
+
logger.error(
|
|
150
|
+
{
|
|
151
|
+
key1: {
|
|
152
|
+
key3: 'val3',
|
|
153
|
+
key4: 'val4'
|
|
154
|
+
},
|
|
155
|
+
key2: 'val2',
|
|
156
|
+
msg: 'blabla'
|
|
157
|
+
},
|
|
158
|
+
'my text message'
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
assert.isTrue(!utils.isBlank(output));
|
|
162
|
+
const jsonObj = JSON.parse(output);
|
|
163
|
+
assert.strictEqual(jsonObj.key2, 'val2');
|
|
164
|
+
assert.strictEqual(jsonObj.msg, 'blabla - my text message');
|
|
165
|
+
assert.deepEqual(jsonObj.key1, {
|
|
166
|
+
key3: 'val3',
|
|
167
|
+
key4: 'val4'
|
|
168
|
+
});
|
|
169
|
+
assert.isNotNull(jsonObj.src);
|
|
170
|
+
assert.isNotNull(jsonObj.src.file);
|
|
171
|
+
assert.isNotNull(jsonObj.src.line);
|
|
172
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
173
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('regular Error object and extra text message', async () => {
|
|
177
|
+
logger.error(new Error('my error message'), 'my text message');
|
|
178
|
+
assert.isTrue(!utils.isBlank(output));
|
|
179
|
+
|
|
180
|
+
const jsonObj = JSON.parse(output);
|
|
181
|
+
assert.strictEqual(jsonObj.msg, 'my error message - my text message');
|
|
182
|
+
assert.isTrue(!utils.isBlank(jsonObj.stack));
|
|
183
|
+
assert.isTrue(!utils.isBlank(jsonObj.name));
|
|
184
|
+
assert.isNotNull(jsonObj.src);
|
|
185
|
+
assert.isNotNull(jsonObj.src.file);
|
|
186
|
+
assert.isNotNull(jsonObj.src.line);
|
|
187
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
188
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('null Error object and extra text message', async () => {
|
|
192
|
+
logger.error(null, 'my text message');
|
|
193
|
+
assert.isTrue(!utils.isBlank(output));
|
|
194
|
+
|
|
195
|
+
const jsonObj = JSON.parse(output);
|
|
196
|
+
assert.strictEqual(jsonObj.msg, 'my text message');
|
|
197
|
+
assert.isTrue(!utils.isBlank(jsonObj.name));
|
|
198
|
+
assert.isNotNull(jsonObj.src);
|
|
199
|
+
assert.isNotNull(jsonObj.src.file);
|
|
200
|
+
assert.isNotNull(jsonObj.src.line);
|
|
201
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
202
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('undefined Error object and extra text message', async () => {
|
|
206
|
+
logger.error(undefined, 'my text message');
|
|
207
|
+
assert.isTrue(!utils.isBlank(output));
|
|
208
|
+
|
|
209
|
+
const jsonObj = JSON.parse(output);
|
|
210
|
+
assert.strictEqual(jsonObj.msg, 'my text message');
|
|
211
|
+
assert.isTrue(!utils.isBlank(jsonObj.name));
|
|
212
|
+
assert.isNotNull(jsonObj.src);
|
|
213
|
+
assert.isNotNull(jsonObj.src.file);
|
|
214
|
+
assert.isNotNull(jsonObj.src.line);
|
|
215
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
216
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('array message', async () => {
|
|
220
|
+
// ==========================================
|
|
221
|
+
// Two calls to "process.stdout.write" will
|
|
222
|
+
// be made because an error for the invalid
|
|
223
|
+
// array message will be logged!
|
|
224
|
+
// ==========================================
|
|
225
|
+
expectTwoCalls = true;
|
|
226
|
+
keepSecondCallOnly = true;
|
|
227
|
+
|
|
228
|
+
logger.error([
|
|
229
|
+
'toto',
|
|
230
|
+
{
|
|
231
|
+
key1: 'val1',
|
|
232
|
+
key2: 'val2'
|
|
233
|
+
}
|
|
234
|
+
]);
|
|
235
|
+
|
|
236
|
+
const jsonObj = JSON.parse(output);
|
|
237
|
+
assert.deepEqual(jsonObj._arrayMsg, [
|
|
238
|
+
'toto',
|
|
239
|
+
{
|
|
240
|
+
key1: 'val1',
|
|
241
|
+
key2: 'val2'
|
|
242
|
+
}
|
|
243
|
+
]);
|
|
244
|
+
assert.isNotNull(jsonObj.src);
|
|
245
|
+
assert.isNotNull(jsonObj.src.file);
|
|
246
|
+
assert.isNotNull(jsonObj.src.line);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('array message and extra text message', async () => {
|
|
250
|
+
// ==========================================
|
|
251
|
+
// Two calls to "process.stdout.write" will
|
|
252
|
+
// be made because an error for the invalid
|
|
253
|
+
// array message will be logged!
|
|
254
|
+
// ==========================================
|
|
255
|
+
expectTwoCalls = true;
|
|
256
|
+
keepSecondCallOnly = true;
|
|
257
|
+
|
|
258
|
+
logger.error(
|
|
259
|
+
[
|
|
260
|
+
'toto',
|
|
261
|
+
{
|
|
262
|
+
key1: 'val1',
|
|
263
|
+
key2: 'val2'
|
|
264
|
+
}
|
|
265
|
+
],
|
|
266
|
+
'my text message'
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
const jsonObj = JSON.parse(output);
|
|
270
|
+
assert.deepEqual(jsonObj._arrayMsg, [
|
|
271
|
+
'toto',
|
|
272
|
+
{
|
|
273
|
+
key1: 'val1',
|
|
274
|
+
key2: 'val2'
|
|
275
|
+
}
|
|
276
|
+
]);
|
|
277
|
+
assert.strictEqual(jsonObj.msg, 'my text message');
|
|
278
|
+
assert.isNotNull(jsonObj.src);
|
|
279
|
+
assert.isNotNull(jsonObj.src.file);
|
|
280
|
+
assert.isNotNull(jsonObj.src.line);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('log level - debug', async () => {
|
|
284
|
+
logger.debug('allo');
|
|
285
|
+
assert.strictEqual(output, '');
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('log level - info', async () => {
|
|
289
|
+
logger.info('allo');
|
|
290
|
+
const jsonObj = JSON.parse(output);
|
|
291
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.INFO));
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('log level - warning', async () => {
|
|
295
|
+
logger.warning('allo');
|
|
296
|
+
const jsonObj = JSON.parse(output);
|
|
297
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.WARNING));
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('log level - error', async () => {
|
|
301
|
+
logger.error('allo');
|
|
302
|
+
const jsonObj = JSON.parse(output);
|
|
303
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.ERROR));
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('log level - custom valid', async () => {
|
|
307
|
+
logger.log(LogLevel.INFO, 'allo');
|
|
308
|
+
const jsonObj = JSON.parse(output);
|
|
309
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.INFO));
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('log level - custom invalid', async () => {
|
|
313
|
+
// ==========================================
|
|
314
|
+
// Two calls to "process.stdout.write" will
|
|
315
|
+
// be made because an error for the invalid
|
|
316
|
+
// level will be logged!
|
|
317
|
+
// ==========================================
|
|
318
|
+
expectTwoCalls = true;
|
|
319
|
+
keepSecondCallOnly = true;
|
|
320
|
+
|
|
321
|
+
logger.log('nope' as any, 'allo');
|
|
322
|
+
const jsonObj = JSON.parse(output);
|
|
323
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.ERROR));
|
|
324
|
+
assert.strictEqual(jsonObj.msg, 'allo');
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('newline after each log - string', async () => {
|
|
328
|
+
expectTwoCalls = true;
|
|
329
|
+
|
|
330
|
+
logger.error('111');
|
|
331
|
+
logger.error('222');
|
|
332
|
+
|
|
333
|
+
const pos = output.indexOf('\n');
|
|
334
|
+
assert.isTrue(pos > -1);
|
|
335
|
+
assert.strictEqual(output.charAt(pos + 1), '{');
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it('newline after each log - complexe objects', async () => {
|
|
339
|
+
expectTwoCalls = true;
|
|
340
|
+
|
|
341
|
+
logger.error({
|
|
342
|
+
key1: 'val1',
|
|
343
|
+
key2: 'val2'
|
|
344
|
+
});
|
|
345
|
+
logger.error({
|
|
346
|
+
key1: 'val1',
|
|
347
|
+
key2: 'val2'
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const pos = output.indexOf('\n');
|
|
351
|
+
assert.isTrue(pos > -1);
|
|
352
|
+
assert.strictEqual(output.charAt(pos + 1), '{');
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it('date message', async () => {
|
|
356
|
+
const someDate = new Date();
|
|
357
|
+
logger.error(someDate);
|
|
358
|
+
assert.isTrue(!utils.isBlank(output));
|
|
359
|
+
|
|
360
|
+
const jsonObj = JSON.parse(output);
|
|
361
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
362
|
+
assert.strictEqual(jsonObj.msg, someDate.toString());
|
|
363
|
+
assert.isTrue(_.isDate(someDate));
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// ==========================================
|
|
367
|
+
// Error controller log messages
|
|
368
|
+
// ==========================================
|
|
369
|
+
describe('Error controller log messages', () => {
|
|
370
|
+
it('app and version properties', async () => {
|
|
371
|
+
const packageJson = require(`${constants.libRoot}/package.json`);
|
|
372
|
+
const appName = packageJson.name;
|
|
373
|
+
const appVersion = packageJson.version;
|
|
374
|
+
|
|
375
|
+
logger.error('allo');
|
|
376
|
+
assert.isTrue(!utils.isBlank(output));
|
|
377
|
+
|
|
378
|
+
const jsonObj = JSON.parse(output);
|
|
379
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
380
|
+
assert.strictEqual(jsonObj[constants.logging.properties.APP_NAME], appName);
|
|
381
|
+
assert.strictEqual(jsonObj[constants.logging.properties.APP_VERSION], appVersion);
|
|
382
|
+
assert.strictEqual(jsonObj[constants.logging.properties.CORRELATION_ID], TESTING_CID);
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
// ==========================================
|
|
387
|
+
// LazyLogger tests
|
|
388
|
+
// ==========================================
|
|
389
|
+
describe('LazyLogger tests', () => {
|
|
390
|
+
let lazyLogger: LazyLogger;
|
|
391
|
+
|
|
392
|
+
beforeEach(async () => {
|
|
393
|
+
lazyLogger = new LazyLogger(
|
|
394
|
+
'titi',
|
|
395
|
+
(name: string): ILogger => {
|
|
396
|
+
const logger2 = new Logger('titi');
|
|
397
|
+
return logger2;
|
|
398
|
+
}
|
|
399
|
+
);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it('debug', async () => {
|
|
403
|
+
lazyLogger.debug('allo');
|
|
404
|
+
assert.isTrue(utils.isBlank(output));
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('info', async () => {
|
|
408
|
+
lazyLogger.info('allo');
|
|
409
|
+
|
|
410
|
+
assert.isTrue(!utils.isBlank(output));
|
|
411
|
+
const jsonObj = JSON.parse(output);
|
|
412
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
413
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.INFO));
|
|
414
|
+
assert.strictEqual(jsonObj.name, 'titi');
|
|
415
|
+
assert.strictEqual(jsonObj.msg, 'allo');
|
|
416
|
+
assert.isNotNull(jsonObj.src);
|
|
417
|
+
assert.isNotNull(jsonObj.src.file);
|
|
418
|
+
assert.isNotNull(jsonObj.src.line);
|
|
419
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
420
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
421
|
+
assert.strictEqual(jsonObj[constants.logging.properties.CORRELATION_ID], TESTING_CID);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it('warning', async () => {
|
|
425
|
+
lazyLogger.warning('allo');
|
|
426
|
+
|
|
427
|
+
assert.isTrue(!utils.isBlank(output));
|
|
428
|
+
const jsonObj = JSON.parse(output);
|
|
429
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
430
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.WARNING));
|
|
431
|
+
assert.strictEqual(jsonObj.name, 'titi');
|
|
432
|
+
assert.strictEqual(jsonObj.msg, 'allo');
|
|
433
|
+
assert.isNotNull(jsonObj.src);
|
|
434
|
+
assert.isNotNull(jsonObj.src.file);
|
|
435
|
+
assert.isNotNull(jsonObj.src.line);
|
|
436
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
437
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
438
|
+
assert.strictEqual(jsonObj[constants.logging.properties.CORRELATION_ID], TESTING_CID);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('error', async () => {
|
|
442
|
+
lazyLogger.error('allo');
|
|
443
|
+
|
|
444
|
+
assert.isTrue(!utils.isBlank(output));
|
|
445
|
+
const jsonObj = JSON.parse(output);
|
|
446
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
447
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.ERROR));
|
|
448
|
+
assert.strictEqual(jsonObj.name, 'titi');
|
|
449
|
+
assert.strictEqual(jsonObj.msg, 'allo');
|
|
450
|
+
assert.isNotNull(jsonObj.src);
|
|
451
|
+
assert.isNotNull(jsonObj.src.file);
|
|
452
|
+
assert.isNotNull(jsonObj.src.line);
|
|
453
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
454
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
455
|
+
assert.strictEqual(jsonObj[constants.logging.properties.CORRELATION_ID], TESTING_CID);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it('log', async () => {
|
|
459
|
+
lazyLogger.log(LogLevel.INFO, 'allo');
|
|
460
|
+
|
|
461
|
+
assert.isTrue(!utils.isBlank(output));
|
|
462
|
+
const jsonObj = JSON.parse(output);
|
|
463
|
+
assert.isTrue(_.isObject(jsonObj));
|
|
464
|
+
assert.strictEqual(jsonObj.level, convertLogLevelToPinoNumberLevel(LogLevel.INFO));
|
|
465
|
+
assert.strictEqual(jsonObj.name, 'titi');
|
|
466
|
+
assert.strictEqual(jsonObj.msg, 'allo');
|
|
467
|
+
assert.isNotNull(jsonObj.src);
|
|
468
|
+
assert.isNotNull(jsonObj.src.file);
|
|
469
|
+
assert.isNotNull(jsonObj.src.line);
|
|
470
|
+
assert.strictEqual(jsonObj.logType, constants.logging.logType.MONTREAL);
|
|
471
|
+
assert.strictEqual(jsonObj.logTypeVersion, '2');
|
|
472
|
+
assert.strictEqual(jsonObj[constants.logging.properties.CORRELATION_ID], TESTING_CID);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it('logger creator is required', async () => {
|
|
476
|
+
try {
|
|
477
|
+
const lazyLogger2 = new LazyLogger('titi', null);
|
|
478
|
+
assert.fail();
|
|
479
|
+
assert.isNotOk(lazyLogger2);
|
|
480
|
+
} catch (err) {
|
|
481
|
+
/* ok */
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
try {
|
|
485
|
+
const lazyLogger3 = new LazyLogger('titi', undefined);
|
|
486
|
+
assert.fail();
|
|
487
|
+
assert.isNotOk(lazyLogger3);
|
|
488
|
+
} catch (err) {
|
|
489
|
+
/* ok */
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
// ==========================================
|
|
495
|
+
// Global log level
|
|
496
|
+
// ==========================================
|
|
497
|
+
describe('Global log level', () => {
|
|
498
|
+
let childLogger: ILogger;
|
|
499
|
+
before(() => {
|
|
500
|
+
childLogger = createLogger('testing child logger');
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
afterEach(() => {
|
|
504
|
+
// Reset the default logging
|
|
505
|
+
setGlobalLogLevel(loggerConfig.getLogLevel());
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it('log debug message when global log level set to DEBUG', () => {
|
|
509
|
+
setGlobalLogLevel(LogLevel.DEBUG);
|
|
510
|
+
|
|
511
|
+
logger.debug('this is the debug message');
|
|
512
|
+
let jsonObj = JSON.parse(output);
|
|
513
|
+
assert.strictEqual(jsonObj.msg, 'this is the debug message');
|
|
514
|
+
|
|
515
|
+
restartCustomWriter();
|
|
516
|
+
childLogger.debug('this is the child logger debug message');
|
|
517
|
+
jsonObj = JSON.parse(output);
|
|
518
|
+
assert.strictEqual(jsonObj.msg, 'this is the child logger debug message');
|
|
519
|
+
|
|
520
|
+
restartCustomWriter();
|
|
521
|
+
logger.info('this is the info message');
|
|
522
|
+
jsonObj = JSON.parse(output);
|
|
523
|
+
assert.strictEqual(jsonObj.msg, 'this is the info message');
|
|
524
|
+
|
|
525
|
+
restartCustomWriter();
|
|
526
|
+
childLogger.info('this is the child logger info message');
|
|
527
|
+
jsonObj = JSON.parse(output);
|
|
528
|
+
assert.strictEqual(jsonObj.msg, 'this is the child logger info message');
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it('filter debug message when global log level set to WARNING', () => {
|
|
532
|
+
assert.isTrue(utils.isBlank(output));
|
|
533
|
+
|
|
534
|
+
setGlobalLogLevel(LogLevel.WARNING);
|
|
535
|
+
|
|
536
|
+
logger.debug('this is my filtered the debug message');
|
|
537
|
+
assert.isTrue(utils.isBlank(output), 'Did not filter debug message as expected.');
|
|
538
|
+
|
|
539
|
+
childLogger.debug('this is my filtered the debug message');
|
|
540
|
+
assert.isTrue(utils.isBlank(output), 'Did not filter debug message as expected.');
|
|
541
|
+
|
|
542
|
+
logger.info('this is my filtered the info message');
|
|
543
|
+
assert.isTrue(utils.isBlank(output), 'Did not filter info message as expected.');
|
|
544
|
+
|
|
545
|
+
childLogger.info('this is my filtered the info message');
|
|
546
|
+
assert.isTrue(utils.isBlank(output), 'Did not filter info message as expected.');
|
|
547
|
+
|
|
548
|
+
logger.warning('this is the warning message');
|
|
549
|
+
let jsonObj = JSON.parse(output);
|
|
550
|
+
assert.strictEqual(jsonObj.msg, 'this is the warning message');
|
|
551
|
+
|
|
552
|
+
restartCustomWriter();
|
|
553
|
+
childLogger.warning('this is the child logger warning message');
|
|
554
|
+
jsonObj = JSON.parse(output);
|
|
555
|
+
assert.strictEqual(jsonObj.msg, 'this is the child logger warning message');
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it('apply current log level to new created logger - INFO', () => {
|
|
559
|
+
assert.isTrue(utils.isBlank(output));
|
|
560
|
+
|
|
561
|
+
setGlobalLogLevel(LogLevel.WARNING);
|
|
562
|
+
const newLogger = createLogger('testing logger');
|
|
563
|
+
|
|
564
|
+
newLogger.info('this is the info message for newly created logger');
|
|
565
|
+
assert.isTrue(utils.isBlank(output), 'Did not filter info message as expected.');
|
|
566
|
+
|
|
567
|
+
newLogger.warning('this is the warning message');
|
|
568
|
+
const jsonObj = JSON.parse(output);
|
|
569
|
+
assert.strictEqual(jsonObj.msg, 'this is the warning message');
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
describe('Log to file', function() {
|
|
575
|
+
timeout(this, 30000);
|
|
576
|
+
|
|
577
|
+
let loggerConfig: LoggerConfigs;
|
|
578
|
+
let logger: ILogger;
|
|
579
|
+
let testingLogDir: string;
|
|
580
|
+
let testingLogFile: string;
|
|
581
|
+
|
|
582
|
+
before(async () => {
|
|
583
|
+
testingLogDir = `${constants.libRoot}/output/testingLogs`;
|
|
584
|
+
if (utils.isDir(testingLogDir)) {
|
|
585
|
+
await utils.clearDir(testingLogDir);
|
|
586
|
+
} else {
|
|
587
|
+
fs.mkdirsSync(testingLogDir);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
testingLogFile = `${testingLogDir}/application.log`;
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
after(async () => {
|
|
594
|
+
await utils.deleteDir(testingLogDir);
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
beforeEach(async () => {
|
|
598
|
+
if (fs.existsSync(testingLogFile)) {
|
|
599
|
+
fs.unlinkSync(testingLogFile);
|
|
600
|
+
}
|
|
601
|
+
assert.isFalse(fs.existsSync(testingLogFile));
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Logging with Pino is asynchronous.
|
|
606
|
+
* We need to make sure the log is written to file by waiting.
|
|
607
|
+
* @see https://github.com/pinojs/pino/blob/master/docs/asynchronous.md
|
|
608
|
+
*/
|
|
609
|
+
async function logAndWait(msg: string) {
|
|
610
|
+
logger.error(msg);
|
|
611
|
+
|
|
612
|
+
assert.isFunction(logger[`pino`][`flush`], `The "flush method should exist on a Pino logger"`);
|
|
613
|
+
logger[`pino`][`flush`]();
|
|
614
|
+
|
|
615
|
+
const max = 5000;
|
|
616
|
+
let elapsed = 0;
|
|
617
|
+
const sleep = 200;
|
|
618
|
+
|
|
619
|
+
while (!fs.existsSync(testingLogFile) && elapsed < max) {
|
|
620
|
+
await utils.sleep(sleep);
|
|
621
|
+
elapsed += sleep;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
it('No log file - default', async () => {
|
|
626
|
+
loggerConfig = new LoggerConfigs(() => TESTING_CID);
|
|
627
|
+
loggerConfig.setLogHumanReadableinConsole(false);
|
|
628
|
+
loggerConfig.setLogSource(true);
|
|
629
|
+
loggerConfig.setLogLevel(LogLevel.DEBUG);
|
|
630
|
+
loggerConfig.setLogDirectory(testingLogDir);
|
|
631
|
+
|
|
632
|
+
initLogger(loggerConfig, 'default', true);
|
|
633
|
+
logger = new Logger('test');
|
|
634
|
+
|
|
635
|
+
await logAndWait('allo');
|
|
636
|
+
assert.isFalse(fs.existsSync(testingLogFile));
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it('No log file - explicit', async () => {
|
|
640
|
+
loggerConfig = new LoggerConfigs(() => TESTING_CID);
|
|
641
|
+
loggerConfig.setLogHumanReadableinConsole(false);
|
|
642
|
+
loggerConfig.setLogSource(true);
|
|
643
|
+
loggerConfig.setLogLevel(LogLevel.DEBUG);
|
|
644
|
+
loggerConfig.setLogDirectory(testingLogDir);
|
|
645
|
+
loggerConfig.setSlowerLogToFileToo(false);
|
|
646
|
+
|
|
647
|
+
initLogger(loggerConfig, 'default', true);
|
|
648
|
+
logger = new Logger('test');
|
|
649
|
+
|
|
650
|
+
await logAndWait('allo');
|
|
651
|
+
assert.isFalse(fs.existsSync(testingLogFile));
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it('Log file', async () => {
|
|
655
|
+
loggerConfig = new LoggerConfigs(() => TESTING_CID);
|
|
656
|
+
loggerConfig.setLogHumanReadableinConsole(false);
|
|
657
|
+
loggerConfig.setLogSource(true);
|
|
658
|
+
loggerConfig.setLogLevel(LogLevel.DEBUG);
|
|
659
|
+
loggerConfig.setLogDirectory(testingLogDir);
|
|
660
|
+
loggerConfig.setSlowerLogToFileToo(true);
|
|
661
|
+
|
|
662
|
+
initLogger(loggerConfig, 'default', true);
|
|
663
|
+
logger = new Logger('test');
|
|
664
|
+
|
|
665
|
+
await logAndWait('allo');
|
|
666
|
+
assert.isTrue(fs.existsSync(testingLogFile));
|
|
667
|
+
|
|
668
|
+
const content = fs.readFileSync(testingLogFile, 'utf-8');
|
|
669
|
+
assert.isOk(content);
|
|
670
|
+
assert.isTrue(content.indexOf(`"msg":"allo"`) > -1);
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
});
|