Skip to content

Commit

Permalink
feat(mantis-cli): ✨ LOGGER V1
Browse files Browse the repository at this point in the history
Logger V1 With Context Added in Utils
  • Loading branch information
orbitturner committed Nov 22, 2023
1 parent 254cdd6 commit d1956d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/actions/some.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export const someAction = async () => {
// Logic for the action
const matarLogger = new OrbitLogger('DSN');

// matarLogger.
matarLogger.info('Hello World !')
};
86 changes: 34 additions & 52 deletions src/utils/orbitLogger.helper.ts
Original file line number Diff line number Diff line change
@@ -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;
}
});
}
}

0 comments on commit d1956d4

Please sign in to comment.