diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-05-30 14:59:30 +0200 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-05-30 08:59:30 -0400 |
| commit | 50a79584cb12129b3db1ef3e0eb9d0c8b9f20b62 (patch) | |
| tree | ee9a90a8b8018c03b1e1a6ace07abdaa494ea90d /log | |
| parent | 80b3c486f6222f65b52eb2eca903b67312e8ce0c (diff) | |
chore: Implement strict mode (denoland/deno_std#453)
Original: https://github.com/denoland/deno_std/commit/be24677d15494e83eea2e99bfc5ccfdde31cb892
Diffstat (limited to 'log')
| -rw-r--r-- | log/handlers.ts | 8 | ||||
| -rw-r--r-- | log/levels.ts | 13 | ||||
| -rw-r--r-- | log/logger_test.ts | 34 | ||||
| -rw-r--r-- | log/mod.ts | 10 | ||||
| -rw-r--r-- | log/test.ts | 6 |
5 files changed, 27 insertions, 44 deletions
diff --git a/log/handlers.ts b/log/handlers.ts index f13a858e9..1f68e9794 100644 --- a/log/handlers.ts +++ b/log/handlers.ts @@ -40,14 +40,14 @@ export class BaseHandler { return this.formatter.replace( /{(\S+)}/g, (match, p1): string => { - const value = logRecord[p1]; + const value = logRecord[p1 as keyof LogRecord]; // do not interpolate missing values if (!value) { return match; } - return value; + return String(value); } ); } @@ -87,7 +87,7 @@ export class ConsoleHandler extends BaseHandler { } export abstract class WriterHandler extends BaseHandler { - protected _writer: Writer; + protected _writer!: Writer; private _encoder = new TextEncoder(); log(msg: string): void { @@ -100,7 +100,7 @@ interface FileHandlerOptions extends HandlerOptions { } export class FileHandler extends WriterHandler { - private _file: File; + private _file!: File; private _filename: string; constructor(levelName: string, options: FileHandlerOptions) { diff --git a/log/levels.ts b/log/levels.ts index 3fc866161..20cafb205 100644 --- a/log/levels.ts +++ b/log/levels.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -export const LogLevel = { +export const LogLevel: Record<string, number> = { NOTSET: 0, DEBUG: 10, INFO: 20, @@ -8,15 +8,6 @@ export const LogLevel = { CRITICAL: 50 }; -const byName = { - NOTSET: LogLevel.NOTSET, - DEBUG: LogLevel.DEBUG, - INFO: LogLevel.INFO, - WARNING: LogLevel.WARNING, - ERROR: LogLevel.ERROR, - CRITICAL: LogLevel.CRITICAL -}; - const byLevel = { [LogLevel.NOTSET]: "NOTSET", [LogLevel.DEBUG]: "DEBUG", @@ -27,7 +18,7 @@ const byLevel = { }; export function getLevelByName(name: string): number { - return byName[name]; + return LogLevel[name]; } export function getLevelName(level: number): string { diff --git a/log/logger_test.ts b/log/logger_test.ts index eb4e7673d..2aca71d1c 100644 --- a/log/logger_test.ts +++ b/log/logger_test.ts @@ -10,7 +10,7 @@ class TestHandler extends BaseHandler { public records: LogRecord[] = []; handle(record: LogRecord): void { - this.records.push({ ...record, datetime: null }); + this.records.push({ ...record }); super.handle(record); } @@ -38,33 +38,29 @@ test(function customHandler(): void { logger.debug("foo", 1, 2); - assertEquals(handler.records, [ - { - msg: "foo", - args: [1, 2], - datetime: null, - level: LogLevel.DEBUG, - levelName: "DEBUG" - } - ]); + const record = handler.records[0]; + assertEquals(record.msg, "foo"); + assertEquals(record.args, [1, 2]); + assertEquals(record.level, LogLevel.DEBUG); + assertEquals(record.levelName, "DEBUG"); assertEquals(handler.messages, ["DEBUG foo"]); }); test(function logFunctions(): void { - let handler: TestHandler; - - const doLog = (level: string): void => { - handler = new TestHandler(level); + const doLog = (level: string): TestHandler => { + const handler = new TestHandler(level); let logger = new Logger(level, [handler]); logger.debug("foo"); logger.info("bar"); logger.warning("baz"); logger.error("boo"); logger.critical("doo"); + return handler; }; - doLog("DEBUG"); + let handler: TestHandler; + handler = doLog("DEBUG"); assertEquals(handler.messages, [ "DEBUG foo", @@ -74,7 +70,7 @@ test(function logFunctions(): void { "CRITICAL doo" ]); - doLog("INFO"); + handler = doLog("INFO"); assertEquals(handler.messages, [ "INFO bar", @@ -83,15 +79,15 @@ test(function logFunctions(): void { "CRITICAL doo" ]); - doLog("WARNING"); + handler = doLog("WARNING"); assertEquals(handler.messages, ["WARNING baz", "ERROR boo", "CRITICAL doo"]); - doLog("ERROR"); + handler = doLog("ERROR"); assertEquals(handler.messages, ["ERROR boo", "CRITICAL doo"]); - doLog("CRITICAL"); + handler = doLog("CRITICAL"); assertEquals(handler.messages, ["CRITICAL doo"]); }); diff --git a/log/mod.ts b/log/mod.ts index 9149e8604..d294cdc07 100644 --- a/log/mod.ts +++ b/log/mod.ts @@ -50,7 +50,7 @@ export const handlers = { export function getLogger(name?: string): Logger { if (!name) { - return state.loggers.get("default"); + return state.loggers.get("default")!; } if (!state.loggers.has(name)) { @@ -59,7 +59,7 @@ export function getLogger(name?: string): Logger { return logger; } - return state.loggers.get(name); + return state.loggers.get(name)!; } // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -78,10 +78,6 @@ export const error = (msg: string, ...args: any[]): void => export const critical = (msg: string, ...args: any[]): void => getLogger("default").critical(msg, ...args); -export function getHandler(name: string): BaseHandler { - return state.handlers.get(name); -} - export async function setup(config: LogConfig): Promise<void> { state.config = { handlers: { ...DEFAULT_CONFIG.handlers, ...config.handlers }, @@ -118,7 +114,7 @@ export async function setup(config: LogConfig): Promise<void> { handlerNames.forEach( (handlerName): void => { if (state.handlers.has(handlerName)) { - handlers.push(state.handlers.get(handlerName)); + handlers.push(state.handlers.get(handlerName)!); } } ); diff --git a/log/test.ts b/log/test.ts index 710de182f..c42c7ed38 100644 --- a/log/test.ts +++ b/log/test.ts @@ -16,7 +16,8 @@ class TestHandler extends log.handlers.BaseHandler { } test(async function defaultHandlers(): Promise<void> { - const loggers = { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const loggers: { [key: string]: (msg: string, ...args: any[]) => void } = { DEBUG: log.debug, INFO: log.info, WARNING: log.warning, @@ -29,9 +30,8 @@ test(async function defaultHandlers(): Promise<void> { continue; } - const level = LogLevel[levelName]; const logger = loggers[levelName]; - const handler = new TestHandler(level); + const handler = new TestHandler(levelName); await log.setup({ handlers: { |
