Add verbose option
This commit is contained in:
+10
-3
@@ -4,6 +4,7 @@ import {get_color} from './color'
|
||||
export const writers = new Map<string, Writer>
|
||||
export let options: Options = {
|
||||
format: "[$time] $level $namespace :",
|
||||
verbose: true,
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
@@ -41,7 +42,10 @@ export class Logger {
|
||||
}
|
||||
|
||||
function log(message: any[], level: Level, namespace: string) : void {
|
||||
if (writers.size === 0) return
|
||||
if (writers.size === 0) {
|
||||
if (options.verbose) console.log("No writer subscribed, discard message")
|
||||
return
|
||||
}
|
||||
|
||||
// Format header of log
|
||||
const head = options.format
|
||||
@@ -50,9 +54,12 @@ function log(message: any[], level: Level, namespace: string) : void {
|
||||
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()) {
|
||||
for (const [name, writer] of writers.entries()) {
|
||||
const options = writer.options
|
||||
if (options?.minLevel > level) continue
|
||||
if (options?.minLevel > level) {
|
||||
if (options.verbose) console.log(`Writer's level is lower, discard message for ${name}`)
|
||||
continue
|
||||
}
|
||||
writer.log(level, options?.with_color ? head_color : head_bw, ...message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export type Options = {
|
||||
format: string,
|
||||
verbose: boolean,
|
||||
}
|
||||
|
||||
export enum Level {
|
||||
|
||||
Reference in New Issue
Block a user