From fb5d9affd939256d97e84be70b8904437bfa8b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 15 Sep 2022 19:30:45 +0200 Subject: [PATCH] logger: color only if enabled in env. --- dev/serve | 1 + src/component/utils/logger.ts | 69 +++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/dev/serve b/dev/serve index aede4c2e..0c6f389d 100755 --- a/dev/serve +++ b/dev/serve @@ -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" \ diff --git a/src/component/utils/logger.ts b/src/component/utils/logger.ts index 54c54df4..d0f3eb0c 100644 --- a/src/component/utils/logger.ts +++ b/src/component/utils/logger.ts @@ -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) { + 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 + } } }