From 7cb22e53ddbe510317ebf804d31749d4910b8490 Mon Sep 17 00:00:00 2001 From: Michael Beckemeyer Date: Wed, 20 Sep 2023 10:56:25 +0200 Subject: [PATCH] Support unknown in logger method signature --- .changeset/nasty-deers-burn.md | 5 +++++ src/packages/core/Logger.test.ts | 7 +++++++ src/packages/core/Logger.ts | 32 +++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 .changeset/nasty-deers-burn.md diff --git a/.changeset/nasty-deers-burn.md b/.changeset/nasty-deers-burn.md new file mode 100644 index 00000000..41418657 --- /dev/null +++ b/.changeset/nasty-deers-burn.md @@ -0,0 +1,5 @@ +--- +"@open-pioneer/core": minor +--- + +The Logger interface now supports `unknown` as message argument. Value types other than `string` or `Error` are converted to strings before logging them. diff --git a/src/packages/core/Logger.test.ts b/src/packages/core/Logger.test.ts index 831129ac..edcb5761 100644 --- a/src/packages/core/Logger.test.ts +++ b/src/packages/core/Logger.test.ts @@ -16,6 +16,13 @@ it("logs complete DEBUG message with additional value", function () { ]); }); +it("logs arbitrary message values formatted as strings", function () { + const logger = new LoggerImpl("test-prefix:LoggerTest", "DEBUG"); + const logSpy = vi.spyOn(console, "debug").mockImplementation(doNothing); + logger.debug(12345); + expect(logSpy).toHaveBeenCalledWith("[DEBUG] test-prefix:LoggerTest: 12345"); +}); + it("logs INFO message", function () { const logger = new LoggerImpl("test-prefix:LoggerTest", "INFO"); const logSpy = vi.spyOn(console, "info").mockImplementation(doNothing); diff --git a/src/packages/core/Logger.ts b/src/packages/core/Logger.ts index 57592ea1..54ac7d1c 100644 --- a/src/packages/core/Logger.ts +++ b/src/packages/core/Logger.ts @@ -31,10 +31,12 @@ export interface LogMethod { /** * Logger's log method signature * - * @param message Log message to be logged (attached to the prefix). Can be a string or an Error. + * @param message + * Log message to be logged (attached to the prefix). This can be an arbitrary value. + * Errors will be logged as-is, and other values are formatted as strings. * @param values Arbitrary amount of additional values to be logged (attached to message). */ - (message: string | Error, ...values: unknown[]): void; + (message: string | Error | unknown, ...values: unknown[]): void; } /** @@ -101,7 +103,7 @@ export class LoggerImpl implements Logger { * @param message * @param values */ - debug(message: string | Error, ...values: unknown[]) { + debug(message: string | Error | unknown, ...values: unknown[]) { this._doLog("DEBUG", message, values); } @@ -110,7 +112,7 @@ export class LoggerImpl implements Logger { * @param message * @param values */ - info(message: string | Error, ...values: unknown[]) { + info(message: string | Error | unknown, ...values: unknown[]) { this._doLog("INFO", message, values); } @@ -119,7 +121,7 @@ export class LoggerImpl implements Logger { * @param message * @param values */ - warn(message: string | Error, ...values: unknown[]) { + warn(message: string | Error | unknown, ...values: unknown[]) { this._doLog("WARN", message, values); } @@ -128,7 +130,7 @@ export class LoggerImpl implements Logger { * @param message * @param values */ - error(message: string | Error, ...values: unknown[]) { + error(message: string | Error | unknown, ...values: unknown[]) { this._doLog("ERROR", message, values); } @@ -139,11 +141,11 @@ export class LoggerImpl implements Logger { * @param values * @private */ - private _doLog(level: LogLevel, messageOrError: string | Error, values: unknown[]) { + private _doLog(level: LogLevel, messageOrError: string | Error | unknown, values: unknown[]) { if (this._isLogLevelEnabled(level)) { let message = `[${level}] ${this.prefix}:`; - if (typeof messageOrError === "string") { - message += " " + messageOrError; + if (!isError(messageOrError)) { + message += " " + String(messageOrError); } else { values.unshift(messageOrError); } @@ -196,3 +198,15 @@ function validateLogLevel(logLevelString: string): LogLevel { ); } } + +function isError(value: unknown): value is Error { + if (value instanceof Error) { + return true; + } + return !!( + value && + typeof value === "object" && + typeof (value as Partial).name === "string" && + typeof (value as Partial).message === "string" + ); +}