logger: color only if enabled in env.

This commit is contained in:
Miroslav Šedivý 2022-09-15 19:30:45 +02:00
parent 84a1d16de3
commit fb5d9affd9
2 changed files with 50 additions and 20 deletions

View File

@ -26,6 +26,7 @@ docker run --rm -it \
-p 3001:8080 \
-e "NEKO_HOST=$NEKO_HOST" \
-e "NEKO_PORT=$NEKO_PORT" \
-e "VUE_APP_LOG_COLOR=true" \
--user "$(id -u):$(id -g)" \
--volume "${PWD}/../:/app" \
--entrypoint="npm" \

View File

@ -2,42 +2,71 @@ export class Logger {
// eslint-disable-next-line
constructor(
protected readonly _scope: string = 'main',
private readonly _color: boolean = !!process.env.VUE_APP_LOG_COLOR,
) {}
protected _console(level: string, m: string, fields?: Record<string, any>) {
const scope = this._scope
let t = ''
const args = []
for (const name in fields) {
if (fields[name] instanceof Error) {
t += ' %c%s="%s"%c'
args.push('color:#d84949;', name, (fields[name] as Error).message, '')
if (this._color) {
t += ' %c%s="%s"%c'
args.push('color:#d84949;', name, (fields[name] as Error).message, '')
} else {
t += ' %s="%s"'
args.push(name, (fields[name] as Error).message)
}
continue
}
if (typeof fields[name] === 'string' || fields[name] instanceof String) {
t += ' %c%s=%c"%s"'
t += this._color ? ' %c%s=%c"%s"' : ' %s="%s"'
} else {
t += ' %c%s=%c%o'
t += this._color ? ' %c%s=%c%o' : ' %s=%o'
}
args.push('color:#498ad8;', name, '', fields[name])
if (this._color) {
args.push('color:#498ad8;', name, '', fields[name])
} else {
args.push(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
if (this._color) {
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
}
} else {
switch (level) {
case 'error':
console.error('[NEKO] [%s] ERR %s' + t, scope, m, ...args)
break
case 'warn':
console.warn('[NEKO] [%s] WRN %s' + t, scope, m, ...args)
break
case 'info':
console.info('[NEKO] [%s] INF %s' + t, scope, m, ...args)
break
default:
case 'debug':
console.debug('[NEKO] [%s] DBG %s' + t, scope, m, ...args)
break
}
}
}