Skip to content

Commit

Permalink
Support unknown in logger method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeckem committed Sep 20, 2023
1 parent 8d136fb commit 69c0fcd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-deers-burn.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions src/packages/core/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 23 additions & 9 deletions src/packages/core/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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<Error>).name === "string" &&
typeof (value as Partial<Error>).message === "string"
);
}

0 comments on commit 69c0fcd

Please sign in to comment.