diff --git a/lib/util/util.js b/lib/util/util.js index 68a1a41e6..27f8e86d9 100644 --- a/lib/util/util.js +++ b/lib/util/util.js @@ -7,6 +7,7 @@ const process = require('node:process'); const toposort = require('toposort'); const winston = require('winston'); const child_process = require('node:child_process'); +const fs = require('node:fs'); /** * Util that contains logger and simple util methods @@ -292,6 +293,7 @@ const Util = { }, loggerTransports: null, + loggedErrors: false, /** * Logger that creates timestamped log file in 'logs/' directory * @@ -324,6 +326,27 @@ const Util = { levels: winston.config.npm.levels, transports: Object.values(Util.loggerTransports), }); + + process.on('exit', () => { + // * this is a workaround for as long as winston logger cannot be told to only create the error log file if there was an error + if ( + !Util.loggedErrors && + Util.loggerTransports?.fileError && + !noLogFile && + !Util.OPTIONS?.noLogFile + ) { + try { + fs.unlinkSync( + Util.loggerTransports?.fileError.filename.startsWith('logs/') + ? Util.loggerTransports?.fileError.filename + : 'logs/' + Util.loggerTransports?.fileError.filename + ); + } catch { + // fail silently + } + } + }); + const winstonError = myWinston.error; const winstonExtension = { /** @@ -373,6 +396,7 @@ const Util = { */ error: function (msg) { winstonError(msg); + Util.loggedErrors = true; Util.signalFatalError(); }, };