diff --git a/log/logger.ts b/log/logger.ts index 05ebc7b9b339..47c888527eaf 100644 --- a/log/logger.ts +++ b/log/logger.ts @@ -167,11 +167,22 @@ export class Logger { return this.#_log(LogLevels.INFO, msg, ...args); } + /** @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. */ warning(msg: () => T, ...args: unknown[]): T | undefined; + /** @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. */ warning(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T; warning( msg: (T extends GenericFunction ? never : T) | (() => T), ...args: unknown[] + ): T | undefined { + return this.warn(msg as () => T, ...args); + } + + warn(msg: () => T, ...args: unknown[]): T | undefined; + warn(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T; + warn( + msg: (T extends GenericFunction ? never : T) | (() => T), + ...args: unknown[] ): T | undefined { return this.#_log(LogLevels.WARNING, msg, ...args); } diff --git a/log/logger_test.ts b/log/logger_test.ts index 11418767dd2a..8027f48d879d 100644 --- a/log/logger_test.ts +++ b/log/logger_test.ts @@ -70,12 +70,12 @@ Deno.test("logFunctions", function () { const logger = new Logger("default", level, { handlers: [handler] }); const debugData = logger.debug("foo"); const infoData = logger.info("bar"); - const warningData = logger.warning("baz"); + const warnData = logger.warn("baz"); const errorData = logger.error("boo"); const criticalData = logger.critical("doo"); assertEquals(debugData, "foo"); assertEquals(infoData, "bar"); - assertEquals(warningData, "baz"); + assertEquals(warnData, "baz"); assertEquals(errorData, "boo"); assertEquals(criticalData, "doo"); return handler; @@ -177,9 +177,9 @@ Deno.test( assertEquals(handler.messages[3], "INFO null"); // number - const data5: number = logger.warning(3); + const data5: number = logger.warn(3); assertEquals(data5, 3); - const data6: number = logger.warning(3, 1); + const data6: number = logger.warn(3, 1); assertEquals(data6, 3); assertEquals(handler.messages[4], "WARNING 3"); assertEquals(handler.messages[5], "WARNING 3"); @@ -217,9 +217,9 @@ Deno.test( assertEquals(handler.messages[13], "INFO Symbol(a)"); // function - const data15: string | undefined = logger.warning(fn); + const data15: string | undefined = logger.warn(fn); assertEquals(data15, "abc"); - const data16: string | undefined = logger.warning(fn, 1); + const data16: string | undefined = logger.warn(fn, 1); assertEquals(data16, "abc"); assertEquals(handler.messages[14], "WARNING abc"); assertEquals(handler.messages[15], "WARNING abc"); diff --git a/log/mod.ts b/log/mod.ts index 6f08f617337c..a00ac0fb70f5 100644 --- a/log/mod.ts +++ b/log/mod.ts @@ -139,7 +139,7 @@ * // additional logger configurations. You can log any data type. * log.debug("Hello world"); * log.info(123456); - * log.warning(true); + * log.warn(true); * log.error({ foo: "bar", fizz: "bazz" }); * log.critical("500 Internal server error"); * @@ -174,7 +174,7 @@ * // 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. + * logger.warn(41256); // logs to both `console` and `file` handlers. * * // get custom logger * logger = log.getLogger("tasks"); @@ -498,8 +498,17 @@ export function info( return getLogger("default").info(msg, ...args); } -/** Log with warning level, using default logger. */ +/** + * @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. + * + * Log with warning level, using default logger. + */ export function warning(msg: () => T, ...args: unknown[]): T | undefined; +/** + * @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. + * + * Log with warning level, using default logger. + */ export function warning( msg: T extends GenericFunction ? never : T, ...args: unknown[] @@ -510,9 +519,26 @@ export function warning( ): T | undefined { // Assist TS compiler with pass-through generic type if (msg instanceof Function) { - return getLogger("default").warning(msg, ...args); + return warn(msg, ...args); + } + return warn(msg, ...args); +} + +/** Log with warning level, using default logger. */ +export function warn(msg: () => T, ...args: unknown[]): T | undefined; +export function warn( + msg: T extends GenericFunction ? never : T, + ...args: unknown[] +): T; +export function warn( + 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").warn(msg, ...args); } - return getLogger("default").warning(msg, ...args); + return getLogger("default").warn(msg, ...args); } /** Log with error level, using default logger. */ diff --git a/log/mod_test.ts b/log/mod_test.ts index 072fd5d46e6d..b0e36ef7a96d 100644 --- a/log/mod_test.ts +++ b/log/mod_test.ts @@ -10,7 +10,7 @@ import { Logger, LogLevels, setup, - warning, + warn, } from "./mod.ts"; import { BaseHandler } from "./handlers.ts"; @@ -41,8 +41,8 @@ Deno.test("default loggers work as expected", function () { const debugResolver: string | undefined = debug(() => "foo"); const infoData: number = info(456, 1, 2, 3); const infoResolver: boolean | undefined = info(() => true); - const warningData: symbol = warning(sym); - const warningResolver: null | undefined = warning(() => null); + const warnData: symbol = warn(sym); + const warnResolver: null | undefined = warn(() => null); const errorData: undefined = error(undefined, 1, 2, 3); const errorResolver: bigint | undefined = error(() => 5n); const criticalData: string = critical("foo"); @@ -51,8 +51,8 @@ Deno.test("default loggers work as expected", function () { assertEquals(debugResolver, undefined); assertEquals(infoData, 456); assertEquals(infoResolver, true); - assertEquals(warningData, sym); - assertEquals(warningResolver, null); + assertEquals(warnData, sym); + assertEquals(warnResolver, null); assertEquals(errorData, undefined); assertEquals(errorResolver, 5n); assertEquals(criticalData, "foo"); diff --git a/log/test.ts b/log/test.ts index 51c9a9fc5e89..9e6d2bdcc740 100644 --- a/log/test.ts +++ b/log/test.ts @@ -23,7 +23,7 @@ Deno.test("defaultHandlers", async function () { } = { DEBUG: log.debug, INFO: log.info, - WARNING: log.warning, + WARNING: log.warn, ERROR: log.error, CRITICAL: log.critical, };