megahertz / electron-log

Simple logging module Electron/Node.js/NW.js application. No dependencies. No complicated configuration.
MIT License
1.3k stars 127 forks source link

Log rotation overrride old log files #425

Closed Harley-xk closed 3 months ago

Harley-xk commented 3 months ago

When the log file reaches the maxSize limit (eg. 1M), the log file is renamed to a new file called xxxx.old.log. and when current log file reaches the maxSize again, the rotate fires. current log file renamed to xxx.old.log again, whitch overrides the last generated 'old log' file.

roicp commented 3 months ago

You can use the "archiveLogFn" function to change the file name

log.transports.file.archiveLogFn = (oldLogFile) => {
    const oldLogFilePath = oldLogFile.toString();
    const info = path.parse(oldLogFilePath);

    try {
        const data = tools.buildDateTimeFileName();

        fs.renameSync(oldLogFilePath, path.join(info.dir, `${info.name}.${data}${info.ext}`));
    } catch (e) {
        console.warn('Could not rotate log', e);
        log.error('Could not rotate log', e);
    }
}

The "tools.buildDateTimeFileName();" is a custom function to returns the current date in a format like "yyyyMMddHHmmss"

I hope this helps you

megahertz commented 3 months ago

@Harley-xk Yes, I decided to use a simple log rotation with the possibility to customize it easily, as roicp mentioned.