deno.land / std@0.224.0 / log / file_handler_test.ts

file_handler_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertThrows } from "../assert/mod.ts";import { LogLevels } from "./levels.ts";import { FileHandler } from "./file_handler.ts";import { LogRecord } from "./logger.ts";
const LOG_FILE = "./file_handler_test_log.file";
Deno.test({ name: "FileHandler doesn't have broken line", 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 destroy() { super.destroy(); Deno.removeSync(LOG_FILE); } }
using testFileHandler = new TestFileHandler("WARN", { filename: LOG_FILE, mode: "w", }); 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.WARN, loggerName: "default", }), ); } },});
Deno.test({ name: "FileHandler wipes existing log file clean with mode 'w'", async fn() { const fileHandler = new FileHandler("WARN", { filename: LOG_FILE, mode: "w", });
fileHandler.setup(); fileHandler.handle( new LogRecord({ msg: "Hello World", args: [], level: LogLevels.WARN, loggerName: "default", }), ); fileHandler.destroy(); const firstFileSize = (await Deno.stat(LOG_FILE)).size;
fileHandler.setup(); fileHandler.handle( new LogRecord({ msg: "Hello World", args: [], level: LogLevels.WARN, loggerName: "default", }), ); fileHandler.destroy(); const secondFileSize = (await Deno.stat(LOG_FILE)).size;
assertEquals(secondFileSize, firstFileSize); Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "FileHandler throws if log file already exists with mode 'x'", fn() { using fileHandler = new FileHandler("WARN", { filename: LOG_FILE, mode: "x", }); Deno.writeFileSync(LOG_FILE, new TextEncoder().encode("hello world"));
assertThrows(() => { fileHandler.setup(); }, Deno.errors.AlreadyExists);
Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "FileHandler flushes buffer on unload", async fn() { const fileHandler = new FileHandler("WARN", { filename: LOG_FILE, mode: "w", }); 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: "FileHandler triggers immediate flush on critical logs", async fn() { using fileHandler = new FileHandler("WARN", { filename: LOG_FILE, mode: "w", }); 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);
Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "FileHandler handles strings larger than the buffer", fn() { const fileHandler = new FileHandler("WARN", { filename: LOG_FILE, mode: "w", }); const logOverBufferLimit = "A".repeat(4096); fileHandler.setup();
fileHandler.log(logOverBufferLimit); fileHandler.destroy();
assertEquals( Deno.readTextFileSync(LOG_FILE), `${logOverBufferLimit}\n`, );
Deno.removeSync(LOG_FILE); },});
Deno.test({ name: "FileHandler handles a mixture of string sizes", fn() { const fileHandler = new FileHandler("WARN", { filename: LOG_FILE, mode: "w", }); const veryLargeLog = "A".repeat(10000); const regularLog = "B".repeat(100); fileHandler.setup();
fileHandler.log(regularLog); fileHandler.log(veryLargeLog); fileHandler.log(regularLog); fileHandler.destroy();
assertEquals( Deno.readTextFileSync(LOG_FILE), `${regularLog}\n${veryLargeLog}\n${regularLog}\n`, );
Deno.removeSync(LOG_FILE); },});
std

Version Info

Tagged at
6 months ago