archive log files in main and renderer process after certain size #359
Answered
by
megahertz
arunbansal-cpi
asked this question in
Q&A
-
I want to have log rotation of files after certain size to a given number(say 5) in main and renderer processes. const logger = require('electron-log');
const glob = require('glob');
const no_of_files = 5;
logger.transports.file.maxSize = 512;
logger.transports.file.resolvePath = () => path.join('/tmp', `${app.name}.log`);
logger.transports.file.archiveLog = (file) => {
const oldPath = file.toString();
const inf = path.parse(oldPath);
let file_count = 0;
//print(`$(inf.name)`)
consoel.log(`File rotation :${inf.name}`);
try {
glob(`${inf.dir}/${inf.name}`, (err, files) => {
if (files.length > 0) file_count = files.length - 1;
});
console.log(`file_count: ${file_count}`);
if (file_count >= no_of_files) file_count = 0;
let new_file_path = path.join(
inf.dir,
`${inf.name}.${++file_count}${inf.ext}`
);
fs.unlink(new_file_path, (err) => {
if (err) {
console.log(`file is not present ${new_file_path}`);
}
});
console.log(`${new_file_path}`);
fs.renameSync(oldPath, new_file_path);
} catch (e) {
logConsole('Could not rotate log', e);
const quarterOfMaxSize = Math.round(transport.maxSize / 4);
file.crop(Math.min(quarterOfMaxSize, 256 * 1024));
}
}; Files are not achiving as intended |
Beta Was this translation helpful? Give feedback.
Answered by
megahertz
May 9, 2023
Replies: 1 comment 2 replies
-
Sometimes archive log works a bit later then maximum file size exceeds the limit, especially when writing a large log message. Anyway, if it works with the default behavior, then the issue is in your To debug such an issue, I would do the following:
|
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
arunbansal-cpi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes archive log works a bit later then maximum file size exceeds the limit, especially when writing a large log message. Anyway, if it works with the default behavior, then the issue is in your
archiveLog
implementation.To debug such an issue, I would do the following:
archiveLog
function.