diff options
Diffstat (limited to 'src/utils/logger.ts')
-rw-r--r-- | src/utils/logger.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/utils/logger.ts b/src/utils/logger.ts new file mode 100644 index 0000000..80126c3 --- /dev/null +++ b/src/utils/logger.ts @@ -0,0 +1,51 @@ +export interface TracingLogger { + info(log: string): void; + warn(log: string): void; + error(log: string): void; + debug(log: string): void; + + createChild(prefix: string): TracingLogger; +} + +export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; + +export class ConsoleTracingLogger implements TracingLogger { + protected prefix: string; + private levels: LogLevel[]; + + constructor(prefix: string, levels: LogLevel[] = ['warn', 'error']) { + this.prefix = prefix; + this.levels = levels; + } + + private makePrefix(level: LogLevel): string { + return `[${new Date().toISOString()}] ${level} (${this.prefix}) > `; + } + + public info(log: string) { + if (this.levels.includes('info')) + console.log(this.makePrefix('info') + log); + } + + public warn(log: string) { + if (this.levels.includes('warn')) + console.warn(this.makePrefix('warn') + log); + } + + public error(log: string) { + if (this.levels.includes('error')) + console.error(this.makePrefix('error') + log); + } + + public debug(log: string) { + if (this.levels.includes('debug')) + console.debug(this.makePrefix('debug') + log); + } + + public createChild(prefix: string, levels?: LogLevel[]) { + return new ConsoleTracingLogger( + `${this.prefix} -> ${prefix}`, + levels ?? this.levels, + ); + } +} |