log4js-node / streamroller

node.js file streams that roll over when they reach a maximum size, or a date/time.
MIT License
37 stars 19 forks source link

There is an extra separator. #64

Closed pikadun closed 2 years ago

pikadun commented 4 years ago

https://github.com/log4js-node/streamroller/blob/e9ba0d85155ba0e8561f219faf084d521708885e/lib/fileNameFormatter.js#L3

I have a dateFile appender like this

{
            type: 'dateFile',
            filename: 'log/info/info',
            pattern: '-yyyy-MM-dd.log',
            alwaysIncludePattern: true,
}

it should create a file which name like log/info/info-yyyy-MM-dd.log but not log/info/info.-yyyy-MM-dd.log

lamweili commented 2 years ago

From the code, if the pattern starts with ., the . is removed. https://github.com/log4js-node/streamroller/blob/e9ba0d85155ba0e8561f219faf084d521708885e/lib/DateRollingFileStream.js#L19-L21

Subsequently, FILENAME_SEP (or .) is added to the front of the resultant pattern. https://github.com/log4js-node/streamroller/blob/e9ba0d85155ba0e8561f219faf084d521708885e/lib/fileNameFormatter.js#L17-L22

I believe this is the issue.


const log4js = require('log4js');
log4js.configure({
  appenders: {
    test1: { 
      type: 'dateFile', 
      filename: 'test1.log', 
      pattern: '.yyyy-MM-dd', 
      alwaysIncludePattern: true, 
      keepFileExt: true
    },
    test2: { 
      type: 'dateFile', 
      filename: 'test2.log', 
      pattern: '-yyyy-MM-dd', 
      alwaysIncludePattern: true, 
      keepFileExt: true
    },
    test3: { 
      type: 'dateFile', 
      filename: 'test3.log', 
      pattern: '.-yyyy-MM-dd', 
      alwaysIncludePattern: true, 
      keepFileExt: true
    },
  },
  categories: {
    default: { appenders: [ 'test1', 'test2', 'test3' ], level: 'debug' }
  }
});
Pattern Expected Actual Test
.yyyy-MM-dd test1.2022-01-07.log test1.2022-01-07.log ✔️
-yyyy-MM-dd test2-2022-01-07.log test2.-2022-01-07.log
.-yyyy-MM-dd test3.-2022-01-07.log test3.-2022-01-07.log ✔️
lamweili commented 2 years ago

I tried the following and it now works.

For more details: https://github.com/log4js-node/streamroller/pull/75#issue-1100547736