refactor logging to fields.

This commit is contained in:
Miroslav Šedivý
2021-09-09 23:23:34 +02:00
parent 2025036013
commit f28bc1184a
4 changed files with 87 additions and 47 deletions

View File

@ -1,5 +1,5 @@
export class Logger {
private _scope: string = 'main'
protected _scope: string = 'main'
constructor(scope?: string) {
if (scope) {
@ -7,19 +7,45 @@ export class Logger {
}
}
public error(error: Error) {
console.error('[%cNEKO%c] [' + this._scope + '] %cERR', 'color: #498ad8;', '', 'color: #d84949;', error)
protected _console(level: string, m: string, fields?: Record<string, any>) {
let t = ''
const args = []
for (const name in fields) {
t += ' %c%s=%c%o'
args.push('color:#498ad8;', name, '', fields[name])
}
const scope = this._scope
switch (level) {
case 'error':
console.error('[%cNEKO%c] [%s] %cERR%c %s' + t, 'color:#498ad8;', '', scope, 'color:#d84949;', '', m, ...args)
break
case 'warn':
console.warn('[%cNEKO%c] [%s] %cWRN%c %s' + t, 'color:#498ad8;', '', scope, 'color:#eae364;', '', m, ...args)
break
case 'info':
console.info('[%cNEKO%c] [%s] %cINF%c %s' + t, 'color:#498ad8;', '', scope, 'color:#4ac94c;', '', m, ...args)
break
default:
case 'debug':
console.debug('[%cNEKO%c] [%s] %cDBG%c %s' + t, 'color:#498ad8;', '', scope, 'color:#eae364;', '', m, ...args)
break
}
}
public warn(...log: any[]) {
console.warn('[%cNEKO%c] [' + this._scope + '] %cWRN', 'color: #498ad8;', '', 'color: #eae364;', ...log)
public error(message: string, fields?: Record<string, any>) {
this._console('error', message, fields)
}
public info(...log: any[]) {
console.info('[%cNEKO%c] [' + this._scope + '] %cINF', 'color: #498ad8;', '', 'color: #4ac94c;', ...log)
public warn(message: string, fields?: Record<string, any>) {
this._console('warn', message, fields)
}
public debug(...log: any[]) {
console.debug('[%cNEKO%c] [' + this._scope + '] %cDBG', 'color: #498ad8;', '', 'color: #eae364;', ...log)
public info(message: string, fields?: Record<string, any>) {
this._console('info', message, fields)
}
public debug(message: string, fields?: Record<string, any>) {
this._console('debug', message, fields)
}
}