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 \ -p 3001:8080 \
-e "NEKO_HOST=$NEKO_HOST" \ -e "NEKO_HOST=$NEKO_HOST" \
-e "NEKO_PORT=$NEKO_PORT" \ -e "NEKO_PORT=$NEKO_PORT" \
-e "VUE_APP_LOG_COLOR=true" \
--user "$(id -u):$(id -g)" \ --user "$(id -u):$(id -g)" \
--volume "${PWD}/../:/app" \ --volume "${PWD}/../:/app" \
--entrypoint="npm" \ --entrypoint="npm" \

View File

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