Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
feat: introduce Logger.createStatic
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Mar 16, 2022
1 parent b9c2a38 commit a4ce6b6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 17 deletions.
2 changes: 2 additions & 0 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/SigninState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
13 changes: 7 additions & 6 deletions src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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];
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
103 changes: 95 additions & 8 deletions src/utils/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ beforeEach(() => {
});

describe("Log", () => {

describe("setLevel", () => {
beforeEach(() => {
Log.setLogger(testLogger);
Expand All @@ -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");
Expand All @@ -109,24 +138,82 @@ 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");
});
});

describe("warn", () => {

it("should work with no config", () => {
subject.warn("test");
});

});

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");
});
});
});
8 changes: 7 additions & 1 deletion src/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a4ce6b6

Please sign in to comment.