From a4ce6b685326e7a8d3773e510d1dd7b1df013b54 Mon Sep 17 00:00:00 2001 From: pamapa Date: Tue, 15 Mar 2022 16:55:14 +0100 Subject: [PATCH] feat: introduce Logger.createStatic --- docs/oidc-client-ts.api.md | 2 + src/SigninState.ts | 2 +- src/State.ts | 13 ++--- src/User.ts | 2 +- src/utils/Logger.test.ts | 103 ++++++++++++++++++++++++++++++++++--- src/utils/Logger.ts | 8 ++- 6 files changed, 113 insertions(+), 17 deletions(-) diff --git a/docs/oidc-client-ts.api.md b/docs/oidc-client-ts.api.md index d09915984..5cc9d9380 100644 --- a/docs/oidc-client-ts.api.md +++ b/docs/oidc-client-ts.api.md @@ -206,6 +206,8 @@ export class Logger { // (undocumented) create(method: string): Logger; // (undocumented) + static createStatic(name: string, staticMethod: string): Logger; + // (undocumented) debug(...args: unknown[]): void; // (undocumented) static debug(name: string, ...args: unknown[]): void; diff --git a/src/SigninState.ts b/src/SigninState.ts index 1ab4f0fd8..203157ed9 100644 --- a/src/SigninState.ts +++ b/src/SigninState.ts @@ -93,7 +93,7 @@ export class SigninState extends State { } public static fromStorageString(storageString: string): SigninState { - Logger.debug("SigninState.fromStorageString"); + Logger.createStatic("SigninState", "fromStorageString"); const data = JSON.parse(storageString); return new SigninState(data); } diff --git a/src/State.ts b/src/State.ts index 53ae3cf8e..b7466a909 100644 --- a/src/State.ts +++ b/src/State.ts @@ -44,15 +44,16 @@ export class State { } public static fromStorageString(storageString: string): State { - Logger.debug("State.fromStorageString"); + Logger.createStatic("State", "fromStorageString"); return new State(JSON.parse(storageString)); } public static async clearStaleState(storage: StateStore, age: number): Promise { + const logger = Logger.createStatic("State", "clearStaleState"); const cutoff = Timer.getEpochTime() - age; const keys = await storage.getAllKeys(); - Logger.debug("State.clearStaleState", "got keys", keys); + logger.debug("got keys", keys); for (let i = 0; i < keys.length; i++) { const key = keys[i]; @@ -63,23 +64,23 @@ export class State { try { const state = State.fromStorageString(item); - Logger.debug("State.clearStaleState", "got item from key: ", key, state.created); + logger.debug("got item from key:", key, state.created); if (state.created <= cutoff) { remove = true; } } catch (err) { - Logger.error("State.clearStaleState", "Error parsing state for key", key, err); + logger.error("Error parsing state for key:", key, err); remove = true; } } else { - Logger.debug("State.clearStaleState", "no item in storage for key: ", key); + logger.debug("no item in storage for key:", key); remove = true; } if (remove) { - Logger.debug("State.clearStaleState", "removed item for key: ", key); + logger.debug("removed item for key:", key); void storage.remove(key); } } diff --git a/src/User.ts b/src/User.ts index c62022496..99a037f5d 100644 --- a/src/User.ts +++ b/src/User.ts @@ -117,7 +117,7 @@ export class User { } public static fromStorageString(storageString: string): User { - Logger.debug("User.fromStorageString"); + Logger.createStatic("User", "fromStorageString"); return new User(JSON.parse(storageString)); } } diff --git a/src/utils/Logger.test.ts b/src/utils/Logger.test.ts index 0650f2231..01f5698bb 100644 --- a/src/utils/Logger.test.ts +++ b/src/utils/Logger.test.ts @@ -18,7 +18,6 @@ beforeEach(() => { }); describe("Log", () => { - describe("setLevel", () => { beforeEach(() => { Log.setLogger(testLogger); @@ -29,78 +28,108 @@ describe("Log", () => { Log.setLevel(Log.NONE); // act + subject.debug("test debug"); subject.info("test info"); subject.warn("test warn"); subject.error("test error"); // assert + expect(testLogger.debug).not.toHaveBeenCalled(); expect(testLogger.info).not.toHaveBeenCalled(); expect(testLogger.warn).not.toHaveBeenCalled(); expect(testLogger.error).not.toHaveBeenCalled(); }); - it("should not log info or warn for ERROR level", () => { + it("should not log debug, info or warn for ERROR level", () => { // arrange Log.setLevel(Log.ERROR); // act + subject.debug("test debug"); subject.info("test info"); subject.warn("test warn"); subject.error("test error"); // assert + expect(testLogger.debug).not.toHaveBeenCalled(); expect(testLogger.info).not.toHaveBeenCalled(); expect(testLogger.warn).not.toHaveBeenCalled(); expect(testLogger.error).toHaveBeenCalled(); }); - it("should not log info for WARN level", () => { + it("should not log debug, info for WARN level", () => { // arrange Log.setLevel(Log.WARN); // act + subject.debug("test debug"); subject.info("test info"); subject.warn("test warn"); subject.error("test error"); // assert + expect(testLogger.debug).not.toHaveBeenCalled(); expect(testLogger.info).not.toHaveBeenCalled(); expect(testLogger.warn).toHaveBeenCalled(); expect(testLogger.error).toHaveBeenCalled(); }); - it("should log to all for INFO level", () => { + it("should not log debug for INFO level", () => { // arrange Log.setLevel(Log.INFO); // act + subject.debug("test debug"); + subject.info("test info"); + subject.warn("test warn"); + subject.error("test error"); + + // assert + expect(testLogger.debug).not.toHaveBeenCalled(); + expect(testLogger.info).toHaveBeenCalled(); + expect(testLogger.warn).toHaveBeenCalled(); + expect(testLogger.error).toHaveBeenCalled(); + }); + + it("should log to all for DEBUG level", () => { + // arrange + Log.setLevel(Log.DEBUG); + + // act + subject.debug("test debug"); subject.info("test info"); subject.warn("test warn"); subject.error("test error"); // assert + expect(testLogger.debug).toHaveBeenCalled(); expect(testLogger.info).toHaveBeenCalled(); expect(testLogger.warn).toHaveBeenCalled(); expect(testLogger.error).toHaveBeenCalled(); }); it("should prevent setting an invalid level", () => { - expect(() => Log.setLevel("foo" as never)).toThrow(Error); + // act + expect(() => Log.setLevel("foo" as never)) + // assert + .toThrow(Error); }); }); describe("setLogger", () => { - it("should use the logger specified", () => { // arrange + Log.setLevel(Log.DEBUG); Log.setLogger(testLogger); // act + subject.debug("test debug"); subject.info("test info"); subject.warn("test warn"); subject.error("test error"); // assert + expect(testLogger.debug).toHaveBeenCalledWith("[name]", "test debug"); expect(testLogger.info).toHaveBeenCalledWith("[name]", "test info"); expect(testLogger.warn).toHaveBeenCalledWith("[name]", "test warn"); expect(testLogger.error).toHaveBeenCalledWith("[name]", "test error"); @@ -109,6 +138,12 @@ describe("Log", () => { }); describe("Logger", () => { + describe("debug", () => { + it("should work with no config", () => { + subject.debug("test"); + }); + }); + describe("info", () => { it("should work with no config", () => { subject.info("test"); @@ -116,7 +151,6 @@ describe("Logger", () => { }); describe("warn", () => { - it("should work with no config", () => { subject.warn("test"); }); @@ -124,9 +158,62 @@ describe("Logger", () => { }); describe("error", () => { - it("should work with no config", () => { subject.error("test"); }); }); + + describe("throw", () => { + it("should work with no config", () => { + // arrange + const error = new Error("test"); + + // act + expect(() => subject.throw(error)) + // assert + .toThrow(Error); + }); + + it("should throw and log to ERROR level", () => { + // arrange + Log.setLogger(testLogger); + const error = new Error("test"); + + // act + expect(() => subject.throw(error)) + // assert + .toThrow(Error); + expect(testLogger.error).toHaveBeenCalledWith("[name]", error); + }); + }); + + describe("create", () => { + it("should return logger and log to DEBUG level", () => { + // arrange + Log.setLevel(Log.DEBUG); + Log.setLogger(testLogger); + + // act + const result = subject.create("method"); + + // assert + expect(result).toBeInstanceOf(Logger); + expect(testLogger.debug).toHaveBeenCalledWith("[name] method:", "begin"); + }); + }); + + describe("createStatic", () => { + it("should return logger and log to DEBUG level", () => { + // arrange + Log.setLevel(Log.DEBUG); + Log.setLogger(testLogger); + + // act + const result = Logger.createStatic("name", "method"); + + // assert + expect(result).toBeInstanceOf(Logger); + expect(testLogger.debug).toHaveBeenCalledWith("[name.method]", "begin"); + }); + }); }); diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index a64d325a5..2e8a82ff6 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -101,9 +101,15 @@ export class Logger { return methodLogger; } + public static createStatic(name: string, staticMethod: string): Logger { + const staticLogger = new Logger(`${name}.${staticMethod}`); + staticLogger.debug("begin"); + return staticLogger; + } + private static _format(name: string, method?: string) { const prefix = `[${name}]`; - return method ? prefix + ` ${method}:` : prefix; + return method ? `${prefix} ${method}:` : prefix; } // helpers for static class methods