Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xkenj1 committed Jul 30, 2024
1 parent 95ed302 commit 06ae64c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
28 changes: 14 additions & 14 deletions packages/shared/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { createLogger, format, transports, Logger as WinstonLogger } from "winston";

import { ILogger, LogLevel } from "./index.js";
import { ILogger } from "./index.js";

type LogLevel = "error" | "warn" | "info" | "debug";

const validLogLevels: LogLevel[] = ["error", "warn", "info", "debug"];

export class Logger implements ILogger {
private logger: WinstonLogger;
private static instance: Logger | null;

private constructor(private level: LogLevel) {
private level: LogLevel;
private constructor() {
this.level = this.isValidLogLevel(process.env.LOG_LEVEL) ? process.env.LOG_LEVEL : "info";
this.logger = createLogger({
level: this.level,
format: format.combine(
format.colorize(),
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
format.errors({ stack: true }),
format.printf(({ level, message, timestamp, stack }) => {
return `${timestamp} ${level}: ${stack || message}`;
return `${timestamp} ${level}: ${stack ?? message ?? ""}`;
}),
),
transports: [new transports.Console()],
Expand All @@ -25,20 +30,15 @@ export class Logger implements ILogger {
* @param level The log level to be used by the logger.
* @returns The instance of the Logger class.
*/
public static getInstance(level?: LogLevel): Logger {
public static getInstance(): Logger {
if (!Logger.instance) {
if (!level) {
throw new Error("Initial configuration is required for the first instantiation.");
}
Logger.instance = new Logger(level);
} else {
Logger.instance.warn(
`Logger instance already exists. Returning the existing instance with log level ${Logger.instance.level}.`,
);
Logger.instance = new Logger();
}

return Logger.instance;
}
isValidLogLevel(level: any): level is LogLevel {
return validLogLevels.includes(level);
}

info(message: string) {
this.logger.info(message);
Expand Down
2 changes: 0 additions & 2 deletions packages/shared/src/types/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ export interface ILogger {
warn: (message: string) => void;
debug: (message: string) => void;
}

export type LogLevel = "error" | "warn" | "info" | "debug";
23 changes: 13 additions & 10 deletions packages/shared/tests/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import { Logger } from "../src/logger.js";

describe("Logger Singleton", () => {
it("creates a logger instance with the given log level", () => {
const logger = Logger.getInstance("info");
const logger = Logger.getInstance();
expect(logger).toBeInstanceOf(Logger);
expect(() => Logger.getInstance()).not.toThrow();
});

it("throws an error if no log level is provided on first instantiation", () => {
Logger["instance"] = null;
expect(() => Logger.getInstance()).toThrow(
new Error("Initial configuration is required for the first instantiation."),
);
});

it("returns the same instance if called multiple times", () => {
const logger1 = Logger.getInstance("info");
const logger2 = Logger.getInstance("warn");
const logger1 = Logger.getInstance();
const logger2 = Logger.getInstance();
expect(logger1).toBe(logger2);
});
it("sets level correctly based on LOG_LEVEL env var", () => {
let logger1 = Logger.getInstance();
expect(logger1["level"]).toEqual("info");

Logger["instance"] = null;
process.env.LOG_LEVEL = "debug";
logger1 = Logger.getInstance();

expect(logger1["level"]).toEqual("debug");
});
});

0 comments on commit 06ae64c

Please sign in to comment.