Allow to control padding of level

This commit is contained in:
2024-05-22 10:11:59 +02:00
parent fae54e1b71
commit 0daf6497d5
2 changed files with 11 additions and 10 deletions
+10 -10
View File
@@ -4,6 +4,7 @@ import {get_color} from './color'
export const writers = new Map<string, Writer>
export let options: Options = {
format: "[$time] $level $namespace :",
pad_level: true,
verbose: true,
}
@@ -51,8 +52,12 @@ function log(message: any[], level: Level, namespace: string) : void {
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))
let lvl = get_string(level)
if (options.pad_level) lvl = pad(lvl)
const head_color = head.replace('$level', get_color(level)(lvl))
const head_bw = head.replace('$level', lvl)
for (const [name, writer] of writers.entries()) {
const options = writer.options
@@ -64,13 +69,8 @@ function log(message: any[], level: Level, namespace: string) : void {
}
}
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 pad(str: string) : string {
return str + " ".repeat("WARNING".length - str.length)
}
function get_string(level: Level) : string {
@@ -81,4 +81,4 @@ function get_string(level: Level) : string {
case Level.WARNING: return "WARNING"
case Level.ERROR: return "ERROR "
}
}
}
+1
View File
@@ -1,5 +1,6 @@
export type Options = {
format: string,
pad_level: boolean,
verbose: boolean,
}