Skip to content

Commit

Permalink
simplify logger
Browse files Browse the repository at this point in the history
  • Loading branch information
harishv7 committed Feb 22, 2024
1 parent 50086ba commit 6d5ea58
Showing 1 changed file with 12 additions and 59 deletions.
71 changes: 12 additions & 59 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6d5ea58

Please sign in to comment.