+12
-28
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user