Files
log/src/logger.ts
T
2024-05-15 21:04:08 +02:00

77 lines
2.0 KiB
TypeScript

import {Level, type Options, type Writer} from './types'
import {get_color} from './color'
export const writers = new Map<string, Writer>
export let options: Options = {
format: "[$time] $level $namespace :",
}
export class Logger {
private readonly _namespace: string
constructor(namespace: string) {
this._namespace = namespace
}
public get namespace() : string {
return this._namespace
}
public extend(sub_namespace: string) : Logger {
return new Logger(`${this.namespace}:${sub_namespace}`)
}
public debug(...data: any[]) : void {
log(data, Level.DEBUG, this._namespace)
}
public trace(...data: any[]) : void {
log(data, Level.TRACE, this._namespace)
}
public info(...data: any[]) : void {
log(data, Level.INFO, this._namespace)
}
public warn(...data: any[]) : void {
log(data, Level.WARNING, this._namespace)
}
public error(...data: any[]) : void {
log(data, Level.ERROR, this._namespace)
}
}
function log(message: any[], level: Level, namespace: string) : void {
if (writers.size === 0) return
// Format header of log
const head = options.format
.replace("$time", new Date().toISOString())
.replace("$namespace", namespace)
const head_bw = head.replace('$level', level_to_string(level, false))
const head_color = head.replace("$level", level_to_string(level, true))
for (const writer of writers.values()) {
writer.log(level, writer.options?.with_color ? head_color : head_bw, ...message)
}
}
function level_to_string(level: Level, with_color: boolean) : string {
const str = get_string(level)
if (!with_color) return str
const color = get_color(level)
return color(str)
}
function get_string(level: Level) : string {
switch (level) {
case Level.DEBUG: return "DEBUG "
case Level.TRACE: return "TRACE "
case Level.INFO: return "INFO "
case Level.WARNING: return "WARNING"
case Level.ERROR: return "ERROR "
}
}