winstonjs / winston-daily-rotate-file

A transport for winston which logs to a rotating file each day.
MIT License
889 stars 151 forks source link

"Invalid regular expression" error for unusual filename paths #355

Open lukaszjenczmyk opened 2 years ago

lukaszjenczmyk commented 2 years ago

Description

When filename contains unusual characters (for example ./logs))/application-%DATE%.log), an error is thrown:

/Users/lukasz/Documents/projects/winston-test/node_modules/file-stream-rotator/FileStreamRotator.js:496
            if(lastEntry.match(t_log)){
                         ^

SyntaxError: Invalid regular expression: /logs))/application-2022-06-28-12.log/: Unmatched ')'
    at String.match (<anonymous>)
    at Object.FileStreamRotator.getStream (/Users/lukasz/Documents/projects/winston-test/node_modules/file-stream-rotator/FileStreamRotator.js:496:26)
    at new DailyRotateFile (/Users/lukasz/Documents/projects/winston-test/node_modules/winston-daily-rotate-file/daily-rotate-file.js:80:57)
    at Object.<anonymous> (/Users/lukasz/Documents/projects/winston-test/index.js:4:17)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47

How to reproduce

OS: Mac & Windows (haven't checked other systems) NodeJS: v16.10.0 winston-daily-rotate-file: v4.7.1

I used the code snippet from the README file and changed filename to a path that contains )) (I know this is a weird path, but one of the users of my application has such characters in the Windows account user name):

var winston = require('winston');
require('winston-daily-rotate-file');

var transport = new winston.transports.DailyRotateFile({
  level: 'info',
  filename: './logs))/application-%DATE%.log',
  datePattern: 'YYYY-MM-DD-HH',
  zippedArchive: true,
  maxSize: '20m',
  maxFiles: '14d'
});

transport.on('rotate', function(oldFilename, newFilename) {
  // do something fun
});

var logger = winston.createLogger({
  transports: [
    transport
  ]
});

logger.info('Hello World!');

Run the script 2 times. The first time it will pass, the next time it will fail.

lukaszjenczmyk commented 2 years ago

Quick update: I investigated the problem a bit more and I found out that the source of the problem is in file-stream-rotator package (a dependency of winston-daily-rotate-file). I opened a PR it that repository: https://github.com/rogerc/file-stream-rotator/pull/94

lukaszjenczmyk commented 1 year ago

Another update: file-stream-rotator fixed that issue in v1.0.0, so now winston-daily-rotate-file should update the dependency version and the issue will be resolved.