Remove writers from lib

Closes #4
This commit is contained in:
2024-05-14 22:49:59 +02:00
parent 4dbe36932c
commit 17ac2bdcf4
8 changed files with 50 additions and 165 deletions
+12 -28
View File
@@ -1,5 +1,3 @@
import { Chalk } from 'chalk'
export type Options = {
format: string,
}
@@ -27,33 +25,19 @@ export type WriterOptions = {
[key: string | number | symbol]: any,
}
export function level_to_string(level: Level, with_color: boolean) : string {
const str = get_string(level)
export abstract class Writer {
protected readonly _options: WriterOptions;
protected constructor(options: WriterOptions) {
this._options = options
}
if (!with_color) return str
public log(level: Level, ...data: any[]) : void {
if (level < this.options.minLevel) return
this.write(level, ...data)
}
protected abstract write(level: Level, ...data: any[]) : void;
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 "
}
}
const chalk = new Chalk({ level: 2 }) // 256 colors
function get_color(level: Level) {
switch (level) {
case Level.DEBUG: return chalk.blueBright
case Level.TRACE: return chalk.green
case Level.INFO: return (str: string) => str
case Level.WARNING: return chalk.hex('#FFA500')
case Level.ERROR: return chalk.red
public get options() : WriterOptions {
return this._options
}
}