diff options
Diffstat (limited to 'packages/taler-util/src/logging.ts')
-rw-r--r-- | packages/taler-util/src/logging.ts | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/packages/taler-util/src/logging.ts b/packages/taler-util/src/logging.ts index 840402d6f..ae4e2df53 100644 --- a/packages/taler-util/src/logging.ts +++ b/packages/taler-util/src/logging.ts @@ -34,34 +34,36 @@ export enum LogLevel { export let globalLogLevel = LogLevel.Info; -export function setGlobalLogLevelFromString(logLevelStr: string) { - let level: LogLevel; +export function setGlobalLogLevelFromString(logLevelStr: string): void { + globalLogLevel = getLevelForString(logLevelStr); +} + +export const byTagLogLevel: Record<string, LogLevel> = {}; +export function setLogLevelFromString(tag: string, logLevelStr: string): void { + byTagLogLevel[tag] = getLevelForString(logLevelStr); +} + +function getLevelForString(logLevelStr: string): LogLevel { switch (logLevelStr.toLowerCase()) { case "trace": - level = LogLevel.Trace; - break; + return LogLevel.Trace; case "info": - level = LogLevel.Info; - break; + return LogLevel.Info; case "warn": case "warning": - level = LogLevel.Warn; - break; + return LogLevel.Warn; case "error": - level = LogLevel.Error; - break; + return LogLevel.Error; case "none": - level = LogLevel.None; - break; + return LogLevel.None; default: if (isNode) { process.stderr.write(`Invalid log level, defaulting to WARNING\n`); } else { console.warn(`Invalid log level, defaulting to WARNING`); } - level = LogLevel.Warn; + return LogLevel.Warn; } - globalLogLevel = level; } function writeNodeLog( @@ -96,10 +98,11 @@ function writeNodeLog( * and uses the corresponding console.* method to log in the browser. */ export class Logger { - constructor(private tag: string) {} + constructor(private tag: string) { } - shouldLogTrace() { - switch (globalLogLevel) { + shouldLogTrace(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: return true; case LogLevel.Message: @@ -111,8 +114,9 @@ export class Logger { } } - shouldLogInfo() { - switch (globalLogLevel) { + shouldLogInfo(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: case LogLevel.Message: case LogLevel.Info: @@ -124,8 +128,9 @@ export class Logger { } } - shouldLogWarn() { - switch (globalLogLevel) { + shouldLogWarn(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: case LogLevel.Message: case LogLevel.Info: @@ -137,8 +142,9 @@ export class Logger { } } - shouldLogError() { - switch (globalLogLevel) { + shouldLogError(): boolean { + const level = byTagLogLevel[this.tag] ?? globalLogLevel; + switch (level) { case LogLevel.Trace: case LogLevel.Message: case LogLevel.Info: @@ -192,7 +198,7 @@ export class Logger { } } - trace(message: any, ...args: any[]): void { + trace(message: string, ...args: any[]): void { if (!this.shouldLogTrace()) { return; } |