diff options
Diffstat (limited to 'std/log')
-rw-r--r-- | std/log/README.md | 26 | ||||
-rw-r--r-- | std/log/handlers.ts | 19 | ||||
-rw-r--r-- | std/log/logger.ts | 8 | ||||
-rw-r--r-- | std/log/mod.ts | 18 |
4 files changed, 46 insertions, 25 deletions
diff --git a/std/log/README.md b/std/log/README.md index 6abe547d5..613e69922 100644 --- a/std/log/README.md +++ b/std/log/README.md @@ -62,11 +62,14 @@ unknownLogger.info("foobar"); // no-op ### Loggers -Loggers are objects that you interact with. When you use logger method it constructs a `LogRecord` and passes it down to its handlers for output. To create custom loggers speficify them in `loggers` when calling `log.setup`. +Loggers are objects that you interact with. When you use logger method it +constructs a `LogRecord` and passes it down to its handlers for output. To +create custom loggers speficify them in `loggers` when calling `log.setup`. #### `LogRecord` -`LogRecord` is an object that encapsulates provided message and arguments as well some meta data that can be later used when formatting a message. +`LogRecord` is an object that encapsulates provided message and arguments as +well some meta data that can be later used when formatting a message. ```ts interface LogRecord { @@ -80,7 +83,10 @@ interface LogRecord { ### Handlers -Handlers are responsible for actual output of log messages. When handler is called by logger it firstly checks that `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. +Handlers are responsible for actual output of log messages. When handler is +called by logger it firstly checks that `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. `log` module comes with two built-in handlers: @@ -89,7 +95,10 @@ Handlers are responsible for actual output of log messages. When handler is call #### 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. +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. Eg. @@ -130,12 +139,15 @@ log.debug("Hello, world!", 1, "two", [3, 4, 5]); #### Custom handlers -Custom handlers can be implemented by subclassing `BaseHandler` or `WriterHandler`. +Custom handlers can be implemented by subclassing `BaseHandler` or +`WriterHandler`. `BaseHandler` is bare-bones handler that has no output logic at all, -`WriterHandler` is an abstract class that supports any target with `Writer` interface. +`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. +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 `FileHandler` and `TestHandler`. diff --git a/std/log/handlers.ts b/std/log/handlers.ts index 93bdd3edd..5dfd0caa4 100644 --- a/std/log/handlers.ts +++ b/std/log/handlers.ts @@ -37,16 +37,19 @@ export class BaseHandler { return this.formatter(logRecord); } - return this.formatter.replace(/{(\S+)}/g, (match, p1): string => { - const value = logRecord[p1 as keyof LogRecord]; + return this.formatter.replace( + /{(\S+)}/g, + (match, p1): string => { + const value = logRecord[p1 as keyof LogRecord]; - // do not interpolate missing values - if (!value) { - return match; - } + // do not interpolate missing values + if (!value) { + return match; + } - return String(value); - }); + return String(value); + } + ); } log(_msg: string): void {} diff --git a/std/log/logger.ts b/std/log/logger.ts index 482743b23..7ef96e15a 100644 --- a/std/log/logger.ts +++ b/std/log/logger.ts @@ -36,9 +36,11 @@ export class Logger { level: level, levelName: getLevelName(level) }; - this.handlers.forEach((handler): void => { - handler.handle(record); - }); + this.handlers.forEach( + (handler): void => { + handler.handle(record); + } + ); } debug(msg: string, ...args: unknown[]): void { diff --git a/std/log/mod.ts b/std/log/mod.ts index 3f34d7f1d..cb166376e 100644 --- a/std/log/mod.ts +++ b/std/log/mod.ts @@ -80,9 +80,11 @@ export async function setup(config: LogConfig): Promise<void> { }; // tear down existing handlers - state.handlers.forEach((handler): void => { - handler.destroy(); - }); + state.handlers.forEach( + (handler): void => { + handler.destroy(); + } + ); state.handlers.clear(); // setup handlers @@ -104,11 +106,13 @@ export async function setup(config: LogConfig): Promise<void> { const handlerNames = loggerConfig.handlers || []; const handlers: BaseHandler[] = []; - handlerNames.forEach((handlerName): void => { - if (state.handlers.has(handlerName)) { - handlers.push(state.handlers.get(handlerName)!); + handlerNames.forEach( + (handlerName): void => { + if (state.handlers.has(handlerName)) { + handlers.push(state.handlers.get(handlerName)!); + } } - }); + ); const levelName = loggerConfig.level || DEFAULT_LEVEL; const logger = new Logger(levelName, handlers); |