deno.land / std@0.177.1 / log / mod.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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
/** * Logging library with the support for terminal and file outputs. Also provides * interfaces for building custom loggers. * * ## Loggers * * Loggers are objects that you interact with. When you use a logger method it * constructs a `LogRecord` and passes it down to its handlers for output. To * create custom loggers, specify them in `loggers` when calling `log.setup`. * * ## Custom message format * * If you want to override default format of message you can define `formatter` * option for handler. It can be either simple string-based format that uses * `LogRecord` fields or more complicated function-based one that takes `LogRecord` * as argument and outputs string. * * The default log format is `{levelName} {msg}`. * * ## Inline Logging * * Log functions return the data passed in the `msg` parameter. Data is returned * regardless if the logger actually logs it. * * ## Lazy Log Evaluation * * Some log statements are expensive to compute. In these cases, you can use * lazy log evaluation to prevent the computation taking place if the logger * won't log the message. * * > NOTE: When using lazy log evaluation, `undefined` will be returned if the * > resolver function is not called because the logger won't log it. It is an * > antipattern use lazy evaluation with inline logging because the return value * > depends on the current log level. * * ## For module authors * * The authors of public modules can let the users display the internal logs of the * module by using a custom logger: * * ```ts * import { getLogger } from "https://deno.land/std@$STD_VERSION/log/mod.ts"; * * function logger() { * return getLogger("my-awesome-module"); * } * * export function sum(a: number, b: number) { * logger().debug(`running ${a} + ${b}`); * return a + b; * } * * export function mult(a: number, b: number) { * logger().debug(`running ${a} * ${b}`); * return a * b; * } * ``` * * The user of the module can then display the internal logs with: * * ```ts, ignore * import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts"; * import { sum } from "<the-awesome-module>/mod.ts"; * * await log.setup({ * handlers: { * console: new log.handlers.ConsoleHandler("DEBUG"), * }, * * loggers: { * "my-awesome-module": { * level: "DEBUG", * handlers: ["console"], * }, * }, * }); * * sum(1, 2); // prints "running 1 + 2" to the console * ``` * * Please note that, due to the order of initialization of the loggers, the * following won't work: * * ```ts * import { getLogger } from "https://deno.land/std@$STD_VERSION/log/mod.ts"; * * const logger = getLogger("my-awesome-module"); * * export function sum(a: number, b: number) { * logger.debug(`running ${a} + ${b}`); // no message will be logged, because getLogger() was called before log.setup() * return a + b; * } * ``` * * @example * ```ts * import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts"; * * // Simple default logger out of the box. You can customize it * // by overriding logger and handler named "default", or providing * // additional logger configurations. You can log any data type. * log.debug("Hello world"); * log.info(123456); * log.warning(true); * log.error({ foo: "bar", fizz: "bazz" }); * log.critical("500 Internal server error"); * * // custom configuration with 2 loggers (the default and `tasks` loggers). * await log.setup({ * handlers: { * console: new log.handlers.ConsoleHandler("DEBUG"), * * file: new log.handlers.FileHandler("WARNING", { * filename: "./log.txt", * // you can change format of output message using any keys in `LogRecord`. * formatter: "{levelName} {msg}", * }), * }, * * loggers: { * // configure default logger available via short-hand methods above. * default: { * level: "DEBUG", * handlers: ["console", "file"], * }, * * tasks: { * level: "ERROR", * handlers: ["console"], * }, * }, * }); * * let logger; * * // get default logger. * logger = log.getLogger(); * logger.debug("fizz"); // logs to `console`, because `file` handler requires "WARNING" level. * logger.warning(41256); // logs to both `console` and `file` handlers. * * // get custom logger * logger = log.getLogger("tasks"); * logger.debug("fizz"); // won't get output because this logger has "ERROR" level. * logger.error({ productType: "book", value: "126.11" }); // log to `console`. * * // if you try to use a logger that hasn't been configured * // you're good to go, it gets created automatically with level set to 0 * // so no message is logged. * const unknownLogger = log.getLogger("mystery"); * unknownLogger.info("foobar"); // no-op * ``` * * @example * Custom message format example * ```ts * import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts"; * * await log.setup({ * handlers: { * stringFmt: new log.handlers.ConsoleHandler("DEBUG", { * formatter: "[{levelName}] {msg}", * }), * * functionFmt: new log.handlers.ConsoleHandler("DEBUG", { * formatter: (logRecord) => { * let msg = `${logRecord.level} ${logRecord.msg}`; * * logRecord.args.forEach((arg, index) => { * msg += `, arg${index}: ${arg}`; * }); * * return msg; * }, * }), * * anotherFmt: new log.handlers.ConsoleHandler("DEBUG", { * formatter: "[{loggerName}] - {levelName} {msg}", * }), * }, * * loggers: { * default: { * level: "DEBUG", * handlers: ["stringFmt", "functionFmt"], * }, * dataLogger: { * level: "INFO", * handlers: ["anotherFmt"], * }, * }, * }); * * // calling: * log.debug("Hello, world!", 1, "two", [3, 4, 5]); * // results in: [DEBUG] Hello, world! * // output from "stringFmt" handler. * // 10 Hello, world!, arg0: 1, arg1: two, arg3: [3, 4, 5] // output from "functionFmt" formatter. * * // calling: * log.getLogger("dataLogger").error("oh no!"); * // results in: * // [dataLogger] - ERROR oh no! // output from anotherFmt handler. * ``` * * @example * Inline Logging * ```ts * import * as logger from "https://deno.land/std@$STD_VERSION/log/mod.ts"; * * const stringData: string = logger.debug("hello world"); * const booleanData: boolean = logger.debug(true, 1, "abc"); * const fn = (): number => { * return 123; * }; * const resolvedFunctionData: number = logger.debug(fn()); * console.log(stringData); // 'hello world' * console.log(booleanData); // true * console.log(resolvedFunctionData); // 123 * ``` * * @example * Lazy Log Evaluation * ```ts * import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts"; * * await log.setup({ * handlers: { * console: new log.handlers.ConsoleHandler("DEBUG"), * }, * * loggers: { * tasks: { * level: "ERROR", * handlers: ["console"], * }, * }, * }); * * function someExpensiveFn(num: number, bool: boolean) { * // do some expensive computation * } * * // not logged, as debug < error. * const data = log.debug(() => someExpensiveFn(5, true)); * console.log(data); // undefined * ``` * * @module */
import { Logger } from "./logger.ts";import type { GenericFunction } from "./logger.ts";import { BaseHandler, ConsoleHandler, FileHandler, RotatingFileHandler, WriterHandler,} from "./handlers.ts";import { assert } from "../_util/asserts.ts";import type { LevelName } from "./levels.ts";
export { LogLevels } from "./levels.ts";export type { LevelName } from "./levels.ts";export { Logger } from "./logger.ts";export type { LogRecord } from "./logger.ts";export type { FormatterFunction, HandlerOptions, LogMode } from "./handlers.ts";
export class LoggerConfig { level?: LevelName; handlers?: string[];}
export interface LogConfig { handlers?: { [name: string]: BaseHandler; }; loggers?: { [name: string]: LoggerConfig; };}
const DEFAULT_LEVEL = "INFO";const DEFAULT_CONFIG: LogConfig = { handlers: { default: new ConsoleHandler(DEFAULT_LEVEL), },
loggers: { default: { level: DEFAULT_LEVEL, handlers: ["default"], }, },};
const state = { handlers: new Map<string, BaseHandler>(), loggers: new Map<string, Logger>(), config: DEFAULT_CONFIG,};
/** * Handlers are responsible for actual output of log messages. When a handler is * called by a logger, it firstly checks that {@linkcode LogRecord}'s level is * not lower than level of the handler. If level check passes, handlers formats * log record into string and outputs it to target. * * ## Custom handlers * * Custom handlers can be implemented by subclassing {@linkcode BaseHandler} or * {@linkcode WriterHandler}. * * {@linkcode BaseHandler} is bare-bones handler that has no output logic at all, * * {@linkcode WriterHandler} is an abstract class that supports any target with * `Writer` interface. * * During setup async hooks `setup` and `destroy` are called, you can use them * to open and close file/HTTP connection or any other action you might need. * * For examples check source code of {@linkcode FileHandler}` * and {@linkcode TestHandler}. */export const handlers = { BaseHandler, ConsoleHandler, WriterHandler, FileHandler, RotatingFileHandler,};
/** Get a logger instance. If not specified `name`, get the default logger. */export function getLogger(name?: string): Logger { if (!name) { const d = state.loggers.get("default"); assert( d != null, `"default" logger must be set for getting logger without name`, ); return d; } const result = state.loggers.get(name); if (!result) { const logger = new Logger(name, "NOTSET", { handlers: [] }); state.loggers.set(name, logger); return logger; } return result;}
/** Log with debug level, using default logger. */export function debug<T>(msg: () => T, ...args: unknown[]): T | undefined;export function debug<T>( msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;export function debug<T>( msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[]): T | undefined { // Assist TS compiler with pass-through generic type if (msg instanceof Function) { return getLogger("default").debug(msg, ...args); } return getLogger("default").debug(msg, ...args);}
/** Log with info level, using default logger. */export function info<T>(msg: () => T, ...args: unknown[]): T | undefined;export function info<T>( msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;export function info<T>( msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[]): T | undefined { // Assist TS compiler with pass-through generic type if (msg instanceof Function) { return getLogger("default").info(msg, ...args); } return getLogger("default").info(msg, ...args);}
/** Log with warning level, using default logger. */export function warning<T>(msg: () => T, ...args: unknown[]): T | undefined;export function warning<T>( msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;export function warning<T>( msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[]): T | undefined { // Assist TS compiler with pass-through generic type if (msg instanceof Function) { return getLogger("default").warning(msg, ...args); } return getLogger("default").warning(msg, ...args);}
/** Log with error level, using default logger. */export function error<T>(msg: () => T, ...args: unknown[]): T | undefined;export function error<T>( msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;export function error<T>( msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[]): T | undefined { // Assist TS compiler with pass-through generic type if (msg instanceof Function) { return getLogger("default").error(msg, ...args); } return getLogger("default").error(msg, ...args);}
/** Log with critical level, using default logger. */export function critical<T>(msg: () => T, ...args: unknown[]): T | undefined;export function critical<T>( msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;export function critical<T>( msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[]): T | undefined { // Assist TS compiler with pass-through generic type if (msg instanceof Function) { return getLogger("default").critical(msg, ...args); } return getLogger("default").critical(msg, ...args);}
/** Setup logger config. */export function setup(config: LogConfig) { state.config = { handlers: { ...DEFAULT_CONFIG.handlers, ...config.handlers }, loggers: { ...DEFAULT_CONFIG.loggers, ...config.loggers }, };
// tear down existing handlers state.handlers.forEach((handler) => { handler.destroy(); }); state.handlers.clear();
// setup handlers const handlers = state.config.handlers || {};
for (const handlerName in handlers) { const handler = handlers[handlerName]; handler.setup(); state.handlers.set(handlerName, handler); }
// remove existing loggers state.loggers.clear();
// setup loggers const loggers = state.config.loggers || {}; for (const loggerName in loggers) { const loggerConfig = loggers[loggerName]; const handlerNames = loggerConfig.handlers || []; const handlers: BaseHandler[] = [];
handlerNames.forEach((handlerName) => { const handler = state.handlers.get(handlerName); if (handler) { handlers.push(handler); } });
const levelName = loggerConfig.level || DEFAULT_LEVEL; const logger = new Logger(loggerName, levelName, { handlers: handlers }); state.loggers.set(loggerName, logger); }}
setup(DEFAULT_CONFIG);
std

Version Info

Tagged at
10 months ago