Skip to content

Commit

Permalink
[Fix] Add an error handler to stdout/stderr (#3072)
Browse files Browse the repository at this point in the history
  • Loading branch information
CommandMC authored Sep 29, 2023
1 parent abd45f9 commit 195ddde
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/backend/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ interface LogOptions {
export let logsDisabled = false

export function initLogger() {
// Add a basic error handler to our stdout/stderr. If we don't do this,
// the main `process.on('uncaughtException', ...)` handler catches them (and
// presents an error message to the user, which is hardly necessary for
// "just" failing to write to the streams)
for (const channel of ['stdout', 'stderr'] as const) {
process[channel].once('error', (error: Error) => {
const prefix = `${getTimeStamp()} ${getLogLevelString(
'ERROR'
)} ${getPrefixString(LogPrefix.Backend)}`
appendMessageToLogFile(
`${prefix} Error writing to ${channel}: ${error.stack}`
)
process[channel].on('error', () => {
// Silence further write errors
})
})
}

// check `disableLogs` setting
const { disableLogs } = GlobalConfig.get().getSettings()

Expand Down

0 comments on commit 195ddde

Please sign in to comment.