deno.land / std@0.177.1 / log / handlers.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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { getLevelByName, LevelName, LogLevels } from "./levels.ts";import type { LogRecord } from "./logger.ts";import { blue, bold, red, yellow } from "../fmt/colors.ts";import { exists, existsSync } from "../fs/exists.ts";import { BufWriterSync } from "../io/buf_writer.ts";import type { Writer } from "../types.d.ts";
const DEFAULT_FORMATTER = "{levelName} {msg}";export type FormatterFunction = (logRecord: LogRecord) => string;export type LogMode = "a" | "w" | "x";
export interface HandlerOptions { formatter?: string | FormatterFunction;}
export class BaseHandler { level: number; levelName: LevelName; formatter: string | FormatterFunction;
constructor(levelName: LevelName, options: HandlerOptions = {}) { this.level = getLevelByName(levelName); this.levelName = levelName;
this.formatter = options.formatter || DEFAULT_FORMATTER; }
handle(logRecord: LogRecord) { if (this.level > logRecord.level) return;
const msg = this.format(logRecord); return this.log(msg); }
format(logRecord: LogRecord): string { if (this.formatter instanceof Function) { return this.formatter(logRecord); }
return this.formatter.replace(/{([^\s}]+)}/g, (match, p1): string => { const value = logRecord[p1 as keyof LogRecord];
// do not interpolate missing values if (value == null) { return match; }
return String(value); }); }
log(_msg: string) {} setup() {} destroy() {}}
/** * This is the default logger. It will output color coded log messages to the * console via `console.log()`. */export class ConsoleHandler extends BaseHandler { override format(logRecord: LogRecord): string { let msg = super.format(logRecord);
switch (logRecord.level) { case LogLevels.INFO: msg = blue(msg); break; case LogLevels.WARNING: msg = yellow(msg); break; case LogLevels.ERROR: msg = red(msg); break; case LogLevels.CRITICAL: msg = bold(red(msg)); break; default: break; }
return msg; }
override log(msg: string) { console.log(msg); }}
export abstract class WriterHandler extends BaseHandler { protected _writer!: Writer; #encoder = new TextEncoder();
abstract override log(msg: string): void;}
interface FileHandlerOptions extends HandlerOptions { filename: string; mode?: LogMode;}
/** * This handler will output to a file using an optional mode (default is `a`, * e.g. append). The file will grow indefinitely. It uses a buffer for writing * to file. Logs can be manually flushed with `fileHandler.flush()`. Log * messages with a log level greater than error are immediately flushed. Logs * are also flushed on process completion. * * Behavior of the log modes is as follows: * * - `'a'` - Default mode. Appends new log messages to the end of an existing log * file, or create a new log file if none exists. * - `'w'` - Upon creation of the handler, any existing log file will be removed * and a new one created. * - `'x'` - This will create a new log file and throw an error if one already * exists. * * This handler requires `--allow-write` permission on the log file. */export class FileHandler extends WriterHandler { protected _file: Deno.FsFile | undefined; protected _buf!: BufWriterSync; protected _filename: string; protected _mode: LogMode; protected _openOptions: Deno.OpenOptions; protected _encoder = new TextEncoder(); #unloadCallback = (() => { this.destroy(); }).bind(this);
constructor(levelName: LevelName, options: FileHandlerOptions) { super(levelName, options); this._filename = options.filename; // default to append mode, write only this._mode = options.mode ? options.mode : "a"; this._openOptions = { createNew: this._mode === "x", create: this._mode !== "x", append: this._mode === "a", truncate: this._mode !== "a", write: true, }; }
override setup() { this._file = Deno.openSync(this._filename, this._openOptions); this._writer = this._file; this._buf = new BufWriterSync(this._file);
addEventListener("unload", this.#unloadCallback); }
override handle(logRecord: LogRecord) { super.handle(logRecord);
// Immediately flush if log level is higher than ERROR if (logRecord.level > LogLevels.ERROR) { this.flush(); } }
log(msg: string) { if (this._encoder.encode(msg).byteLength + 1 > this._buf.available()) { this.flush(); } this._buf.writeSync(this._encoder.encode(msg + "\n")); }
flush() { if (this._buf?.buffered() > 0) { this._buf.flush(); } }
override destroy() { this.flush(); this._file?.close(); this._file = undefined; removeEventListener("unload", this.#unloadCallback); }}
interface RotatingFileHandlerOptions extends FileHandlerOptions { maxBytes: number; maxBackupCount: number;}
/** * This handler extends the functionality of the {@linkcode FileHandler} by * "rotating" the log file when it reaches a certain size. `maxBytes` specifies * the maximum size in bytes that the log file can grow to before rolling over * to a new one. If the size of the new log message plus the current log file * size exceeds `maxBytes` then a roll-over is triggered. When a roll-over * occurs, before the log message is written, the log file is renamed and * appended with `.1`. If a `.1` version already existed, it would have been * renamed `.2` first and so on. The maximum number of log files to keep is * specified by `maxBackupCount`. After the renames are complete the log message * is written to the original, now blank, file. * * Example: Given `log.txt`, `log.txt.1`, `log.txt.2` and `log.txt.3`, a * `maxBackupCount` of 3 and a new log message which would cause `log.txt` to * exceed `maxBytes`, then `log.txt.2` would be renamed to `log.txt.3` (thereby * discarding the original contents of `log.txt.3` since 3 is the maximum number * of backups to keep), `log.txt.1` would be renamed to `log.txt.2`, `log.txt` * would be renamed to `log.txt.1` and finally `log.txt` would be created from * scratch where the new log message would be written. * * This handler uses a buffer for writing log messages to file. Logs can be * manually flushed with `fileHandler.flush()`. Log messages with a log level * greater than ERROR are immediately flushed. Logs are also flushed on process * completion. * * Additional notes on `mode` as described above: * * - `'a'` Default mode. As above, this will pick up where the logs left off in * rotation, or create a new log file if it doesn't exist. * - `'w'` in addition to starting with a clean `filename`, this mode will also * cause any existing backups (up to `maxBackupCount`) to be deleted on setup * giving a fully clean slate. * - `'x'` requires that neither `filename`, nor any backups (up to * `maxBackupCount`), exist before setup. * * This handler requires both `--allow-read` and `--allow-write` permissions on * the log files. */export class RotatingFileHandler extends FileHandler { #maxBytes: number; #maxBackupCount: number; #currentFileSize = 0;
constructor(levelName: LevelName, options: RotatingFileHandlerOptions) { super(levelName, options); this.#maxBytes = options.maxBytes; this.#maxBackupCount = options.maxBackupCount; }
override async setup() { if (this.#maxBytes < 1) { this.destroy(); throw new Error("maxBytes cannot be less than 1"); } if (this.#maxBackupCount < 1) { this.destroy(); throw new Error("maxBackupCount cannot be less than 1"); } await super.setup();
if (this._mode === "w") { // Remove old backups too as it doesn't make sense to start with a clean // log file, but old backups for (let i = 1; i <= this.#maxBackupCount; i++) { try { await Deno.remove(this._filename + "." + i); } catch (error) { if (!(error instanceof Deno.errors.NotFound)) { throw error; } } } } else if (this._mode === "x") { // Throw if any backups also exist for (let i = 1; i <= this.#maxBackupCount; i++) { if (await exists(this._filename + "." + i)) { this.destroy(); throw new Deno.errors.AlreadyExists( "Backup log file " + this._filename + "." + i + " already exists", ); } } } else { this.#currentFileSize = (await Deno.stat(this._filename)).size; } }
override log(msg: string) { const msgByteLength = this._encoder.encode(msg).byteLength + 1;
if (this.#currentFileSize + msgByteLength > this.#maxBytes) { this.rotateLogFiles(); this.#currentFileSize = 0; }
super.log(msg);
this.#currentFileSize += msgByteLength; }
rotateLogFiles() { this._buf.flush(); this._file!.close();
for (let i = this.#maxBackupCount - 1; i >= 0; i--) { const source = this._filename + (i === 0 ? "" : "." + i); const dest = this._filename + "." + (i + 1);
if (existsSync(source)) { Deno.renameSync(source, dest); } }
this._file = Deno.openSync(this._filename, this._openOptions); this._writer = this._file; this._buf = new BufWriterSync(this._file); }}
std

Version Info

Tagged at
11 months ago