@adviser/cement 0.2.29 → 0.2.30

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,1132 +0,0 @@
1
- import {
2
- TimeFactory,
3
- IsLogger,
4
- Level,
5
- LevelHandlerImpl,
6
- LogCollector,
7
- Logger,
8
- LoggerImpl,
9
- logValue,
10
- Result,
11
- runtimeFn,
12
- TimeMode,
13
- BuildURI,
14
- URI,
15
- MutableURL,
16
- JSONFormatter,
17
- YAMLFormatter,
18
- } from "@adviser/cement";
19
- import { WebSysAbstraction } from "@adviser/cement/web";
20
-
21
- describe("TestLogger", () => {
22
- let logCollector: LogCollector;
23
- let logger: Logger;
24
-
25
- beforeEach(() => {
26
- logCollector = new LogCollector();
27
- logger = new LoggerImpl({
28
- out: logCollector,
29
- sys: WebSysAbstraction({ TimeMode: TimeMode.STEP }),
30
- levelHandler: new LevelHandlerImpl(),
31
- });
32
- });
33
-
34
- describe("Error()", () => {
35
- it("should set the level attribute to error", async () => {
36
- logger.Error().Msg("");
37
- await logger.Flush();
38
- expect(logCollector.Logs()).toEqual([{ level: "error" }]);
39
- });
40
-
41
- it("should set the error message", async () => {
42
- logger.Err(new Error("test")).Msg("");
43
- await logger.Flush();
44
- expect(logCollector.Logs()).toEqual([{ error: "test" }]);
45
- });
46
-
47
- it("should set the error from string", async () => {
48
- logger.Err("test").Msg("");
49
- await logger.Flush();
50
- expect(logCollector.Logs()).toEqual([{ error: "test" }]);
51
- });
52
-
53
- it("should set the error from bool", async () => {
54
- logger.Err(false).Msg("");
55
- await logger.Flush();
56
- expect(logCollector.Logs()).toEqual([{ error: "false" }]);
57
- });
58
- });
59
-
60
- describe("Info()", () => {
61
- it("should set the level attribute to info", async () => {
62
- logger.Info().Msg("");
63
- await logger.Flush();
64
- expect(logCollector.Logs()).toEqual([{ level: "info" }]);
65
- });
66
- });
67
-
68
- describe("Any()", () => {
69
- it("should set the Any attribute", async () => {
70
- logger.Any("key", "value").Msg("");
71
- await logger.Flush();
72
- expect(logCollector.Logs()).toEqual([{ key: "value" }]);
73
- });
74
- });
75
-
76
- describe("Dur()", () => {
77
- it("should set the Dur attribute", async () => {
78
- logger.Dur("key", 123).Msg("");
79
- await logger.Flush();
80
- expect(logCollector.Logs()).toEqual([{ key: "123ms" }]);
81
- });
82
- });
83
- describe("Uint64()", () => {
84
- it("should set the Uint64 / number attribute", async () => {
85
- logger.Uint64("Hey", 123).Msg("");
86
- await logger.Flush();
87
- expect(logCollector.Logs()).toEqual([{ Hey: 123 }]);
88
- });
89
- });
90
- describe("Str()", () => {
91
- it("should set the String attribute", async () => {
92
- logger.Str("key", "value").Msg("");
93
- await logger.Flush();
94
- expect(logCollector.Logs()).toEqual([{ key: "value" }]);
95
- });
96
- });
97
-
98
- describe("With()", () => {
99
- it("should return a new logger with the same attributes", async () => {
100
- const log = logger.With().Str("key", "value").Logger();
101
- const newLogger = log.With().Str("str", "str").Logger();
102
- logger.Msg("logger1");
103
- logger.Msg("logger2");
104
- newLogger.Msg("newLogger1");
105
- newLogger.Msg("newLogger2");
106
-
107
- log.Info().Msg("log1");
108
- log.Info().Msg("log2");
109
- await log.Flush();
110
-
111
- expect(logCollector.Logs()).toEqual([
112
- { msg: "logger1" },
113
- { msg: "logger2" },
114
- { key: "value", msg: "newLogger1", str: "str" },
115
- { key: "value", msg: "newLogger2", str: "str" },
116
- { level: "info", key: "value", msg: "log1" },
117
- { level: "info", key: "value", msg: "log2" },
118
- ]);
119
- });
120
- });
121
-
122
- describe("Timestamp()", () => {
123
- it("should set the Timestamp attribute", async () => {
124
- const WithConstant = logger.With().Str("key", "withconstant").Str("key1", "anotherone").Logger();
125
- const timelog = WithConstant.With().Timestamp().Str("key", "withconstant2").Logger();
126
- timelog.Msg("1");
127
- timelog.Msg("2");
128
- timelog.Timestamp().Msg("3");
129
-
130
- await timelog.Flush();
131
- const timer = TimeFactory(TimeMode.STEP);
132
-
133
- expect(logCollector.Logs()).toEqual([
134
- {
135
- key: "withconstant2",
136
- key1: "anotherone",
137
- ts: timer.Now().toISOString(),
138
- msg: "1",
139
- },
140
- {
141
- key: "withconstant2",
142
- key1: "anotherone",
143
- ts: timer.Now().toISOString(),
144
- msg: "2",
145
- },
146
- {
147
- key: "withconstant2",
148
- key1: "anotherone",
149
- ts: timer.Now().toISOString(),
150
- msg: "3",
151
- },
152
- ]);
153
- });
154
-
155
- it("should NOT set the Timestamp attribute", async () => {
156
- const timelog = logger.With().Logger();
157
- timelog.Msg("1");
158
- timelog.Msg("2");
159
- timelog.Timestamp().Msg("3");
160
-
161
- await timelog.Flush();
162
- const timer = TimeFactory(TimeMode.STEP);
163
-
164
- expect(logCollector.Logs()).toEqual([
165
- { msg: "1" },
166
- { msg: "2" },
167
- {
168
- ts: timer.Now().toISOString(),
169
- msg: "3",
170
- },
171
- ]);
172
- });
173
- });
174
-
175
- it("remove empty msg", async () => {
176
- const log = logger;
177
- log.Warn().Msg();
178
- await log.Flush();
179
- expect(logCollector.Logs()).toEqual([{ level: "warn" }]);
180
- });
181
-
182
- it("check log level", async () => {
183
- const log = logger.With().Module("test").Logger().With().Logger();
184
- log.Warn().Msg("Warn");
185
- log.Info().Msg("Info");
186
- log.Error().Msg("Error");
187
- log.Log().Msg("Log");
188
- log.WithLevel(Level.ERROR).Msg("WithLevel");
189
- log.Debug().Str("should", "reset").Msg("Debug");
190
- log.Info().Str("what", "the").Msg("Simple");
191
- await log.Flush();
192
- const expected = [
193
- { msg: "Warn", level: "warn", module: "test" },
194
- { msg: "Info", level: "info", module: "test" },
195
- { msg: "Error", level: "error", module: "test" },
196
- { msg: "Log", module: "test" },
197
- { msg: "WithLevel", level: "error", module: "test" },
198
- { level: "info", module: "test", msg: "Simple", what: "the" },
199
- ];
200
- expect(logCollector.Logs()).toEqual(expected);
201
- logCollector.Logs().splice(0, logCollector.Logs().length);
202
- logger.With().Logger().SetDebug("test");
203
- log.Debug().Msg("Debug1");
204
- await log.Flush();
205
- expect(logCollector.Logs()).toEqual([...expected, { msg: "Debug1", level: "debug", module: "test" }]);
206
- });
207
-
208
- it("should flush all logs", async () => {
209
- const log = new LoggerImpl();
210
- log.Info().Msg("1");
211
- log.Info().Msg("2");
212
- await log.Flush();
213
- log.Info().Msg("DONE");
214
- return log.Flush();
215
- });
216
-
217
- it("carry debug", async () => {
218
- const log = logger;
219
- log.Module("xxx").SetDebug("yyy", ["i , xxx"]);
220
-
221
- log.Debug().Msg("Debug1");
222
- const next1 = log.With().Str("next1", "meno").Logger();
223
- next1.Debug().Msg("Next1");
224
- const next2 = next1.With().Str("next2", "meno").Logger();
225
- next2.Debug().Msg("Next2");
226
-
227
- next2.Module("zzz");
228
- next2.Debug().Msg("Next3");
229
-
230
- log.Debug().Msg("Top");
231
- next1.Debug().Msg("Next1");
232
-
233
- await log.Flush();
234
-
235
- expect(logCollector.Logs()).toEqual([
236
- {
237
- level: "debug",
238
- module: "xxx",
239
- msg: "Debug1",
240
- },
241
- {
242
- level: "debug",
243
- module: "xxx",
244
- msg: "Next1",
245
- next1: "meno",
246
- },
247
- {
248
- level: "debug",
249
- module: "xxx",
250
- msg: "Next2",
251
- next1: "meno",
252
- next2: "meno",
253
- },
254
- {
255
- level: "debug",
256
- module: "xxx",
257
- msg: "Top",
258
- },
259
- {
260
- level: "debug",
261
- module: "xxx",
262
- msg: "Next1",
263
- next1: "meno",
264
- },
265
- ]);
266
- });
267
-
268
- it("should return an Error on Msg", async () => {
269
- const log = logger;
270
- log.Module("xxx").SetDebug("xxx");
271
- log.Debug().Msg("Debug1");
272
- expect(JSON.parse(log.Debug().Msg("Debug2").AsError().message)).toEqual({
273
- level: "debug",
274
- module: "xxx",
275
- msg: "Debug2",
276
- });
277
-
278
- expect(JSON.parse(log.Info().Msg("Info2").AsError().message)).toEqual({
279
- level: "info",
280
- module: "xxx",
281
- msg: "Info2",
282
- });
283
-
284
- await log.Flush();
285
- expect(logCollector.Logs()).toEqual([
286
- {
287
- level: "debug",
288
- module: "xxx",
289
- msg: "Debug1",
290
- },
291
- {
292
- level: "debug",
293
- module: "xxx",
294
- msg: "Debug2",
295
- },
296
- {
297
- level: "info",
298
- module: "xxx",
299
- msg: "Info2",
300
- },
301
- ]);
302
- });
303
-
304
- it("top should enable modules wildcard", async () => {
305
- const log = logger;
306
-
307
- const xxxModule = log.With().Module("xxx").Logger();
308
-
309
- log.Debug().Msg("log-Msg0");
310
- xxxModule.Debug().Msg("xxx-log-Msg0");
311
- log.EnableLevel(Level.DEBUG);
312
-
313
- log.Debug().Msg("log-Msg");
314
- xxxModule.Debug().Msg("xxx-log-Msg");
315
-
316
- const yyyModule = log.With().Module("yyy").Logger();
317
- yyyModule.Debug().Msg("yyy-log-Msg");
318
-
319
- log.DisableLevel(Level.DEBUG);
320
- yyyModule.Debug().Msg("yyy-log-Msg1");
321
- log.Debug().Msg("log-Msg1");
322
- xxxModule.Debug().Msg("xxx-log-Msg1");
323
-
324
- await log.Flush();
325
- expect(logCollector.Logs()).toEqual([
326
- {
327
- level: "debug",
328
- msg: "log-Msg",
329
- },
330
- {
331
- level: "debug",
332
- module: "xxx",
333
- msg: "xxx-log-Msg",
334
- },
335
- {
336
- level: "debug",
337
- module: "yyy",
338
- msg: "yyy-log-Msg",
339
- },
340
- ]);
341
- });
342
-
343
- it("down should enable modules wildcard", async () => {
344
- const log = logger;
345
-
346
- const xxxModule = log.With().Module("xxx").Logger();
347
-
348
- log.Debug().Msg("log-Msg");
349
- xxxModule.Debug().Msg("xxx-log-Msg");
350
- xxxModule.EnableLevel(Level.DEBUG);
351
-
352
- log.Debug().Msg("log-Msg1");
353
- xxxModule.Debug().Msg("xxx-log-Msg1");
354
-
355
- const yyyModule = log.With().Module("yyy").Logger();
356
- yyyModule.Debug().Msg("yyy-log-Msg");
357
-
358
- yyyModule.DisableLevel(Level.DEBUG);
359
-
360
- log.Debug().Msg("log-Msg2");
361
- xxxModule.Debug().Msg("xxx-log-Msg2");
362
- yyyModule.Debug().Msg("yyy-log-Msg2");
363
-
364
- await log.Flush();
365
- expect(logCollector.Logs()).toEqual([
366
- {
367
- level: "debug",
368
- msg: "log-Msg1",
369
- },
370
- {
371
- level: "debug",
372
- module: "xxx",
373
- msg: "xxx-log-Msg1",
374
- },
375
- {
376
- level: "debug",
377
- module: "yyy",
378
- msg: "yyy-log-Msg",
379
- },
380
- ]);
381
- });
382
-
383
- it("global set debug on modules", async () => {
384
- const log = logger;
385
-
386
- const xxxModule = log.With().Module("xxx").Logger();
387
-
388
- log.Debug().Msg("log-Msg");
389
- xxxModule.Debug().Msg("xxx-log-Msg");
390
- await log.Flush();
391
- expect(logCollector.Logs()).toEqual([]);
392
-
393
- xxxModule.EnableLevel(Level.DEBUG, "yyy", "xxx");
394
-
395
- const yyyModule = log.With().Module("yyy").Logger();
396
- yyyModule.Debug().Msg("yyy-log-Msg");
397
-
398
- xxxModule.Debug().Msg("xxx-log-Msg1");
399
-
400
- log.Debug().Msg("log-Msg1");
401
-
402
- yyyModule.DisableLevel(Level.DEBUG, "yyy");
403
- yyyModule.Debug().Msg("yyy-log-Msg1");
404
-
405
- xxxModule.Debug().Msg("xxx-log-Msg2");
406
-
407
- log.Debug().Msg("log-Msg3");
408
-
409
- await log.Flush();
410
- expect(logCollector.Logs()).toEqual([
411
- {
412
- level: "debug",
413
- module: "yyy",
414
- msg: "yyy-log-Msg",
415
- },
416
- {
417
- level: "debug",
418
- module: "xxx",
419
- msg: "xxx-log-Msg1",
420
- },
421
- {
422
- level: "debug",
423
- module: "xxx",
424
- msg: "xxx-log-Msg2",
425
- },
426
- ]);
427
- });
428
-
429
- it("global Check", () => {
430
- const g1 = new LoggerImpl().EnableLevel(Level.DEBUG) as LoggerImpl;
431
- const g2 = new LoggerImpl();
432
- const g3 = g2.With().Module("X").Logger() as LoggerImpl;
433
- expect(g1._levelHandler).toBe(g2._levelHandler);
434
- expect(g1._levelHandler).toBe(g3._levelHandler);
435
- expect((g1._levelHandler as LevelHandlerImpl)._globalLevels.has(Level.DEBUG)).toBeTruthy();
436
- expect((g2._levelHandler as LevelHandlerImpl)._globalLevels.has(Level.DEBUG)).toBeTruthy();
437
- expect((g3._levelHandler as LevelHandlerImpl)._globalLevels.has(Level.DEBUG)).toBeTruthy();
438
- });
439
-
440
- it("isLogger", () => {
441
- const log = new LoggerImpl();
442
- expect(IsLogger(log)).toBeTruthy();
443
- expect(
444
- IsLogger({
445
- Info: () => log.Info(),
446
- Flush: () => log.Flush(),
447
- With: () => log.With(),
448
- }),
449
- ).toBeFalsy();
450
- });
451
-
452
- it("bool", async () => {
453
- const log = logger;
454
- log.Info().Bool("true", true).Msg("1");
455
- log.Info().Bool("false", false).Msg("2");
456
- log.Info().Bool("true", "wurst").Msg("3");
457
- log.Info().Bool("false", null).Msg("4");
458
- await log.Flush();
459
- expect(logCollector.Logs()).toEqual([
460
- {
461
- level: "info",
462
- msg: "1",
463
- true: true,
464
- },
465
- {
466
- false: false,
467
- level: "info",
468
- msg: "2",
469
- },
470
- {
471
- level: "info",
472
- msg: "3",
473
- true: true,
474
- },
475
- {
476
- false: false,
477
- level: "info",
478
- msg: "4",
479
- },
480
- ]);
481
- });
482
-
483
- it("int", async () => {
484
- const log = logger;
485
- log.Info().Int("1", 1).Msg("1");
486
- log.Info().Int("2", 2).Msg("2");
487
- log.Info().Int("3", 3).Msg("3");
488
- log.Info().Int("4", 4).Msg("4");
489
- await log.Flush();
490
- expect(logCollector.Logs()).toEqual([
491
- {
492
- "1": 1,
493
- level: "info",
494
- msg: "1",
495
- },
496
- {
497
- "2": 2,
498
- level: "info",
499
- msg: "2",
500
- },
501
- {
502
- "3": 3,
503
- level: "info",
504
- msg: "3",
505
- },
506
- {
507
- "4": 4,
508
- level: "info",
509
- msg: "4",
510
- },
511
- ]);
512
- });
513
-
514
- it("int", async () => {
515
- const log = logger;
516
- log.Info().Int("1", 1).Msg("1");
517
- log.Info().Int("2", 2).Msg("2");
518
- log.Info().Int("3", 3).Msg("3");
519
- log.Info().Int("4", 4).Msg("4");
520
- await log.Flush();
521
- expect(logCollector.Logs()).toEqual([
522
- {
523
- "1": 1,
524
- level: "info",
525
- msg: "1",
526
- },
527
- {
528
- "2": 2,
529
- level: "info",
530
- msg: "2",
531
- },
532
- {
533
- "3": 3,
534
- level: "info",
535
- msg: "3",
536
- },
537
- {
538
- "4": 4,
539
- level: "info",
540
- msg: "4",
541
- },
542
- ]);
543
- });
544
-
545
- it("ref", async () => {
546
- const log = logger;
547
- let value = 4711;
548
- const fn = (): string => "" + value++;
549
- log.Info().Ref("1", { toString: fn }).Msg("1");
550
- log.Info().Ref("2", { toString: fn }).Msg("2");
551
- log.Info().Ref("3", fn).Msg("3");
552
- log.Info().Ref("4", fn).Msg("4");
553
- await log.Flush();
554
- expect(logCollector.Logs()).toEqual([
555
- {
556
- "1": "4711",
557
- level: "info",
558
- msg: "1",
559
- },
560
- {
561
- "2": "4712",
562
- level: "info",
563
- msg: "2",
564
- },
565
- {
566
- "3": "4713",
567
- level: "info",
568
- msg: "3",
569
- },
570
- {
571
- "4": "4714",
572
- level: "info",
573
- msg: "4",
574
- },
575
- ]);
576
- });
577
- it("result", async () => {
578
- const log = logger;
579
- log.Info().Result("res.ok", Result.Ok(4711)).Msg("1");
580
- log.Info().Result("res.err", Result.Err("Error")).Msg("2");
581
- await log.Flush();
582
- expect(logCollector.Logs()).toEqual([
583
- {
584
- level: "info",
585
- msg: "1",
586
- "res.ok": 4711,
587
- },
588
- {
589
- error: "Error",
590
- level: "info",
591
- msg: "2",
592
- },
593
- ]);
594
- });
595
- it("url", async () => {
596
- const log = logger;
597
- const url = new MutableURL("http://localhost:8080");
598
- log.Info().Url(url).Msg("1");
599
- url.searchParams.set("test", "1");
600
- log.Info().Url(url).Msg("2");
601
- await log.Flush();
602
- expect(logCollector.Logs()).toEqual([
603
- {
604
- level: "info",
605
- msg: "1",
606
- url: "http://localhost:8080/",
607
- },
608
- {
609
- level: "info",
610
- msg: "2",
611
- url: "http://localhost:8080/?test=1",
612
- },
613
- ]);
614
- });
615
-
616
- it("str", async () => {
617
- const log = logger;
618
- log.Error().Str("1", undefined).Msg("1");
619
- await log.Flush();
620
- expect(logCollector.Logs()).toEqual([
621
- {
622
- "1": "--Falsy--",
623
- level: "error",
624
- msg: "1",
625
- },
626
- ]);
627
- });
628
-
629
- it("len", async () => {
630
- const log = logger;
631
- for (const key of [undefined, "key"]) {
632
- log.Info().Len(undefined, key).Msg("undefined");
633
- log.Info().Len(null, key).Msg("null");
634
- log
635
- .Info()
636
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
637
- .Len(true as any, key)
638
- .Msg("bool");
639
- log
640
- .Info()
641
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
642
- .Len(1 as any, key)
643
- .Msg("number");
644
-
645
- log.Info().Len("string", key).Msg("string");
646
- log
647
- .Info()
648
- .Len(new Uint8Array([1, 2]), key)
649
- .Msg("uint8array");
650
- log
651
- .Info()
652
- .Len(Array.from([1, 2]), key)
653
- .Msg("Array");
654
- log.Info().Len({ a: 1 }, key).Msg("object");
655
- }
656
- await log.Flush();
657
- expect(logCollector.Logs()).toEqual(
658
- Array.from(["len", "key"])
659
- .map((key) => [
660
- {
661
- [key]: -1,
662
- level: "info",
663
- msg: "undefined",
664
- },
665
- {
666
- [key]: -1,
667
- level: "info",
668
- msg: "null",
669
- },
670
- {
671
- [key]: -1,
672
- level: "info",
673
- msg: "bool",
674
- },
675
- {
676
- [key]: -1,
677
- level: "info",
678
- msg: "number",
679
- },
680
- {
681
- [key]: 6,
682
- level: "info",
683
- msg: "string",
684
- },
685
- {
686
- [key]: 2,
687
- level: "info",
688
- msg: "uint8array",
689
- },
690
- {
691
- [key]: 2,
692
- level: "info",
693
- msg: "Array",
694
- },
695
- {
696
- [key]: 1,
697
- level: "info",
698
- msg: "object",
699
- },
700
- ])
701
- .flat(),
702
- );
703
- });
704
-
705
- it("wildcard debug", async () => {
706
- const m1 = logger.With().Module("m1").Logger();
707
- const m2 = logger.With().Module("m2").Logger();
708
-
709
- m1.Debug().Msg("m1");
710
- m2.Debug().Msg("m2");
711
-
712
- logger.SetDebug("*");
713
- m1.Debug().Msg("m3");
714
- m2.Debug().Msg("m4");
715
- await logger.Flush();
716
- expect(logCollector.Logs()).toEqual([
717
- {
718
- level: "debug",
719
- module: "m1",
720
- msg: "m3",
721
- },
722
- {
723
- level: "debug",
724
- module: "m2",
725
- msg: "m4",
726
- },
727
- ]);
728
- });
729
- it("array setDebug could receive anything", () => {
730
- function c(u: unknown): string {
731
- return u as string;
732
- }
733
- logger.SetDebug(c(1), c(true), c(null), c(undefined), "", "test ", "test1, ,test2,,test3,,,more", [
734
- c(2),
735
- c(true),
736
- c(null),
737
- c(undefined),
738
- "",
739
- " testx",
740
- "test1x , , test2x,, test3x ,,,morex",
741
- ]);
742
- expect(Array.from(((logger as LoggerImpl)._levelHandler as LevelHandlerImpl)._modules.keys())).toEqual([
743
- "test",
744
- "test1",
745
- "test2",
746
- "test3",
747
- "more",
748
- "testx",
749
- "test1x",
750
- "test2x",
751
- "test3x",
752
- "morex",
753
- ]);
754
- });
755
- it("object setDebug could receive anything", async () => {
756
- logger
757
- .Error()
758
- .Any("sock", {
759
- m: 1,
760
- nested: {
761
- m: 2,
762
- mfn: logValue(() => 23),
763
- },
764
- mfn: logValue(() => 19),
765
- })
766
- .Msg("1");
767
- await logger.Flush();
768
- expect(logCollector.Logs()).toEqual([
769
- {
770
- level: "error",
771
- msg: "1",
772
- sock: {
773
- m: 1,
774
- mfn: 19,
775
- nested: {
776
- m: 2,
777
- mfn: 23,
778
- },
779
- },
780
- },
781
- ]);
782
- });
783
-
784
- it("don't serialize json on string", async () => {
785
- logger
786
- .Error()
787
- .Err(new Error(JSON.stringify({ o: { h: 1 } })))
788
- .Str("sock", JSON.stringify({ a: { h: 1 } }))
789
- .Str("bla", '{a":1}')
790
- .Msg("1");
791
- await logger.Flush();
792
- expect(logCollector.Logs()).toEqual([
793
- {
794
- level: "error",
795
- error: { o: { h: 1 } },
796
- msg: "1",
797
- sock: { a: { h: 1 } },
798
- bla: '{a":1}',
799
- },
800
- ]);
801
- });
802
-
803
- it("see exposed Stack", async () => {
804
- const e = new Error("test");
805
- logger.Error().Err(e).Msg("1");
806
- logger.SetExposeStack(true);
807
- logger.Error().Err(e).Msg("2");
808
- logger.SetExposeStack(false);
809
- logger.Error().Err(e).Msg("3");
810
- await logger.Flush();
811
- expect(logCollector.Logs()).toEqual([
812
- {
813
- error: "test",
814
- level: "error",
815
- msg: "1",
816
- },
817
- {
818
- error: "test",
819
- level: "error",
820
- msg: "2",
821
- stack: e.stack?.split("\n").map((s) => s.trim()),
822
- },
823
- {
824
- error: "test",
825
- level: "error",
826
- msg: "3",
827
- },
828
- ]);
829
- });
830
-
831
- it("which writer for which runtime", async () => {
832
- const logger = new LoggerImpl();
833
- if (runtimeFn().isNodeIsh) {
834
- expect(logger._logWriter._out instanceof WritableStream).toBeTruthy();
835
- logger.Info().Msg("Running in Node");
836
- }
837
- if (runtimeFn().isBrowser) {
838
- expect(logger._logWriter._out.constructor.name).toBe("ConsoleWriterStream");
839
- logger.Info().Msg("Running in Browser");
840
- }
841
- });
842
-
843
- it("self-ref", async () => {
844
- const nested: Record<string, unknown> = {
845
- m: 2,
846
- mfn: logValue(() => 23),
847
- };
848
- nested.flat = nested;
849
- nested.layer = {
850
- jo: 4,
851
- boom: nested,
852
- };
853
- logger
854
- .Error()
855
- .Any("sock", {
856
- m: 1,
857
- nested: nested,
858
- mfn: logValue(() => 19),
859
- })
860
- .Msg("1");
861
- await logger.Flush();
862
- expect(logCollector.Logs()).toEqual([
863
- {
864
- level: "error",
865
- msg: "1",
866
- sock: {
867
- m: 1,
868
- mfn: 19,
869
- nested: {
870
- m: 2,
871
- mfn: 23,
872
- flat: "...",
873
- layer: {
874
- jo: 4,
875
- boom: "...",
876
- },
877
- },
878
- },
879
- },
880
- ]);
881
- });
882
-
883
- it("serialize json as string", async () => {
884
- const suri = "file://./doof?test=1";
885
- const auri = JSON.stringify({ uri: suri });
886
- const buri = BuildURI.from(suri);
887
- const uri = URI.from(suri);
888
- expect(JSON.stringify({ uri: buri })).toEqual(auri);
889
- expect(JSON.stringify({ uri })).toEqual(auri);
890
- });
891
-
892
- it("emits attributes", async () => {
893
- const log = logger
894
- .With()
895
- .Str("str", "a str")
896
- .Ref("bla", () => "blub")
897
- .Any("what", { a: 1 })
898
- .Logger();
899
- expect(log.Attributes()).toEqual({ str: "a str", what: { a: 1 }, bla: "blub" });
900
-
901
- const tlog = log.With().Timestamp().Logger();
902
- const refTime = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
903
- expect(tlog.Attributes()).toEqual({
904
- str: "a str",
905
- what: { a: 1 },
906
- bla: "blub",
907
- ts: refTime.Now().toISOString(),
908
- });
909
- });
910
- it("Url could receive URL", async () => {
911
- logger.Info().Url(new URL("http://localhost:8080")).Msg("1");
912
- await logger.Flush();
913
- expect(logCollector.Logs()).toEqual([
914
- {
915
- level: "info",
916
- msg: "1",
917
- url: "http://localhost:8080/",
918
- },
919
- ]);
920
- });
921
-
922
- it("Url could receive String", async () => {
923
- logger.Info().Url("http://localhost:8080").Msg("1");
924
- await logger.Flush();
925
- expect(logCollector.Logs()).toEqual([
926
- {
927
- level: "info",
928
- msg: "1",
929
- url: "http://localhost:8080/",
930
- },
931
- ]);
932
- });
933
-
934
- it("error could receive Result", async () => {
935
- logger.Info().Error().Err(Result.Err("xxxxx")).Msg("1");
936
- logger.Info().Error().Err(Result.Ok("yyyyy")).Msg("2");
937
- await logger.Flush();
938
- expect(logCollector.Logs()).toEqual([
939
- {
940
- error: "xxxxx",
941
- level: "error",
942
- msg: "1",
943
- },
944
- {
945
- noerror: "yyyyy",
946
- level: "error",
947
- msg: "2",
948
- },
949
- ]);
950
- });
951
-
952
- it("introspect json", async () => {
953
- logger
954
- .Info()
955
- .Str("bla", JSON.stringify({ a: 4711 }))
956
- .Any("y", {
957
- a: JSON.stringify({ b: 4711, c: '{"d":4711}', e: ['{"f":4712}'] }),
958
- })
959
- .Msg(JSON.stringify(["x", 4712, { a: 4711 }, '{"d":4711}', '{"a":4711}']));
960
- await logger.Flush();
961
- expect(logCollector.Logs()).toEqual([
962
- {
963
- bla: { a: 4711 },
964
- level: "info",
965
- msg: [
966
- "x",
967
- 4712,
968
- {
969
- a: 4711,
970
- },
971
- {
972
- d: 4711,
973
- },
974
- {
975
- a: 4711,
976
- },
977
- ],
978
- y: {
979
- a: {
980
- b: 4711,
981
- c: {
982
- d: 4711,
983
- },
984
- e: [
985
- {
986
- f: 4712,
987
- },
988
- ],
989
- },
990
- },
991
- },
992
- ]);
993
- });
994
-
995
- it("introspect uint8array", async () => {
996
- logger
997
- .Info()
998
- .Any("fhex", new Uint8Array(new Array(36).fill(1).map((_, i) => i)))
999
- .Any("hex", { a: new Uint8Array(new Array(36).fill(1).map((_, i) => i)) })
1000
- .Msg("1");
1001
- await logger.Flush();
1002
- expect(logCollector.Logs()).toEqual([
1003
- {
1004
- fhex: [
1005
- "0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................",
1006
- "0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................",
1007
- '0020 20 21 22 23 !"#',
1008
- ],
1009
- hex: {
1010
- a: [
1011
- "0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................",
1012
- "0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................",
1013
- '0020 20 21 22 23 !"#',
1014
- ],
1015
- },
1016
- level: "info",
1017
- msg: "1",
1018
- },
1019
- ]);
1020
- });
1021
-
1022
- it("my own json formatter", async () => {
1023
- logger.SetExposeStack(true).SetFormatter(new JSONFormatter(logger.TxtEnDe(), 2));
1024
- logger
1025
- .Error()
1026
- .Str("bla", "blub")
1027
- // .Err(new Error("test"))
1028
- .Str("xxx", '{"b": 4711}')
1029
- .Str("lines", "a\nb\nc")
1030
- .Any("flat", new Uint8Array(new Array(36).fill(1).map((_, i) => i)))
1031
- .Any("hi", {
1032
- ho: 1,
1033
- su: "bla",
1034
- js: '{"a":1}',
1035
- bi: new Uint8Array(new Array(36).fill(1).map((_, i) => i)),
1036
- ls: "a\nb\nc",
1037
- })
1038
- .Msg("hello");
1039
- await logger.Flush();
1040
- expect(logCollector.Logs(true)).toEqual([
1041
- "{",
1042
- ' "level": "error",',
1043
- ' "bla": "blub",',
1044
- // " \"error\": \"test\",",
1045
- // " \"stack\": [",
1046
- // " \"Error: test\",",
1047
- // " ],",
1048
- ' "xxx": {',
1049
- ' "b": 4711',
1050
- " },",
1051
- ' "lines": [',
1052
- ' "a",',
1053
- ' "b",',
1054
- ' "c"',
1055
- " ],",
1056
- ' "flat": [',
1057
- ' "0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................",',
1058
- ' "0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................",',
1059
- ' "0020 20 21 22 23 !\\"#"',
1060
- " ],",
1061
- ' "hi": {',
1062
- ' "ho": 1,',
1063
- ' "su": "bla",',
1064
- ' "js": {',
1065
- ' "a": 1',
1066
- " },",
1067
- ' "bi": [',
1068
- ' "0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................",',
1069
- ' "0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................",',
1070
- ' "0020 20 21 22 23 !\\"#"',
1071
- " ],",
1072
-
1073
- ' "ls": [',
1074
- ' "a",',
1075
- ' "b",',
1076
- ' "c"',
1077
- " ]",
1078
- " },",
1079
- ' "msg": "hello"',
1080
- "}",
1081
- ]);
1082
- });
1083
-
1084
- it("my own yaml formatter", async () => {
1085
- const log = logger.SetExposeStack(true).SetFormatter(new YAMLFormatter(logger.TxtEnDe(), 2)).With().Logger();
1086
- log
1087
- .Error()
1088
- .Str("bla", "blub")
1089
- // .Err(new Error("test"))
1090
- .Str("xxx", '{"b": 4711}')
1091
- .Str("lines", "a\nb\nc")
1092
- .Any("flat", new Uint8Array(new Array(36).fill(1).map((_, i) => i)))
1093
- .Any("hi", {
1094
- ho: 1,
1095
- su: "bla",
1096
- js: '{"a":1}',
1097
- bi: new Uint8Array(new Array(36).fill(1).map((_, i) => i)),
1098
- ls: "a\nb\nc",
1099
- })
1100
- .Msg("hello");
1101
- await log.Flush();
1102
- expect(logCollector.Logs(true)).toEqual([
1103
- "---",
1104
- "level: error",
1105
- "bla: blub",
1106
- "xxx:",
1107
- " b: 4711",
1108
- "lines:",
1109
- " - a",
1110
- " - b",
1111
- " - c",
1112
- "flat:",
1113
- " - 0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................",
1114
- " - 0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................",
1115
- ' - 0020 20 21 22 23 !"#',
1116
- "hi:",
1117
- " ho: 1",
1118
- " su: bla",
1119
- " js:",
1120
- " a: 1",
1121
- " bi:",
1122
- " - 0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................",
1123
- " - 0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................",
1124
- ' - 0020 20 21 22 23 !"#',
1125
- " ls:",
1126
- " - a",
1127
- " - b",
1128
- " - c",
1129
- "msg: hello",
1130
- ]);
1131
- });
1132
- });