From a780b0a5ba31baa4bf3f28260925f44398d52763 Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Wed, 10 May 2023 09:00:46 +0200 Subject: [PATCH] fix: an issue where worker's log-levels wheren't respected --- .../packages/generic/src/appContainer.ts | 71 ++++++++++++++----- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/apps/appcontainer-node/packages/generic/src/appContainer.ts b/apps/appcontainer-node/packages/generic/src/appContainer.ts index 5781dd51..c3cf16ca 100644 --- a/apps/appcontainer-node/packages/generic/src/appContainer.ts +++ b/apps/appcontainer-node/packages/generic/src/appContainer.ts @@ -335,6 +335,7 @@ export class AppContainer { // kill child processes } async setLogLevel(logLevel: LogLevel): Promise { + this.logger.info(`Seting log level to "${logLevel}"`) setLogLevel(logLevel) } async _debugKill(): Promise { @@ -665,9 +666,8 @@ export class AppContainer { const messages = `${data}`.split('\n') for (const message of messages) { + if (!message?.length) continue try { - if (!message?.length) continue - // Ignore some messages: if (message.indexOf('NODE_TLS_REJECT_UNAUTHORIZED') !== -1) { continue @@ -680,28 +680,61 @@ export class AppContainer { this.busyPorts.add(busyPort) } - const json = JSON.parse(`${message}`) + let parsedMessage: string | undefined = undefined + let logLevel = '' + let messageData: any = {} + try { + const json = JSON.parse(`${message}`) + if (typeof json === 'object') { + logLevel = json.level + parsedMessage = `[${json.label}] ${json.message}` + messageData = _.omit(json, ['message', 'localTimestamp', 'level']) + } + } catch { + // There was an error parsing the message (the probably message wasn't JSON). + } - if (typeof json === 'object') { - const logFcn = - json.level === 'error' - ? this.logger.error - : json.level === 'warn' - ? this.logger.warn - : json.level === 'info' - ? this.logger.info - : defaultLog + if (parsedMessage === undefined) { + // [logLevel] [category] message + const m = message.match(/^\[([^\]]+)\]\W\[([^\]]+)\]\W(.*)/) + if (m) { + logLevel = m[1] + parsedMessage = `[${m[2]}] ${m[3]}` + } + } - const messageData = _.omit(json, ['message', 'localTimestamp', 'level']) + if (parsedMessage === undefined) { + // Fall back to just just log the whole message: + parsedMessage = `${message}` + } - logFcn( - `App "${appId}" (${appType}): ${json.message}`, - _.isEmpty(messageData) ? undefined : messageData - ) + const logLevels: { [key: string]: LeveledLogMethod } = { + error: this.logger.error, + warn: this.logger.warn, + help: this.logger.help, + data: this.logger.data, + info: this.logger.info, + debug: this.logger.debug, + prompt: this.logger.prompt, + http: this.logger.http, + verbose: this.logger.verbose, + input: this.logger.input, + silly: this.logger.silly, + + emerg: this.logger.emerg, + alert: this.logger.alert, + crit: this.logger.crit, + warning: this.logger.warning, + notice: this.logger.notice, } + const logFcn = logLevels[logLevel] || defaultLog + logFcn( + `App "${appId}" (${appType}): ${parsedMessage}`, + _.isEmpty(messageData) ? undefined : messageData + ) } catch (err) { - // There was an error parsing the message (the probably message wasn't JSON). - + this.logger.error(err) + // Fallback: defaultLog(`${appId} stdout: ${message}`) } }