From 2c40b7bcca742256e7a0cbdefcc4e38357f2ce93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Mon, 24 Jul 2023 11:44:30 +0200 Subject: [PATCH] #1045: remove -error.log file on exit if there was no error --- lib/util/util.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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(); }, };