From d1956d47f7f2fc10540d280a48ada7db2f1ffb46 Mon Sep 17 00:00:00 2001 From: Orbit Turner Date: Wed, 22 Nov 2023 19:28:26 +0000 Subject: [PATCH] feat(mantis-cli): :sparkles: LOGGER V1 Logger V1 With Context Added in Utils --- src/actions/some.action.ts | 2 +- src/utils/orbitLogger.helper.ts | 86 +++++++++++++-------------------- 2 files changed, 35 insertions(+), 53 deletions(-) diff --git a/src/actions/some.action.ts b/src/actions/some.action.ts index 88421d5..58ecb8f 100644 --- a/src/actions/some.action.ts +++ b/src/actions/some.action.ts @@ -4,5 +4,5 @@ export const someAction = async () => { // Logic for the action const matarLogger = new OrbitLogger('DSN'); - // matarLogger. + matarLogger.info('Hello World !') }; diff --git a/src/utils/orbitLogger.helper.ts b/src/utils/orbitLogger.helper.ts index 0a5a50d..ff5a732 100644 --- a/src/utils/orbitLogger.helper.ts +++ b/src/utils/orbitLogger.helper.ts @@ -1,58 +1,40 @@ import Logger, { LoggerOptions } from '@ptkdev/logger'; -// Define a general type for logger methods. -type LoggerMethod = (...args: any[]) => void; +export class OrbitLogger { + private logger: Logger; + private context: string; -// Logger interface with dynamic key access to methods. -interface LoggerWithTags { - [key: string]: LoggerMethod; -} + constructor(context: string, options?: LoggerOptions) { + this.context = context; + this.logger = new Logger(options); + } -// Mapping of method names to their 'tag' parameter positions. -interface TagPositionMap { - [methodName: string]: number; -} + debug(message: string, tag?: string) { + this.logger.debug(message, tag || this.context); + } + + info(message: string, tag?: string) { + this.logger.info(message, tag || this.context); + } + + warning(message: string, tag?: string) { + this.logger.warning(message, tag || this.context); + } + + error(message: string, tag?: string) { + this.logger.error(message, tag || this.context); + } + + sponsor(message: string, tag?: string) { + this.logger.sponsor(message, tag || this.context); + } + + stackoverflow(message: string, error_string?: string, tag?: string) { + this.logger.stackoverflow(message, error_string, tag || this.context); + } + + docs(message: string, url?: string, tag?: string) { + this.logger.docs(message, url, tag || this.context); + } -export class OrbitLogger { - private logger: LoggerWithTags; - private context: string; - - // Map specifying the position of the 'tag' parameter in logger methods. - private tagPositionMap: TagPositionMap = { - info: 1, - error: 1, - warning: 1, - sponsor: 1, - stackoverflow: 1, - docs: 2 // 'tag' is the third parameter for the docs method - }; - - constructor(context: string, options?: LoggerOptions) { - this.context = context; - - // Creating a Proxy around the Logger instance. - this.logger = new Proxy(new Logger(options) as unknown as LoggerWithTags, { - get: (target, prop: string) => { - // Get the original function from the logger. - const originalFunction: LoggerMethod | undefined = target[prop]; - - // Check if the property accessed is a function. - if (typeof originalFunction === 'function') { - // Return a new function that modifies the 'tag' parameter. - return (...args: any[]) => { - const tagPosition = this.tagPositionMap[prop]; - // If the 'tag' position is defined, insert or modify the 'tag' parameter. - if (typeof tagPosition === 'number') { - args[tagPosition] = args[tagPosition] ? `${this.context}, ${args[tagPosition]}` : this.context; - } - // Call the original function with modified arguments. - return originalFunction(...args); - }; - } - - // If not a function, return the property as is. - return originalFunction; - } - }); - } }