-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
12 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |