From 6d5ea58cb41c27515a19a63602e8160d18355bef Mon Sep 17 00:00:00 2001 From: Harish V Date: Fri, 23 Feb 2024 01:53:29 +0800 Subject: [PATCH] simplify logger --- src/logger/logger.ts | 71 ++++++++------------------------------------ 1 file changed, 12 insertions(+), 59 deletions(-) diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 60bada2b6..70d00d4d8 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -1,89 +1,42 @@ -import moment from "moment-timezone" - -import { config } from "@root/config/config" - import CloudWatchLogger from "./cloudwatch.logger" import { consoleLogger } from "./console.logger" -import { - Loggable, - ExtendedLogger, - LogMethod, - LoggerVariants, - WithDebug, - WithFatal, -} from "./logger.types" +import { ExtendedLogger, LoggerVariants } from "./logger.types" -const NODE_ENV = config.get("env") +const NODE_ENV = process.env.NODE_ENV || "development" const useCloudwatchLogger = NODE_ENV === "prod" || NODE_ENV === "vapt" || NODE_ENV === "staging" const useConsoleLogger = !(NODE_ENV === "test") -const timestampGenerator = () => - moment().tz("Asia/Singapore").format("YYYY-MM-DD HH:mm:ss") - export class IsomerLogger implements ExtendedLogger { private loggers: LoggerVariants[] constructor() { this.loggers = [] + if (useConsoleLogger) this.use(consoleLogger) + if (useCloudwatchLogger) this.use(new CloudWatchLogger()) } - private getStructuredMessage = ( - level: string, - message: Loggable, - additionalData?: object - ): object => ({ - timestamp: timestampGenerator(), - level, - message: typeof message === "string" ? message : JSON.stringify(message), - ...additionalData, - }) - - private emitLog( - level: string, - message: Loggable, - additionalData?: object - ): void { - // Directly use the message if it's an object, otherwise, generate the structured message - const structuredMessage = - typeof message === "object" && !additionalData - ? message // If message is already an object and there's no additionalData, use it directly. - : this.getStructuredMessage(level, message, additionalData) // Otherwise, generate a structured message. - + private emitLog(level: string, message: string | object): void { this.loggers.forEach((logger: any) => { if (typeof logger[level] === "function") { - logger[level](structuredMessage) + logger[level](message) } }) } - info = (message: Loggable, additionalData?: object): void => { - this.emitLog("info", message, additionalData) - } + info = (message: string | object): void => this.emitLog("info", message) - warn = (message: Loggable, additionalData?: object): void => { - this.emitLog("warn", message, additionalData) - } + warn = (message: string | object): void => this.emitLog("warn", message) - error = (message: Loggable, additionalData?: object): void => { - this.emitLog("error", message, additionalData) - } + error = (message: string | object): void => this.emitLog("error", message) - debug = (message: Loggable, additionalData?: object): void => { - this.emitLog("debug", message, additionalData) - } + debug = (message: string | object): void => this.emitLog("debug", message) - fatal = (message: Loggable, additionalData?: object): void => { - this.emitLog("fatal", message, additionalData) - } + fatal = (message: string | object): void => this.emitLog("fatal", message) use(logger: LoggerVariants) { this.loggers.push(logger) } } -const logger = new IsomerLogger() -if (useConsoleLogger) logger.use(consoleLogger) -if (useCloudwatchLogger) logger.use(new CloudWatchLogger()) - -export default logger +export default new IsomerLogger()