Skip to content

Commit

Permalink
refactor: remove isDebugMode from logger in favour of minimumLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekyEggo committed Apr 17, 2024
1 parent 7b41577 commit 61149ac
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 41 deletions.
37 changes: 16 additions & 21 deletions src/common/logging/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe("Logger", () => {
it("clones options on construction", () => {
// Arrange.
const options: LoggerOptions = {
isDebugMode: false,
level: LogLevel.ERROR,
targets: [{ write: jest.fn() }]
};
Expand Down Expand Up @@ -37,7 +36,6 @@ describe("Logger", () => {
it("writes to all targets", () => {
// Arrange.
const options: LoggerOptions = {
isDebugMode: false,
level: LogLevel.INFO,
targets: [{ write: jest.fn() }, { write: jest.fn() }, { write: jest.fn() }]
};
Expand Down Expand Up @@ -91,8 +89,8 @@ describe("Logger", () => {
// Arrange.
const target = { write: jest.fn() };
const parent = new Logger({
isDebugMode: true,
level: LogLevel.TRACE,
minimumLevel: LogLevel.TRACE,
targets: [target]
});

Expand Down Expand Up @@ -229,8 +227,8 @@ describe("Logger", () => {
// Arrange.
const target = { write: jest.fn() };
const logger = new Logger({
isDebugMode: true,
level,
minimumLevel: LogLevel.TRACE,
targets: [target]
});

Expand All @@ -249,7 +247,6 @@ describe("Logger", () => {
it("inherited by scoped loggers", () => {
// Arrange.
const parent = new Logger({
isDebugMode: false,
level: LogLevel.ERROR,
targets: [{ write: jest.fn() }]
});
Expand All @@ -274,7 +271,6 @@ describe("Logger", () => {
it("inherited from parents with defined log-level", () => {
// Arrange.
const parent = new Logger({
isDebugMode: false,
level: LogLevel.ERROR,
targets: [{ write: jest.fn() }]
});
Expand All @@ -298,7 +294,6 @@ describe("Logger", () => {
it("defaults when set to undefined", () => {
// Arrange.
const parent = new Logger({
isDebugMode: false,
level: LogLevel.ERROR,
targets: [{ write: jest.fn() }]
});
Expand All @@ -324,61 +319,61 @@ describe("Logger", () => {
describe("log-level validation", () => {
const testCases = [
{
isDebugMode: false,
minimumLevel: LogLevel.INFO,
name: "Can be ERROR",
level: LogLevel.ERROR,
expected: LogLevel.ERROR
},
{
isDebugMode: true,
minimumLevel: LogLevel.TRACE,
name: "Can be ERROR",
level: LogLevel.ERROR,
expected: LogLevel.ERROR
},
{
isDebugMode: false,
minimumLevel: LogLevel.INFO,
name: "Can be WARN",
level: LogLevel.WARN,
expected: LogLevel.WARN
},
{
isDebugMode: true,
minimumLevel: LogLevel.TRACE,
name: "Can be WARN",
level: LogLevel.WARN,
expected: LogLevel.WARN
},
{
isDebugMode: false,
minimumLevel: LogLevel.INFO,
name: "Can be INFO",
level: LogLevel.INFO,
expected: LogLevel.INFO
},
{
isDebugMode: true,
minimumLevel: LogLevel.TRACE,
name: "Can be INFO",
level: LogLevel.INFO,
expected: LogLevel.INFO
},
{
isDebugMode: false,
minimumLevel: LogLevel.INFO,
name: "Cannot be DEBUG",
level: LogLevel.DEBUG,
expected: LogLevel.INFO
},
{
isDebugMode: true,
minimumLevel: LogLevel.TRACE,
name: "Can be DEBUG",
level: LogLevel.DEBUG,
expected: LogLevel.DEBUG
},
{
isDebugMode: false,
minimumLevel: LogLevel.INFO,
name: "Cannot be TRACE",
level: LogLevel.TRACE,
expected: LogLevel.INFO
},
{
isDebugMode: true,
minimumLevel: LogLevel.TRACE,
name: "Can be TRACE",
level: LogLevel.TRACE,
expected: LogLevel.TRACE
Expand All @@ -389,11 +384,11 @@ describe("Logger", () => {
* Asserts the {@link Logger} validates the {@link LogLevel} on construction.
*/
describe("construction", () => {
it.each(testCases)("$name when isDebugMode is $isDebugMode", ({ level, expected, isDebugMode }) => {
it.each(testCases)("$name when minimumLevel is $minimumLevel", ({ level, expected, minimumLevel }) => {
// Arrange.
const options: LoggerOptions = {
isDebugMode,
level,
minimumLevel: minimumLevel as LogLevel.INFO | LogLevel.TRACE,
targets: [{ write: jest.fn() }]
};

Expand All @@ -420,11 +415,11 @@ describe("Logger", () => {
* Asserts {@link Logger.setLogLevel} validates teh {@link LogLevel}.
*/
describe("setLevel", () => {
it.each(testCases)("$name when isDebugMode is $isDebugMode", ({ level, expected, isDebugMode }) => {
it.each(testCases)("$name when minimumLevel is $minimumLevel", ({ level, expected, minimumLevel }) => {
// Arrange.
const options: LoggerOptions = {
isDebugMode,
level: LogLevel.ERROR,
minimumLevel: minimumLevel as LogLevel.INFO | LogLevel.TRACE,
targets: [{ write: jest.fn() }]
};

Expand Down
1 change: 0 additions & 1 deletion src/common/logging/__tests__/routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe("registerCreateLogEntryRoute", () => {

const logger = new Logger({
level: LogLevel.INFO,
isDebugMode: true,
targets: [{ write: jest.fn() }]
});

Expand Down
2 changes: 1 addition & 1 deletion src/common/logging/console-target.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogLevel } from ".";
import { LogLevel } from "./level";
import type { LogEntry, LogTarget } from "./target";

/**
Expand Down
14 changes: 7 additions & 7 deletions src/common/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Logger {
/**
* Options that define the loggers behavior.
*/
private readonly options: LoggerOptions;
private readonly options: LoggerOptions & Required<Pick<LoggerOptions, "minimumLevel">>;

/**
* Scope associated with this {@link Logger}.
Expand All @@ -25,7 +25,7 @@ export class Logger {
* @param opts Options that define the loggers behavior.
*/
constructor(opts: LoggerOptions) {
this.options = { ...opts };
this.options = { minimumLevel: LogLevel.TRACE, ...opts };
this.scope = this.options.scope === undefined || this.options.scope.trim() === "" ? "" : this.options.scope;

if (typeof this.options.level !== "function") {
Expand Down Expand Up @@ -96,7 +96,7 @@ export class Logger {
* @returns This instance for chaining.
*/
public setLevel(level?: LogLevel): this {
if ((level === LogLevel.DEBUG || level === LogLevel.TRACE) && !this.options.isDebugMode) {
if (level !== undefined && level > this.options.minimumLevel) {
this._level = LogLevel.INFO;
this.warn(`Log level cannot be set to ${LogLevel[level]} whilst not in debug mode.`);
} else {
Expand Down Expand Up @@ -143,14 +143,14 @@ export class Logger {
*/
export type LoggerOptions = {
/**
* Indicates whether the current context is running in debug mode.
* Determines the minimum level of logs that can be written.
*/
isDebugMode: boolean;
level: LogLevel | (() => LogLevel);

/**
* Determines the minimum level of logs that can be written.
* Minimum level the logger can be set to.
*/
level: LogLevel | (() => LogLevel);
minimumLevel?: LogLevel.INFO | LogLevel.TRACE;

/**
* Optional value that defines the scope of the logger.
Expand Down
2 changes: 0 additions & 2 deletions src/plugin/__tests__/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("connection", () => {
// Re-import the connection to ensure a fresh state.
beforeEach(async () => {
connectionLogger = new Logger({
isDebugMode: false,
level: LogLevel.TRACE,
targets: [{ write: jest.fn() }]
});
Expand Down Expand Up @@ -299,7 +298,6 @@ describe("connection", () => {
it("logs arguments", () => {
// Arrange
const scopedLogger = new Logger({
isDebugMode: false,
level: LogLevel.TRACE,
targets: [{ write: jest.fn() }]
});
Expand Down
1 change: 0 additions & 1 deletion src/plugin/__tests__/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe("I18nProvider", () => {

beforeEach(() => {
scopedLogger = new Logger({
isDebugMode: false,
level: LogLevel.TRACE,
targets: [{ write: jest.fn }]
});
Expand Down
1 change: 0 additions & 1 deletion src/plugin/logging/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LogLevel, Logger, type LoggerOptions } from "../../../common/logging";

const options: LoggerOptions = {
isDebugMode: false,
level: LogLevel.TRACE,
targets: [{ write: jest.fn() }]
};
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/logging/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ describe("createLogger", () => {
// Assert.
expect(spyOnFileTarget).toHaveBeenCalledTimes(1);
expect(Logger).toHaveBeenCalledWith<[LoggerOptions]>({
isDebugMode: true,
level: LogLevel.DEBUG,
minimumLevel: LogLevel.TRACE,
targets: [expect.any(ConsoleTarget), spyOnFileTarget.mock.instances[0]]
});
});
Expand All @@ -59,8 +59,8 @@ describe("createLogger", () => {

// Assert.
expect(Logger).toHaveBeenCalledWith<[LoggerOptions]>({
isDebugMode: false,
level: LogLevel.INFO,
minimumLevel: LogLevel.INFO,
targets: [spyOnFileTarget.mock.instances[0]]
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ if (isDebugMode()) {
* Logger responsible for capturing log messages.
*/
export const logger = new Logger({
isDebugMode: isDebugMode(),
level: isDebugMode() ? LogLevel.DEBUG : LogLevel.INFO,
minimumLevel: isDebugMode() ? LogLevel.TRACE : LogLevel.INFO,
targets
});

Expand Down
3 changes: 1 addition & 2 deletions src/ui/__tests__/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ describe("Logging", () => {
expect(logger).toBe(spyOnLogger.mock.instances[0]);
expect(spyOnLogger).toHaveBeenCalledTimes(1);
expect(spyOnLogger).toHaveBeenCalledWith<[LoggingModule.LoggerOptions]>({
isDebugMode: true,
level: LoggingModule.LogLevel.TRACE,
level: LoggingModule.LogLevel.DEBUG,
targets: [expect.any(LoggingModule.ConsoleTarget), LoggingModule.createRoutedLogTarget(router)]
});
});
Expand Down
3 changes: 1 addition & 2 deletions src/ui/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { router } from "./plugin";
* Logger responsible for capturing log messages.
*/
export const logger = new Logger({
isDebugMode: true,
level: LogLevel.TRACE,
level: LogLevel.DEBUG,
targets: [new ConsoleTarget(), createRoutedLogTarget(router)]
});

0 comments on commit 61149ac

Please sign in to comment.