Skip to content

Commit

Permalink
fix: an issue where worker's log-levels wheren't respected
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed May 10, 2023
1 parent f116c8f commit a780b0a
Showing 1 changed file with 52 additions and 19 deletions.
71 changes: 52 additions & 19 deletions apps/appcontainer-node/packages/generic/src/appContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ export class AppContainer {
// kill child processes
}
async setLogLevel(logLevel: LogLevel): Promise<void> {
this.logger.info(`Seting log level to "${logLevel}"`)
setLogLevel(logLevel)
}
async _debugKill(): Promise<void> {
Expand Down Expand Up @@ -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
Expand All @@ -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}`)
}
}
Expand Down

0 comments on commit a780b0a

Please sign in to comment.