@geekmidas/logger 0.1.0 → 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.
@@ -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
- { app: 'test', userId: 123, ts: 1234567890 },
60
- 'Debug message',
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
- { app: 'test', userId: 123, ts: 1234567890 },
84
- 'Info message',
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
- { app: 'test', code: 'DEPRECATED', ts: 1234567890 },
108
- 'Warning message',
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
- { app: 'test', error, ts: 1234567890 },
133
- 'Error occurred',
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
- { app: 'test', exitCode: 1, ts: 1234567890 },
158
- 'Fatal error',
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
- { app: 'test', stack: 'trace', ts: 1234567890 },
182
- 'Trace message',
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
- app: 'myApp',
208
- env: 'production',
209
- userId: 123,
210
- action: 'login',
211
- ts: 1234567890,
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
- env: 'production',
225
- status: 'new', // Overridden
226
- userId: 456,
227
- ts: 1234567890,
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);
@@ -294,15 +375,13 @@ describe('ConsoleLogger', () => {
294
375
 
295
376
  childLogger.info({ query: 'SELECT *' }, 'Query executed');
296
377
 
297
- expect(console.info).toHaveBeenCalledWith(
298
- {
299
- app: 'myApp',
300
- module: 'database',
301
- query: 'SELECT *',
302
- ts: 1234567890,
303
- },
304
- 'Query executed',
305
- );
378
+ expect(console.info).toHaveBeenCalledWith({
379
+ app: 'myApp',
380
+ module: 'database',
381
+ query: 'SELECT *',
382
+ msg: 'Query executed',
383
+ ts: 1234567890,
384
+ });
306
385
  });
307
386
 
308
387
  it('should override parent context in child logger', () => {
@@ -326,16 +405,14 @@ describe('ConsoleLogger', () => {
326
405
 
327
406
  grandchildLogger.info({ table: 'users' }, 'Query executed');
328
407
 
329
- expect(console.info).toHaveBeenCalledWith(
330
- {
331
- app: 'myApp',
332
- module: 'database',
333
- operation: 'query',
334
- table: 'users',
335
- ts: 1234567890,
336
- },
337
- 'Query executed',
338
- );
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
+ });
339
416
  });
340
417
 
341
418
  it('should not affect parent logger', () => {
@@ -358,10 +435,11 @@ describe('ConsoleLogger', () => {
358
435
 
359
436
  logger.info('Simple message without context object');
360
437
 
361
- expect(console.info).toHaveBeenCalledWith(
362
- { app: 'test', ts: 1234567890 },
363
- 'Simple message without context object',
364
- );
438
+ expect(console.info).toHaveBeenCalledWith({
439
+ app: 'test',
440
+ msg: 'Simple message without context object',
441
+ ts: 1234567890,
442
+ });
365
443
  });
366
444
 
367
445
  it('should handle string-only logging with child logger', () => {
@@ -370,10 +448,12 @@ describe('ConsoleLogger', () => {
370
448
 
371
449
  child.warn('Warning message');
372
450
 
373
- expect(console.warn).toHaveBeenCalledWith(
374
- { app: 'test', module: 'auth', ts: 1234567890 },
375
- 'Warning message',
376
- );
451
+ expect(console.warn).toHaveBeenCalledWith({
452
+ app: 'test',
453
+ module: 'auth',
454
+ msg: 'Warning message',
455
+ ts: 1234567890,
456
+ });
377
457
  });
378
458
 
379
459
  it('should handle empty context object', () => {
@@ -381,10 +461,10 @@ describe('ConsoleLogger', () => {
381
461
 
382
462
  logger.info({}, 'Empty context');
383
463
 
384
- expect(console.info).toHaveBeenCalledWith(
385
- { ts: 1234567890 },
386
- 'Empty context',
387
- );
464
+ expect(console.info).toHaveBeenCalledWith({
465
+ msg: 'Empty context',
466
+ ts: 1234567890,
467
+ });
388
468
  });
389
469
 
390
470
  it('should handle complex nested objects', () => {
@@ -401,10 +481,11 @@ describe('ConsoleLogger', () => {
401
481
 
402
482
  logger.info(complexObj, 'Complex object');
403
483
 
404
- expect(console.info).toHaveBeenCalledWith(
405
- { ...complexObj, ts: 1234567890 },
406
- 'Complex object',
407
- );
484
+ expect(console.info).toHaveBeenCalledWith({
485
+ ...complexObj,
486
+ msg: 'Complex object',
487
+ ts: 1234567890,
488
+ });
408
489
  });
409
490
 
410
491
  it('should handle null and undefined values', () => {
@@ -412,10 +493,12 @@ describe('ConsoleLogger', () => {
412
493
 
413
494
  logger.info({ value: null, missing: undefined }, 'Null values');
414
495
 
415
- expect(console.info).toHaveBeenCalledWith(
416
- { value: null, missing: undefined, ts: 1234567890 },
417
- 'Null values',
418
- );
496
+ expect(console.info).toHaveBeenCalledWith({
497
+ value: null,
498
+ missing: undefined,
499
+ msg: 'Null values',
500
+ ts: 1234567890,
501
+ });
419
502
  });
420
503
 
421
504
  it('should handle special characters in strings', () => {
@@ -423,10 +506,11 @@ describe('ConsoleLogger', () => {
423
506
 
424
507
  logger.info({ message: 'Test\n\t"quotes"' }, 'Special chars');
425
508
 
426
- expect(console.info).toHaveBeenCalledWith(
427
- { message: 'Test\n\t"quotes"', ts: 1234567890 },
428
- 'Special chars',
429
- );
509
+ expect(console.info).toHaveBeenCalledWith({
510
+ message: 'Test\n\t"quotes"',
511
+ msg: 'Special chars',
512
+ ts: 1234567890,
513
+ });
430
514
  });
431
515
  });
432
516
 
@@ -444,17 +528,15 @@ describe('ConsoleLogger', () => {
444
528
  'HTTP request completed',
445
529
  );
446
530
 
447
- expect(console.info).toHaveBeenCalledWith(
448
- {
449
- service: 'api',
450
- method: 'POST',
451
- path: '/users',
452
- statusCode: 201,
453
- duration: 45,
454
- ts: 1234567890,
455
- },
456
- 'HTTP request completed',
457
- );
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
+ });
458
540
  });
459
541
 
460
542
  it('should log error with stack trace', () => {
@@ -475,9 +557,9 @@ describe('ConsoleLogger', () => {
475
557
  service: 'worker',
476
558
  error: 'Database connection failed',
477
559
  operation: 'connect',
560
+ msg: 'Database error',
478
561
  ts: 1234567890,
479
562
  }),
480
- 'Database error',
481
563
  );
482
564
  });
483
565
 
@@ -495,50 +577,47 @@ describe('ConsoleLogger', () => {
495
577
  'Order created successfully',
496
578
  );
497
579
 
498
- expect(console.info).toHaveBeenCalledWith(
499
- {
500
- app: 'ecommerce',
501
- eventType: 'order.created',
502
- orderId: 'ORD-123',
503
- userId: 456,
504
- amount: 99.99,
505
- currency: 'USD',
506
- ts: 1234567890,
507
- },
508
- 'Order created successfully',
509
- );
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
+ });
510
590
  });
511
591
 
512
592
  it('should use child logger for module-specific logging', () => {
513
- const appLogger = new ConsoleLogger({ app: 'myApp', env: 'production' });
593
+ const appLogger = new ConsoleLogger(
594
+ { app: 'myApp', env: 'production' },
595
+ LogLevel.Debug,
596
+ );
514
597
  const authLogger = appLogger.child({ module: 'auth' });
515
598
  const dbLogger = appLogger.child({ module: 'database' });
516
599
 
517
600
  authLogger.info({ userId: 123 }, 'User authenticated');
518
601
  dbLogger.debug({ query: 'SELECT *', duration: 10 }, 'Query executed');
519
602
 
520
- expect(console.info).toHaveBeenCalledWith(
521
- {
522
- app: 'myApp',
523
- env: 'production',
524
- module: 'auth',
525
- userId: 123,
526
- ts: 1234567890,
527
- },
528
- 'User authenticated',
529
- );
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
+ });
530
611
 
531
- expect(console.debug).toHaveBeenCalledWith(
532
- {
533
- app: 'myApp',
534
- env: 'production',
535
- module: 'database',
536
- query: 'SELECT *',
537
- duration: 10,
538
- ts: 1234567890,
539
- },
540
- 'Query executed',
541
- );
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
+ });
542
621
  });
543
622
  });
544
623
 
@@ -550,4 +629,35 @@ describe('ConsoleLogger', () => {
550
629
  expect(DEFAULT_LOGGER.data).toBeDefined();
551
630
  });
552
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
+ });
553
663
  });