diff --git a/lib/main/index.js b/lib/main/index.js index 92c57da..319c4a8 100644 --- a/lib/main/index.js +++ b/lib/main/index.js @@ -72,6 +72,7 @@ const Main = { serverName: this.serverName }; await Alerts.handleUncaughtExceptions(`${errorOrigin}\n${errorMessage}`, messageExtraInfo); + await Logs.clearLogsBeforeExit(); if (exitOnException) process.exit(1); }); } diff --git a/lib/main/logs/index.js b/lib/main/logs/index.js index 1b2bc7e..421f20a 100644 --- a/lib/main/logs/index.js +++ b/lib/main/logs/index.js @@ -89,4 +89,15 @@ CollectLogsHook.logStream = new stream.Writable({ } }); +CollectLogsHook.clearLogsBeforeExit = async function (timeout = 5000) { + if (typeof this.storage.flushLogs === 'function') { + try { + await Promise.race([ + this.storage.flushLogs(), + new Promise((resolve, reject) => setTimeout(() => reject(new Error('flushLogs timed out')), timeout)) + ]); + } catch (err) { } + } +}; + module.exports = CollectLogsHook;