Skip to content

Commit

Permalink
#1045: remove -error.log file on exit if there was no error
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Jul 24, 2023
1 parent 4b72006 commit 2c40b7b
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 2c40b7b

Please sign in to comment.