deno.land / std@0.166.0 / log / handlers_test.ts

handlers_test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertNotEquals, assertRejects,} from "../testing/asserts.ts";import { getLevelByName, getLevelName, LevelName, LogLevelNames, LogLevels,} from "./levels.ts";import { BaseHandler, FileHandler, RotatingFileHandler } from "./handlers.ts";import { LogRecord } from "./logger.ts";import { existsSync } from "../fs/exists.ts";
const LOG_FILE = "./test_log.file";
class TestHandler extends BaseHandler { public messages: string[] = [];
public override log(str: string) { this.messages.push(str); }}
Deno.test("simpleHandler", function () { const cases = new Map<number, string[]>([ [ LogLevels.DEBUG, [ "DEBUG debug-test", "INFO info-test", "WARNING warning-test", "ERROR error-test", "CRITICAL critical-test", ], ], [ LogLevels.INFO, [ "INFO info-test", "WARNING warning-test", "ERROR error-test", "CRITICAL critical-test", ], ], [ LogLevels.WARNING, ["WARNING warning-test", "ERROR error-test", "CRITICAL critical-test"], ], [LogLevels.ERROR, ["ERROR error-test", "CRITICAL critical-test"]], [LogLevels.CRITICAL, ["CRITICAL critical-test"]], ]);
for (const [testCase, messages] of cases.entries()) { const testLevel = getLevelName(testCase); const handler = new TestHandler(testLevel);
for (const levelName of LogLevelNames) { const level = getLevelByName(levelName as LevelName); handler.handle( new LogRecord({ msg: `${levelName.toLowerCase()}-test`, args: [], level: level, loggerName: "default", }), ); }
assertEquals(handler.level, testCase); assertEquals(handler.levelName, testLevel); assertEquals(handler.messages, messages); }});
Deno.test("testFormatterAsString", function () { const handler = new TestHandler("DEBUG", { formatter: "test {levelName} {msg}", });
handler.handle( new LogRecord({ msg: "Hello, world!", args: [], level: LogLevels.DEBUG, loggerName: "default", }), );
assertEquals(handler.messages, ["test DEBUG Hello, world!"]);});
Deno.test("testFormatterAsStringWithoutSpace", function () { const handler = new TestHandler("DEBUG", { formatter: "test:{levelName}:{msg}", });
handler.handle( new LogRecord({ msg: "Hello, world!", args: [], level: LogLevels.DEBUG, loggerName: "default", }), );
assertEquals(handler.messages, ["test:DEBUG:Hello, world!"]);});
Deno.test("testFormatterWithEmptyMsg", function () { const handler = new TestHandler("DEBUG", { formatter: "test {levelName} {msg}", });
handler.handle( new LogRecord({ msg: "", args: [], level: LogLevels.DEBUG, loggerName: "default", }), );
assertEquals(handler.messages, ["test DEBUG "]);});
Deno.test("testFormatterAsFunction", function () { const handler = new TestHandler("DEBUG", { formatter: (logRecord): string => `fn formatter ${logRecord.levelName} ${logRecord.msg}`, });
handler.handle( new LogRecord({ msg: "Hello, world!", args: [], level: LogLevels.ERROR, loggerName: "default", }), );
assertEquals(handler.messages, ["fn formatter ERROR Hello, world!"]);});
Deno.test({ name: "FileHandler Shouldn't Have Broken line", async fn() { class TestFileHandler extends FileHandler { override flush() { super.flush(); const decoder = new TextDecoder("utf-8"); const data = Deno.readFileSync(LOG_FILE); const text = decoder.decode(data); assertEquals(text.slice(-1), "\n"); }
override async destroy() { await super.destroy(); await Deno.remove(LOG_FILE); } }
const testFileHandler = new TestFileHandler("WARNING", { filename: LOG_FILE, mode: "w", }); await testFileHandler.setup();
for (let i = 0; i < 300; i++) { testFileHandler.handle( new LogRecord({ msg: "The starry heavens above me and the moral law within me.", args: [], level: LogLevels.WARNING, loggerName: "default", }), ); }
await testFileHandler.destroy(); },});
Deno.test({ name: "FileHandler with mode 'w' will wipe clean existing log file", async fn() { const fileHandler = new FileHandler("WARNING", { filename: LOG_FILE, mode: "w", });
await fileHandler.setup(); fileHandler.handle( new LogRecord({ msg: "Hello World", args: [], level: LogLevels.WARNING, loggerName: "default", }), ); await fileHandler.destroy(); const firstFileSize = (await Deno.stat(LOG_FILE)).size;
await fileHandler.setup(); fileHandler.handle( new LogRecord({ msg: "Hello World", args: [], level: LogLevels.WARNING, loggerName: "default", }), ); await fileHandler.destroy(); const secondFileSize = (await Deno.stat(LOG_FILE)).size;
assertEquals(secondFileSize, firstFileSize); Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "FileHandler with mode 'x' will throw if log file already exists", async fn() { const fileHandler = new FileHandler("WARNING", { filename: LOG_FILE, mode: "x", }); Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("hello world"));
await assertRejects(async () => { await fileHandler.setup(); }, Deno.errors.AlreadyExists);
await fileHandler.destroy();
Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "RotatingFileHandler with mode 'w' will wipe clean existing log file and remove others", async fn() { Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("hello world")); Deno.writeFileSync( LOG_FILE + ".1", new TextEncoder().encode("hello world"), ); Deno.writeFileSync( LOG_FILE + ".2", new TextEncoder().encode("hello world"), ); Deno.writeFileSync( LOG_FILE + ".3", new TextEncoder().encode("hello world"), );
const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 50, maxBackupCount: 3, mode: "w", }); await fileHandler.setup(); await fileHandler.destroy();
assertEquals((await Deno.stat(LOG_FILE)).size, 0); assert(!existsSync(LOG_FILE + ".1")); assert(!existsSync(LOG_FILE + ".2")); assert(!existsSync(LOG_FILE + ".3"));
Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "RotatingFileHandler with mode 'x' will throw if any log file already exists", async fn() { Deno.writeFileSync( LOG_FILE + ".3", new TextEncoder().encode("hello world"), ); const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 50, maxBackupCount: 3, mode: "x", }); await assertRejects( async () => { await fileHandler.setup(); }, Deno.errors.AlreadyExists, "Backup log file " + LOG_FILE + ".3 already exists", );
fileHandler.destroy(); Deno.removeSync(LOG_FILE + ".3"); Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "RotatingFileHandler with first rollover, monitor step by step", async fn() { const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 25, maxBackupCount: 3, mode: "w", }); await fileHandler.setup();
fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); // 'ERROR AAA\n' = 10 bytes fileHandler.flush(); assertEquals((await Deno.stat(LOG_FILE)).size, 10); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); fileHandler.flush(); assertEquals((await Deno.stat(LOG_FILE)).size, 20); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); fileHandler.flush(); // Rollover occurred. Log file now has 1 record, rollover file has the original 2 assertEquals((await Deno.stat(LOG_FILE)).size, 10); assertEquals((await Deno.stat(LOG_FILE + ".1")).size, 20); await fileHandler.destroy();
Deno.removeSync(LOG_FILE); Deno.removeSync(LOG_FILE + ".1"); },});
Deno.test({ name: "RotatingFileHandler with first rollover, check all at once", async fn() { const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 25, maxBackupCount: 3, mode: "w", }); await fileHandler.setup();
fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); // 'ERROR AAA\n' = 10 bytes fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), );
await fileHandler.destroy();
assertEquals((await Deno.stat(LOG_FILE)).size, 10); assertEquals((await Deno.stat(LOG_FILE + ".1")).size, 20);
Deno.removeSync(LOG_FILE); Deno.removeSync(LOG_FILE + ".1"); },});
Deno.test({ name: "RotatingFileHandler with all backups rollover", async fn() { Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("original log file")); Deno.writeFileSync( LOG_FILE + ".1", new TextEncoder().encode("original log.1 file"), ); Deno.writeFileSync( LOG_FILE + ".2", new TextEncoder().encode("original log.2 file"), ); Deno.writeFileSync( LOG_FILE + ".3", new TextEncoder().encode("original log.3 file"), );
const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 2, maxBackupCount: 3, mode: "a", }); await fileHandler.setup(); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); // 'ERROR AAA\n' = 10 bytes await fileHandler.destroy();
const decoder = new TextDecoder(); assertEquals(decoder.decode(Deno.readFileSync(LOG_FILE)), "ERROR AAA\n"); assertEquals( decoder.decode(Deno.readFileSync(LOG_FILE + ".1")), "original log file", ); assertEquals( decoder.decode(Deno.readFileSync(LOG_FILE + ".2")), "original log.1 file", ); assertEquals( decoder.decode(Deno.readFileSync(LOG_FILE + ".3")), "original log.2 file", ); assert(!existsSync(LOG_FILE + ".4"));
Deno.removeSync(LOG_FILE); Deno.removeSync(LOG_FILE + ".1"); Deno.removeSync(LOG_FILE + ".2"); Deno.removeSync(LOG_FILE + ".3"); },});
Deno.test({ name: "RotatingFileHandler maxBytes cannot be less than 1", async fn() { await assertRejects( async () => { const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 0, maxBackupCount: 3, mode: "w", }); await fileHandler.setup(); }, Error, "maxBytes cannot be less than 1", ); },});
Deno.test({ name: "RotatingFileHandler maxBackupCount cannot be less than 1", async fn() { await assertRejects( async () => { const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 50, maxBackupCount: 0, mode: "w", }); await fileHandler.setup(); }, Error, "maxBackupCount cannot be less than 1", ); },});
Deno.test({ name: "Window unload flushes buffer", async fn() { const fileHandler = new FileHandler("WARNING", { filename: LOG_FILE, mode: "w", }); await fileHandler.setup(); fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), ); // 'ERROR AAA\n' = 10 bytes
assertEquals((await Deno.stat(LOG_FILE)).size, 0); dispatchEvent(new Event("unload")); dispatchEvent(new Event("unload")); assertEquals((await Deno.stat(LOG_FILE)).size, 10);
Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "RotatingFileHandler: rotate on byte length, not msg length", async fn() { const fileHandler = new RotatingFileHandler("WARNING", { filename: LOG_FILE, maxBytes: 7, maxBackupCount: 1, mode: "w", }); await fileHandler.setup();
const msg = "。"; const msgLength = msg.length; const msgByteLength = new TextEncoder().encode(msg).byteLength; assertNotEquals(msgLength, msgByteLength); assertEquals(msgLength, 1); assertEquals(msgByteLength, 3);
fileHandler.log(msg); // logs 4 bytes (including '\n') fileHandler.log(msg); // max bytes is 7, but this would be 8. Rollover.
await fileHandler.destroy();
const fileSize1 = (await Deno.stat(LOG_FILE)).size; const fileSize2 = (await Deno.stat(LOG_FILE + ".1")).size;
assertEquals(fileSize1, msgByteLength + 1); assertEquals(fileSize2, msgByteLength + 1);
Deno.removeSync(LOG_FILE); Deno.removeSync(LOG_FILE + ".1"); },});
Deno.test({ name: "FileHandler: Critical logs trigger immediate flush", async fn() { const fileHandler = new FileHandler("WARNING", { filename: LOG_FILE, mode: "w", }); await fileHandler.setup();
fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.ERROR, loggerName: "default", }), );
// ERROR won't trigger immediate flush const fileSize = (await Deno.stat(LOG_FILE)).size; assertEquals(fileSize, 0);
fileHandler.handle( new LogRecord({ msg: "AAA", args: [], level: LogLevels.CRITICAL, loggerName: "default", }), );
// CRITICAL will trigger immediate flush const fileSize2 = (await Deno.stat(LOG_FILE)).size; // ERROR record is 10 bytes, CRITICAL is 13 bytes assertEquals(fileSize2, 23);
await fileHandler.destroy(); Deno.removeSync(LOG_FILE); },});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2448

Version Info

Tagged at
2 months ago