fix: SDA-1487: add support for creating custom log folders on the fly (#790)

* SDA-1487: add support for creating custom log folders on the fly

- Currently, if we use custom logs path and a directory doesn't exist, logs are created in the default Electron path.
- This change fixes that by creating the custom logs directory (recursively) on the fly
- This change also fixes issue with clearing old log files. Now, we keep the log files for the last 5 days instead of 10 days

* SDA-1487: fix unit tests
This commit is contained in:
Vishwas Shashidhar 2019-10-17 13:29:18 +05:30 committed by GitHub
parent af886faaf5
commit efe7f824b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -42,7 +42,7 @@
"@types/enzyme": "3.9.0",
"@types/ffi-napi": "2.4.1",
"@types/jest": "23.3.12",
"@types/node": "10.11.4",
"@types/node": "11.13.22",
"@types/react": "16.8.3",
"@types/react-dom": "16.0.9",
"@types/ref-napi": "1.4.0",

View File

@ -36,7 +36,10 @@ class Logger {
// If the user has specified a custom log path use it.
const customLogPathArg = getCommandLineArgs(process.argv, '--logPath=', false);
const customLogsFolder = customLogPathArg && customLogPathArg.substring(customLogPathArg.indexOf('=') + 1);
if (customLogsFolder && fs.existsSync(customLogsFolder)) {
if (customLogsFolder) {
if (!fs.existsSync(customLogsFolder)) {
fs.mkdirSync(customLogsFolder, { recursive: true });
}
app.setPath('logs', customLogsFolder);
}
@ -66,7 +69,9 @@ class Logger {
}
// cleans up old logs if there are any
this.cleanupOldLogs();
if (app.isPackaged) {
this.cleanupOldLogs();
}
}
/**
@ -234,15 +239,12 @@ class Logger {
*/
private cleanupOldLogs(): void {
const files = fs.readdirSync(this.logPath);
const deleteTimeStamp = new Date().getTime() + (10 * 24 * 60 * 60 * 1000);
const deleteTimeStamp = new Date().getTime() - (5 * 24 * 60 * 60 * 1000);
files.forEach((file) => {
if (file === '.DS_Store' || file === 'app.log') {
return;
}
const filePath = path.join(this.logPath, file);
const stat = fs.statSync(filePath);
const fileTimestamp = new Date(util.inspect(stat.mtime)).getTime();
if (fileTimestamp > deleteTimeStamp) {
if (fileTimestamp < deleteTimeStamp) {
fs.unlinkSync(filePath);
}
});