diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-02-23 16:46:10 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-02-23 17:04:54 -0700 |
commit | d0d6aae1e56428f597f69f5c9cfb261afe671f5d (patch) | |
tree | ef1d572f2bda148ef1d13602b6b4117be8e18aa4 /src/utils/logger.ts | |
download | cps-interpreter-d0d6aae1e56428f597f69f5c9cfb261afe671f5d.tar.gz cps-interpreter-d0d6aae1e56428f597f69f5c9cfb261afe671f5d.zip |
initial parser
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, + ); + } +} |