@geekmidas/logger 0.0.1 → 0.2.0
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/README.md +114 -7
- package/dist/console.cjs +75 -17
- package/dist/console.cjs.map +1 -1
- package/dist/console.d.cts +31 -7
- package/dist/console.d.mts +31 -7
- package/dist/console.mjs +76 -18
- package/dist/console.mjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/pino.cjs +42 -1
- package/dist/pino.cjs.map +1 -1
- package/dist/pino.d.cts +23 -3
- package/dist/pino.d.mts +23 -3
- package/dist/pino.mjs +42 -2
- package/dist/pino.mjs.map +1 -1
- package/dist/redact-paths-Br-tI2GZ.d.cts +18 -0
- package/dist/redact-paths-CIsuxHH7.d.mts +18 -0
- package/dist/redact-paths-D0m0DIuQ.cjs +73 -0
- package/dist/redact-paths-D0m0DIuQ.cjs.map +1 -0
- package/dist/redact-paths-DQoIXhkS.mjs +67 -0
- package/dist/redact-paths-DQoIXhkS.mjs.map +1 -0
- package/dist/redact-paths.cjs +3 -0
- package/dist/redact-paths.d.cts +2 -0
- package/dist/redact-paths.d.mts +2 -0
- package/dist/redact-paths.mjs +3 -0
- package/dist/{types-C1RfRbo6.d.mts → types-Bga8WDuP.d.mts} +86 -2
- package/dist/{types-DXdmn7h5.d.cts → types-JxCFymH0.d.cts} +86 -2
- package/dist/types-ag_0Cvbg.cjs.map +1 -1
- package/dist/types-yQ6XOihF.mjs.map +1 -1
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/package.json +10 -1
- package/src/__tests__/console.spec.ts +277 -140
- package/src/__tests__/pino-redaction.integration.spec.ts +307 -0
- package/src/__tests__/pino.spec.ts +199 -0
- package/src/console.ts +95 -23
- package/src/index.ts +1 -0
- package/src/pino.ts +105 -2
- package/src/redact-paths.ts +71 -0
- package/src/types.ts +87 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { ConsoleLogger } from '../console';
|
|
2
|
+
import { ConsoleLogger, createLogger } from '../console';
|
|
3
|
+
import { LogLevel } from '../types';
|
|
3
4
|
|
|
4
5
|
describe('ConsoleLogger', () => {
|
|
5
6
|
// Mock console methods
|
|
@@ -50,19 +51,21 @@ describe('ConsoleLogger', () => {
|
|
|
50
51
|
|
|
51
52
|
describe('Log levels', () => {
|
|
52
53
|
describe('debug', () => {
|
|
53
|
-
it('should log debug message with context', () => {
|
|
54
|
-
const logger = new ConsoleLogger({ app: 'test' });
|
|
54
|
+
it('should log debug message with context when level is Debug', () => {
|
|
55
|
+
const logger = new ConsoleLogger({ app: 'test' }, LogLevel.Debug);
|
|
55
56
|
|
|
56
57
|
logger.debug({ userId: 123 }, 'Debug message');
|
|
57
58
|
|
|
58
|
-
expect(console.debug).toHaveBeenCalledWith(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
expect(console.debug).toHaveBeenCalledWith({
|
|
60
|
+
app: 'test',
|
|
61
|
+
userId: 123,
|
|
62
|
+
msg: 'Debug message',
|
|
63
|
+
ts: 1234567890,
|
|
64
|
+
});
|
|
62
65
|
});
|
|
63
66
|
|
|
64
|
-
it('should log debug with only context object', () => {
|
|
65
|
-
const logger = new ConsoleLogger();
|
|
67
|
+
it('should log debug with only context object when level is Debug', () => {
|
|
68
|
+
const logger = new ConsoleLogger({}, LogLevel.Debug);
|
|
66
69
|
|
|
67
70
|
logger.debug({ action: 'test' });
|
|
68
71
|
|
|
@@ -79,10 +82,12 @@ describe('ConsoleLogger', () => {
|
|
|
79
82
|
|
|
80
83
|
logger.info({ userId: 123 }, 'Info message');
|
|
81
84
|
|
|
82
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
86
|
+
app: 'test',
|
|
87
|
+
userId: 123,
|
|
88
|
+
msg: 'Info message',
|
|
89
|
+
ts: 1234567890,
|
|
90
|
+
});
|
|
86
91
|
});
|
|
87
92
|
|
|
88
93
|
it('should log info with only context object', () => {
|
|
@@ -103,10 +108,12 @@ describe('ConsoleLogger', () => {
|
|
|
103
108
|
|
|
104
109
|
logger.warn({ code: 'DEPRECATED' }, 'Warning message');
|
|
105
110
|
|
|
106
|
-
expect(console.warn).toHaveBeenCalledWith(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
111
|
+
expect(console.warn).toHaveBeenCalledWith({
|
|
112
|
+
app: 'test',
|
|
113
|
+
code: 'DEPRECATED',
|
|
114
|
+
msg: 'Warning message',
|
|
115
|
+
ts: 1234567890,
|
|
116
|
+
});
|
|
110
117
|
});
|
|
111
118
|
|
|
112
119
|
it('should log warning with only context object', () => {
|
|
@@ -128,10 +135,12 @@ describe('ConsoleLogger', () => {
|
|
|
128
135
|
|
|
129
136
|
logger.error({ error }, 'Error occurred');
|
|
130
137
|
|
|
131
|
-
expect(console.error).toHaveBeenCalledWith(
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
138
|
+
expect(console.error).toHaveBeenCalledWith({
|
|
139
|
+
app: 'test',
|
|
140
|
+
error,
|
|
141
|
+
msg: 'Error occurred',
|
|
142
|
+
ts: 1234567890,
|
|
143
|
+
});
|
|
135
144
|
});
|
|
136
145
|
|
|
137
146
|
it('should log error with only context object', () => {
|
|
@@ -153,10 +162,12 @@ describe('ConsoleLogger', () => {
|
|
|
153
162
|
|
|
154
163
|
logger.fatal({ exitCode: 1 }, 'Fatal error');
|
|
155
164
|
|
|
156
|
-
expect(console.error).toHaveBeenCalledWith(
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
165
|
+
expect(console.error).toHaveBeenCalledWith({
|
|
166
|
+
app: 'test',
|
|
167
|
+
exitCode: 1,
|
|
168
|
+
msg: 'Fatal error',
|
|
169
|
+
ts: 1234567890,
|
|
170
|
+
});
|
|
160
171
|
});
|
|
161
172
|
|
|
162
173
|
it('should log fatal with only context object', () => {
|
|
@@ -172,19 +183,21 @@ describe('ConsoleLogger', () => {
|
|
|
172
183
|
});
|
|
173
184
|
|
|
174
185
|
describe('trace', () => {
|
|
175
|
-
it('should log trace message with context', () => {
|
|
176
|
-
const logger = new ConsoleLogger({ app: 'test' });
|
|
186
|
+
it('should log trace message with context when level is Trace', () => {
|
|
187
|
+
const logger = new ConsoleLogger({ app: 'test' }, LogLevel.Trace);
|
|
177
188
|
|
|
178
189
|
logger.trace({ stack: 'trace' }, 'Trace message');
|
|
179
190
|
|
|
180
|
-
expect(console.trace).toHaveBeenCalledWith(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
191
|
+
expect(console.trace).toHaveBeenCalledWith({
|
|
192
|
+
app: 'test',
|
|
193
|
+
stack: 'trace',
|
|
194
|
+
msg: 'Trace message',
|
|
195
|
+
ts: 1234567890,
|
|
196
|
+
});
|
|
184
197
|
});
|
|
185
198
|
|
|
186
|
-
it('should log trace with only context object', () => {
|
|
187
|
-
const logger = new ConsoleLogger();
|
|
199
|
+
it('should log trace with only context object when level is Trace', () => {
|
|
200
|
+
const logger = new ConsoleLogger({}, LogLevel.Trace);
|
|
188
201
|
|
|
189
202
|
logger.trace({ depth: 5 });
|
|
190
203
|
|
|
@@ -196,22 +209,94 @@ describe('ConsoleLogger', () => {
|
|
|
196
209
|
});
|
|
197
210
|
});
|
|
198
211
|
|
|
212
|
+
describe('Log level filtering', () => {
|
|
213
|
+
it('should not log debug when level is Info', () => {
|
|
214
|
+
const logger = new ConsoleLogger({}, LogLevel.Info);
|
|
215
|
+
|
|
216
|
+
logger.debug('This should not appear');
|
|
217
|
+
|
|
218
|
+
expect(console.debug).not.toHaveBeenCalled();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('should not log trace when level is Debug', () => {
|
|
222
|
+
const logger = new ConsoleLogger({}, LogLevel.Debug);
|
|
223
|
+
|
|
224
|
+
logger.trace('This should not appear');
|
|
225
|
+
|
|
226
|
+
expect(console.trace).not.toHaveBeenCalled();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should log debug when level is Debug', () => {
|
|
230
|
+
const logger = new ConsoleLogger({}, LogLevel.Debug);
|
|
231
|
+
|
|
232
|
+
logger.debug('Debug message');
|
|
233
|
+
|
|
234
|
+
expect(console.debug).toHaveBeenCalled();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should log info when level is Debug', () => {
|
|
238
|
+
const logger = new ConsoleLogger({}, LogLevel.Debug);
|
|
239
|
+
|
|
240
|
+
logger.info('Info message');
|
|
241
|
+
|
|
242
|
+
expect(console.info).toHaveBeenCalled();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('should not log anything when level is Silent', () => {
|
|
246
|
+
const logger = new ConsoleLogger({}, LogLevel.Silent);
|
|
247
|
+
|
|
248
|
+
logger.trace('trace');
|
|
249
|
+
logger.debug('debug');
|
|
250
|
+
logger.info('info');
|
|
251
|
+
logger.warn('warn');
|
|
252
|
+
logger.error('error');
|
|
253
|
+
logger.fatal('fatal');
|
|
254
|
+
|
|
255
|
+
expect(console.trace).not.toHaveBeenCalled();
|
|
256
|
+
expect(console.debug).not.toHaveBeenCalled();
|
|
257
|
+
expect(console.info).not.toHaveBeenCalled();
|
|
258
|
+
expect(console.warn).not.toHaveBeenCalled();
|
|
259
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('should log error when level is Error', () => {
|
|
263
|
+
const logger = new ConsoleLogger({}, LogLevel.Error);
|
|
264
|
+
|
|
265
|
+
logger.info('This should not appear');
|
|
266
|
+
logger.warn('This should not appear');
|
|
267
|
+
logger.error('Error message');
|
|
268
|
+
|
|
269
|
+
expect(console.info).not.toHaveBeenCalled();
|
|
270
|
+
expect(console.warn).not.toHaveBeenCalled();
|
|
271
|
+
expect(console.error).toHaveBeenCalled();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should inherit log level in child logger', () => {
|
|
275
|
+
const parent = new ConsoleLogger({}, LogLevel.Warn);
|
|
276
|
+
const child = parent.child({ module: 'test' });
|
|
277
|
+
|
|
278
|
+
child.info('This should not appear');
|
|
279
|
+
child.warn('Warning message');
|
|
280
|
+
|
|
281
|
+
expect(console.info).not.toHaveBeenCalled();
|
|
282
|
+
expect(console.warn).toHaveBeenCalled();
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
|
|
199
286
|
describe('Context merging', () => {
|
|
200
287
|
it('should merge logger context with log context', () => {
|
|
201
288
|
const logger = new ConsoleLogger({ app: 'myApp', env: 'production' });
|
|
202
289
|
|
|
203
290
|
logger.info({ userId: 123, action: 'login' }, 'User logged in');
|
|
204
291
|
|
|
205
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
'User logged in',
|
|
214
|
-
);
|
|
292
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
293
|
+
app: 'myApp',
|
|
294
|
+
env: 'production',
|
|
295
|
+
userId: 123,
|
|
296
|
+
action: 'login',
|
|
297
|
+
msg: 'User logged in',
|
|
298
|
+
ts: 1234567890,
|
|
299
|
+
});
|
|
215
300
|
});
|
|
216
301
|
|
|
217
302
|
it('should override logger context with log context', () => {
|
|
@@ -219,15 +304,13 @@ describe('ConsoleLogger', () => {
|
|
|
219
304
|
|
|
220
305
|
logger.info({ status: 'new', userId: 456 }, 'Status updated');
|
|
221
306
|
|
|
222
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
'Status updated',
|
|
230
|
-
);
|
|
307
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
308
|
+
env: 'production',
|
|
309
|
+
status: 'new', // Overridden
|
|
310
|
+
userId: 456,
|
|
311
|
+
msg: 'Status updated',
|
|
312
|
+
ts: 1234567890,
|
|
313
|
+
});
|
|
231
314
|
});
|
|
232
315
|
|
|
233
316
|
it('should always add timestamp', () => {
|
|
@@ -237,7 +320,6 @@ describe('ConsoleLogger', () => {
|
|
|
237
320
|
|
|
238
321
|
expect(console.info).toHaveBeenCalledWith(
|
|
239
322
|
expect.objectContaining({ ts: 1234567890 }),
|
|
240
|
-
'Test message',
|
|
241
323
|
);
|
|
242
324
|
});
|
|
243
325
|
});
|
|
@@ -251,15 +333,14 @@ describe('ConsoleLogger', () => {
|
|
|
251
333
|
logger.info({ action: 'test' }, 'Message', obj1, obj2);
|
|
252
334
|
|
|
253
335
|
expect(console.info).toHaveBeenCalledWith(
|
|
254
|
-
{ action: 'test', ts: 1234567890 },
|
|
255
|
-
'Message',
|
|
336
|
+
{ action: 'test', msg: 'Message', ts: 1234567890 },
|
|
256
337
|
obj1,
|
|
257
338
|
obj2,
|
|
258
339
|
);
|
|
259
340
|
});
|
|
260
341
|
|
|
261
342
|
it('should pass additional arguments without message', () => {
|
|
262
|
-
const logger = new ConsoleLogger();
|
|
343
|
+
const logger = new ConsoleLogger({}, LogLevel.Debug);
|
|
263
344
|
const extra = 'extra data';
|
|
264
345
|
|
|
265
346
|
logger.debug({ action: 'test' }, undefined as any, extra);
|
|
@@ -277,7 +358,9 @@ describe('ConsoleLogger', () => {
|
|
|
277
358
|
app: 'myApp',
|
|
278
359
|
version: '1.0.0',
|
|
279
360
|
});
|
|
280
|
-
const childLogger = parentLogger.child({
|
|
361
|
+
const childLogger = parentLogger.child({
|
|
362
|
+
module: 'auth',
|
|
363
|
+
}) as ConsoleLogger;
|
|
281
364
|
|
|
282
365
|
expect(childLogger.data).toEqual({
|
|
283
366
|
app: 'myApp',
|
|
@@ -292,15 +375,13 @@ describe('ConsoleLogger', () => {
|
|
|
292
375
|
|
|
293
376
|
childLogger.info({ query: 'SELECT *' }, 'Query executed');
|
|
294
377
|
|
|
295
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
'Query executed',
|
|
303
|
-
);
|
|
378
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
379
|
+
app: 'myApp',
|
|
380
|
+
module: 'database',
|
|
381
|
+
query: 'SELECT *',
|
|
382
|
+
msg: 'Query executed',
|
|
383
|
+
ts: 1234567890,
|
|
384
|
+
});
|
|
304
385
|
});
|
|
305
386
|
|
|
306
387
|
it('should override parent context in child logger', () => {
|
|
@@ -308,7 +389,7 @@ describe('ConsoleLogger', () => {
|
|
|
308
389
|
const childLogger = parentLogger.child({
|
|
309
390
|
status: 'child',
|
|
310
391
|
module: 'api',
|
|
311
|
-
});
|
|
392
|
+
}) as ConsoleLogger;
|
|
312
393
|
|
|
313
394
|
expect(childLogger.data).toEqual({
|
|
314
395
|
env: 'dev',
|
|
@@ -324,21 +405,21 @@ describe('ConsoleLogger', () => {
|
|
|
324
405
|
|
|
325
406
|
grandchildLogger.info({ table: 'users' }, 'Query executed');
|
|
326
407
|
|
|
327
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
'Query executed',
|
|
336
|
-
);
|
|
408
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
409
|
+
app: 'myApp',
|
|
410
|
+
module: 'database',
|
|
411
|
+
operation: 'query',
|
|
412
|
+
table: 'users',
|
|
413
|
+
msg: 'Query executed',
|
|
414
|
+
ts: 1234567890,
|
|
415
|
+
});
|
|
337
416
|
});
|
|
338
417
|
|
|
339
418
|
it('should not affect parent logger', () => {
|
|
340
419
|
const parentLogger = new ConsoleLogger({ app: 'myApp' });
|
|
341
|
-
const childLogger = parentLogger.child({
|
|
420
|
+
const childLogger = parentLogger.child({
|
|
421
|
+
module: 'auth',
|
|
422
|
+
}) as ConsoleLogger;
|
|
342
423
|
|
|
343
424
|
// Child logger has additional context
|
|
344
425
|
expect(childLogger.data).toEqual({ app: 'myApp', module: 'auth' });
|
|
@@ -349,15 +430,41 @@ describe('ConsoleLogger', () => {
|
|
|
349
430
|
});
|
|
350
431
|
|
|
351
432
|
describe('Edge cases', () => {
|
|
433
|
+
it('should handle string-only logging without context object', () => {
|
|
434
|
+
const logger = new ConsoleLogger({ app: 'test' });
|
|
435
|
+
|
|
436
|
+
logger.info('Simple message without context object');
|
|
437
|
+
|
|
438
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
439
|
+
app: 'test',
|
|
440
|
+
msg: 'Simple message without context object',
|
|
441
|
+
ts: 1234567890,
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it('should handle string-only logging with child logger', () => {
|
|
446
|
+
const logger = new ConsoleLogger({ app: 'test' });
|
|
447
|
+
const child = logger.child({ module: 'auth' });
|
|
448
|
+
|
|
449
|
+
child.warn('Warning message');
|
|
450
|
+
|
|
451
|
+
expect(console.warn).toHaveBeenCalledWith({
|
|
452
|
+
app: 'test',
|
|
453
|
+
module: 'auth',
|
|
454
|
+
msg: 'Warning message',
|
|
455
|
+
ts: 1234567890,
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
|
|
352
459
|
it('should handle empty context object', () => {
|
|
353
460
|
const logger = new ConsoleLogger();
|
|
354
461
|
|
|
355
462
|
logger.info({}, 'Empty context');
|
|
356
463
|
|
|
357
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
);
|
|
464
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
465
|
+
msg: 'Empty context',
|
|
466
|
+
ts: 1234567890,
|
|
467
|
+
});
|
|
361
468
|
});
|
|
362
469
|
|
|
363
470
|
it('should handle complex nested objects', () => {
|
|
@@ -374,10 +481,11 @@ describe('ConsoleLogger', () => {
|
|
|
374
481
|
|
|
375
482
|
logger.info(complexObj, 'Complex object');
|
|
376
483
|
|
|
377
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
378
|
-
|
|
379
|
-
'Complex object',
|
|
380
|
-
|
|
484
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
485
|
+
...complexObj,
|
|
486
|
+
msg: 'Complex object',
|
|
487
|
+
ts: 1234567890,
|
|
488
|
+
});
|
|
381
489
|
});
|
|
382
490
|
|
|
383
491
|
it('should handle null and undefined values', () => {
|
|
@@ -385,10 +493,12 @@ describe('ConsoleLogger', () => {
|
|
|
385
493
|
|
|
386
494
|
logger.info({ value: null, missing: undefined }, 'Null values');
|
|
387
495
|
|
|
388
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
496
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
497
|
+
value: null,
|
|
498
|
+
missing: undefined,
|
|
499
|
+
msg: 'Null values',
|
|
500
|
+
ts: 1234567890,
|
|
501
|
+
});
|
|
392
502
|
});
|
|
393
503
|
|
|
394
504
|
it('should handle special characters in strings', () => {
|
|
@@ -396,10 +506,11 @@ describe('ConsoleLogger', () => {
|
|
|
396
506
|
|
|
397
507
|
logger.info({ message: 'Test\n\t"quotes"' }, 'Special chars');
|
|
398
508
|
|
|
399
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
400
|
-
|
|
401
|
-
'Special chars',
|
|
402
|
-
|
|
509
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
510
|
+
message: 'Test\n\t"quotes"',
|
|
511
|
+
msg: 'Special chars',
|
|
512
|
+
ts: 1234567890,
|
|
513
|
+
});
|
|
403
514
|
});
|
|
404
515
|
});
|
|
405
516
|
|
|
@@ -417,17 +528,15 @@ describe('ConsoleLogger', () => {
|
|
|
417
528
|
'HTTP request completed',
|
|
418
529
|
);
|
|
419
530
|
|
|
420
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
'HTTP request completed',
|
|
430
|
-
);
|
|
531
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
532
|
+
service: 'api',
|
|
533
|
+
method: 'POST',
|
|
534
|
+
path: '/users',
|
|
535
|
+
statusCode: 201,
|
|
536
|
+
duration: 45,
|
|
537
|
+
msg: 'HTTP request completed',
|
|
538
|
+
ts: 1234567890,
|
|
539
|
+
});
|
|
431
540
|
});
|
|
432
541
|
|
|
433
542
|
it('should log error with stack trace', () => {
|
|
@@ -448,9 +557,9 @@ describe('ConsoleLogger', () => {
|
|
|
448
557
|
service: 'worker',
|
|
449
558
|
error: 'Database connection failed',
|
|
450
559
|
operation: 'connect',
|
|
560
|
+
msg: 'Database error',
|
|
451
561
|
ts: 1234567890,
|
|
452
562
|
}),
|
|
453
|
-
'Database error',
|
|
454
563
|
);
|
|
455
564
|
});
|
|
456
565
|
|
|
@@ -468,50 +577,47 @@ describe('ConsoleLogger', () => {
|
|
|
468
577
|
'Order created successfully',
|
|
469
578
|
);
|
|
470
579
|
|
|
471
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
'Order created successfully',
|
|
482
|
-
);
|
|
580
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
581
|
+
app: 'ecommerce',
|
|
582
|
+
eventType: 'order.created',
|
|
583
|
+
orderId: 'ORD-123',
|
|
584
|
+
userId: 456,
|
|
585
|
+
amount: 99.99,
|
|
586
|
+
currency: 'USD',
|
|
587
|
+
msg: 'Order created successfully',
|
|
588
|
+
ts: 1234567890,
|
|
589
|
+
});
|
|
483
590
|
});
|
|
484
591
|
|
|
485
592
|
it('should use child logger for module-specific logging', () => {
|
|
486
|
-
const appLogger = new ConsoleLogger(
|
|
593
|
+
const appLogger = new ConsoleLogger(
|
|
594
|
+
{ app: 'myApp', env: 'production' },
|
|
595
|
+
LogLevel.Debug,
|
|
596
|
+
);
|
|
487
597
|
const authLogger = appLogger.child({ module: 'auth' });
|
|
488
598
|
const dbLogger = appLogger.child({ module: 'database' });
|
|
489
599
|
|
|
490
600
|
authLogger.info({ userId: 123 }, 'User authenticated');
|
|
491
601
|
dbLogger.debug({ query: 'SELECT *', duration: 10 }, 'Query executed');
|
|
492
602
|
|
|
493
|
-
expect(console.info).toHaveBeenCalledWith(
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
'User authenticated',
|
|
502
|
-
);
|
|
603
|
+
expect(console.info).toHaveBeenCalledWith({
|
|
604
|
+
app: 'myApp',
|
|
605
|
+
env: 'production',
|
|
606
|
+
module: 'auth',
|
|
607
|
+
userId: 123,
|
|
608
|
+
msg: 'User authenticated',
|
|
609
|
+
ts: 1234567890,
|
|
610
|
+
});
|
|
503
611
|
|
|
504
|
-
expect(console.debug).toHaveBeenCalledWith(
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
'Query executed',
|
|
514
|
-
);
|
|
612
|
+
expect(console.debug).toHaveBeenCalledWith({
|
|
613
|
+
app: 'myApp',
|
|
614
|
+
env: 'production',
|
|
615
|
+
module: 'database',
|
|
616
|
+
query: 'SELECT *',
|
|
617
|
+
duration: 10,
|
|
618
|
+
msg: 'Query executed',
|
|
619
|
+
ts: 1234567890,
|
|
620
|
+
});
|
|
515
621
|
});
|
|
516
622
|
});
|
|
517
623
|
|
|
@@ -523,4 +629,35 @@ describe('ConsoleLogger', () => {
|
|
|
523
629
|
expect(DEFAULT_LOGGER.data).toBeDefined();
|
|
524
630
|
});
|
|
525
631
|
});
|
|
632
|
+
|
|
633
|
+
describe('createLogger', () => {
|
|
634
|
+
it('should create logger with default Info level', () => {
|
|
635
|
+
const logger = createLogger();
|
|
636
|
+
|
|
637
|
+
logger.debug('This should not appear');
|
|
638
|
+
logger.info('This should appear');
|
|
639
|
+
|
|
640
|
+
expect(console.debug).not.toHaveBeenCalled();
|
|
641
|
+
expect(console.info).toHaveBeenCalled();
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it('should create logger with specified level', () => {
|
|
645
|
+
const logger = createLogger({ level: LogLevel.Debug });
|
|
646
|
+
|
|
647
|
+
logger.trace('This should not appear');
|
|
648
|
+
logger.debug('This should appear');
|
|
649
|
+
|
|
650
|
+
expect(console.trace).not.toHaveBeenCalled();
|
|
651
|
+
expect(console.debug).toHaveBeenCalled();
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it('should create silent logger', () => {
|
|
655
|
+
const logger = createLogger({ level: LogLevel.Silent });
|
|
656
|
+
|
|
657
|
+
logger.error('This should not appear');
|
|
658
|
+
logger.fatal('This should not appear');
|
|
659
|
+
|
|
660
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
661
|
+
});
|
|
662
|
+
});
|
|
526
663
|
});
|