Skip to content

Instantly share code, notes, and snippets.

@JPBM135
Created October 3, 2024 07:16
Show Gist options
  • Save JPBM135/cf1b4cf08cafff228aea66600cea686d to your computer and use it in GitHub Desktop.
Save JPBM135/cf1b4cf08cafff228aea66600cea686d to your computer and use it in GitHub Desktop.
Useful Logger
import { inspect } from "node:util";
import kleur from "kleur";
kleur.enabled = true;
inspect.defaultOptions.depth = 10;
inspect.defaultOptions.maxArrayLength = 100;
interface ILoggerAdapter {
debug(prefix: string, message: string, ...args: any[]): void;
error(prefix: string, message: string, ...args: any[]): void;
info(prefix: string, message: string, ...args: any[]): void;
success(prefix: string, message: string, ...args: any[]): void;
warn(prefix: string, message: string, ...args: any[]): void;
}
export enum LoggerLevel {
Error,
Info,
Debug,
}
export class ReadableAdapter implements ILoggerAdapter {
public success(prefix: string, message: string, ...args: any[]) {
console.log(
`${kleur.green(
`[Success]:` + (prefix ? ` [${prefix}]` : "")
)} ${message}`,
...args
);
}
public info(prefix: string, message: string, ...args: any[]) {
if (Logger.LOGGER_LEVEL < LoggerLevel.Info) return;
console.log(
`${kleur.blue("[Info]:" + (prefix ? ` [${prefix}]` : ""))} ${message}`,
...args
);
}
public debug(prefix: string, message: string, ...args: any[]) {
if (Logger.LOGGER_LEVEL < LoggerLevel.Debug) return;
console.log(
`${kleur.gray("[Debug]:" + (prefix ? ` [${prefix}]` : ""))} ${message}`,
...args
);
}
public warn(prefix: string, message: string, ...args: any[]) {
console.log(
`${kleur.yellow("[Warn]:" + (prefix ? ` [${prefix}]` : ""))} ${message}`,
...args
);
}
public error(prefix: string, message: string, ...args: any[]) {
console.trace(
`${kleur.red("[Error]:" + (prefix ? ` [${prefix}]` : ""))} ${message}`,
...args
);
}
}
export class JSONAdapter implements ILoggerAdapter {
public success(prefix: string, message: string, ...args: any[]) {
console.log(
JSON.stringify({
type: "success",
prefix,
message,
args,
})
);
}
public info(prefix: string, message: string, ...args: any[]) {
console.log(
JSON.stringify({
type: "info",
prefix,
message,
args,
})
);
}
public debug(prefix: string, message: string, ...args: any[]) {
console.log(
JSON.stringify({
type: "debug",
prefix,
message,
args,
})
);
}
public warn(prefix: string, message: string, ...args: any[]) {
console.log(
JSON.stringify({
type: "warn",
prefix,
message,
args,
})
);
}
public error(prefix: string, message: string, ...args: any[]) {
console.log(
JSON.stringify({
type: "error",
prefix,
message,
args,
})
);
}
}
class Logger {
public static LOGGER_LEVEL = LoggerLevel.Debug;
public static setLevel(level: LoggerLevel) {
Logger.LOGGER_LEVEL = level;
}
public static DEFAULT_ADAPTER: ILoggerAdapter = new ReadableAdapter();
public static setDefaultAdapter(adapter: ILoggerAdapter) {
Logger.DEFAULT_ADAPTER = adapter;
}
public PREFIX = "";
private adapter: ILoggerAdapter;
public constructor(prefix?: string, adapter?: ILoggerAdapter) {
if (prefix) this.PREFIX = prefix;
this.adapter = adapter || Logger.DEFAULT_ADAPTER;
}
public success(message: string, ...args: any[]) {
this.adapter.success(this.PREFIX, message, ...args);
}
public info(message: string, ...args: any[]) {
this.adapter.info(this.PREFIX, message, ...args);
}
public debug(message: string, ...args: any[]) {
this.adapter.debug(this.PREFIX, message, ...args);
}
public warn(message: string, ...args: any[]) {
this.adapter.warn(this.PREFIX, message, ...args);
}
public error(message: string, ...args: any[]) {
this.adapter.error(this.PREFIX, message, ...args);
}
public createChildren(prefix: string) {
if (!this.PREFIX) return new Logger(prefix);
return new Logger(`${this.PREFIX}/${prefix}`);
}
}
export default new Logger();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment