Skip to content

Commit

Permalink
logging.ts: Fix args handling, and invert LogLevel check.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Griffin committed Dec 9, 2024
1 parent 9cffc75 commit f394920
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
29 changes: 28 additions & 1 deletion __tests__/unit/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ describe("logging", () => {

describe("levels", () => {
describe("TRACE log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.DEBUG);
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.TRACE);
it("always log", () => {
logger.trace("Should log trace");
logger.debug("Should log debug");
logger.warn("Should log warnings");
logger.error("Should log errors");
logger.fatal("Should log fatal");
});
});
describe("DEBUG log level", () => {
Expand All @@ -68,6 +69,32 @@ describe("levels", () => {
});
});
});
describe("FATAL log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.FATAL);
it("skipped", () => {
logger.trace("Should not log", "trace");
logger.debug("Should not log", "debug");
logger.info("Should not log", "info");
logger.warn("Should not log", "warn");
logger.error("Should not log", "error");
});
it("logged", () => {
logger.fatal("Should log (%s substitution!)", "with");
logger.fatal("Should log without substitution!");
});
});

describe("OFF log level", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.OFF);
it("skipped", () => {
logger.trace("Should not log", "trace");
logger.debug("Should not log", "debug");
logger.info("Should not log", "info");
logger.warn("Should not log", "warn");
logger.error("Should not log", "error");
logger.fatal("Shoiuld not log", "fatal");
});
});

describe("Log messages", () => {
const logger: LogHandler = new ConsoleLogHandler(LOG_LEVELS.TRACE);
Expand Down
26 changes: 13 additions & 13 deletions src/util/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,38 @@ export class ConsoleLogHandler implements LogHandler {
}

trace(msg: string, ...args: any[]): void {
if (this.#level >= LOG_LEVELS.TRACE) {
console.trace(msg, args);
if (this.#level <= LOG_LEVELS.TRACE) {
console.trace(msg, ...args);
}
}

debug(msg: string, ...args: any[]): void {
if (this.#level >= LOG_LEVELS.DEBUG) {
console.debug(msg, args);
if (this.#level <= LOG_LEVELS.DEBUG) {
console.debug(msg, ...args);
}
}

info(msg: string, ...args: any[]): void {
if (this.#level >= LOG_LEVELS.INFO) {
console.info(msg, args);
if (this.#level <= LOG_LEVELS.INFO) {
console.info(msg, ...args);
}
}

warn(msg: string, ...args: any[]): void {
if (this.#level >= LOG_LEVELS.WARN) {
console.warn(msg, args);
if (this.#level <= LOG_LEVELS.WARN) {
console.warn(msg, ...args);
}
}

error(msg: string, ...args: any[]): void {
if (this.#level >= LOG_LEVELS.ERROR) {
console.error(msg, args);
if (this.#level <= LOG_LEVELS.ERROR) {
console.error(msg, ...args);
}
}

fatal(msg: string, args?: any[]): void {
if (this.#level >= LOG_LEVELS.FATAL) {
console.error(msg, args);
fatal(msg: string, ...args: any[]): void {
if (this.#level <= LOG_LEVELS.FATAL) {
console.error(msg, ...args);
}
}
}

0 comments on commit f394920

Please sign in to comment.