rogerc / file-stream-rotator

NodeJS file stream rotator
MIT License
143 stars 69 forks source link

Rotation file counter issue on process restart #101

Open jeromevalentin opened 1 year ago

jeromevalentin commented 1 year ago

I'm using winston-daily-rotate-file in version 4.7.1, referencing file-stream-rotator in version 0.6.1.

How to reproduce?

Defect

The internal rotation counter used to determine the "current" logging file is not extracted correctly from the content of the given audit file. The log file is matched directly to the last entry of audit file.

Consequences:

Analyzing logs becomes really complicated as if the process is restarted automatically (in case of failure .... meaning when logs are the most useful), the ordered of the logged files are not readable easily along the time.

Proposed Fix

Escape the whole log file name prior to perform filename matching by using standard:

...
    function escapeRegExp(string) {
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
    }

    var lastEntry = auditLog.files[auditLog.files.length - 1].name;
    if(lastEntry.match(escapeRegExp(t_log))){
        var lastCount = lastEntry.match(t_log + "\\.(\\d+)");
....