Skip to content

Commit

Permalink
Merge pull request #1046 from Accenture/bugfix/1045-logger-creates-er…
Browse files Browse the repository at this point in the history
…rorlog-files-even-if-no-errors-occured

#1045: remove -error.log file on exit if there was no error
  • Loading branch information
JoernBerkefeld authored Jul 24, 2023
2 parents 4b72006 + 2c40b7b commit 1cb3771
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -292,6 +293,7 @@ const Util = {
},

loggerTransports: null,
loggedErrors: false,
/**
* Logger that creates timestamped log file in 'logs/' directory
*
Expand Down Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -373,6 +396,7 @@ const Util = {
*/
error: function (msg) {
winstonError(msg);
Util.loggedErrors = true;
Util.signalFatalError();
},
};
Expand Down

0 comments on commit 1cb3771

Please sign in to comment.