Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1045: remove -error.log file on exit if there was no error #1046

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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