Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dateFile appender "daysToKeep" not working #836

Closed
Shinapp opened this issue Feb 13, 2019 · 23 comments
Closed

dateFile appender "daysToKeep" not working #836

Shinapp opened this issue Feb 13, 2019 · 23 comments
Assignees
Milestone

Comments

@Shinapp
Copy link

Shinapp commented Feb 13, 2019

#806
Problem seems still exists fir log4js 4.0.2

I have changed local time to test:

  1. File not rolled when alwaysIncludePattern set to false
  2. Old files not being removed after the "daysToKeep"

├─┬ log4js@4.0.2
│ ├── date-format@2.0.0
│ ├─┬ debug@3.2.6
│ │ └── ms@2.1.1
│ ├── flatted@2.0.0
│ ├── rfdc@1.1.2
│ └─┬ streamroller@1.0.1
│ ├── async@2.6.1 deduped
│ ├── date-format@2.0.0 deduped
│ ├── debug@3.2.6 deduped
│ ├── fs-extra@7.0.1 deduped
│ └── lodash@4.17.11 deduped

node v11.5.0

@GanjuV
Copy link

GanjuV commented Feb 16, 2019

daysToKeep is working even if you set alwaysIncludePattern to false in version 4.0.0.

@nomiddlename
Copy link
Collaborator

@Shinapp could you post your log4js config, please? Something that might be the cause is that log4js only checks the date/time when you write something - so you have to log something after you change the date on your system, or nothing will happen.

@Shinapp
Copy link
Author

Shinapp commented Feb 18, 2019

@nomiddlename Yes, I have logged something.
Attached my log.config

{
"appenders": {
"infoLog" : {
"type": "dateFile",
"filename": "./logs/simpleTester.log",
"alwaysIncludePattern": true,
"pattern": "yyyyMMdd",
"keepFileExt": false,
"daysToKeep": 1,
"layout": {
"type": "pattern",
"pattern": "[%d] [%-5p] - %m"
}
},
"consoleLog" : {
"type": "console",
"layout": {
"type": "pattern",
"pattern": "[%d] [%-5p] - %m"
}
},
"debugLog" : {
"type": "dateFile",
"filename": "./logs/Debug_simpleTester.log",
"alwaysIncludePattern": true,
"pattern": "yyyyMMdd",
"keepFileExt": false,
"daysToKeep": "10",
"layout": {
"type": "pattern",
"pattern": "[%d] [%-5p] - %m"
}
},
"info": {
"type": "logLevelFilter", "level": "info", "appender":"infoLog"
},
"debug": {
"type": "logLevelFilter", "level": "debug", "appender":"debugLog"
},
"console": {
"type": "logLevelFilter", "level": "all", "appender":"consoleLog"
}
},
"categories": {
"default": {"appenders": ["info", "debug"], "level": "all"},
"test": {"appenders": ["console"], "level": "all"}
}
}

log4js.configure('./config/log.config')
var logger = log4js.getLogger('default')

@nomiddlename
Copy link
Collaborator

We made some fixes to date file rotation in 4.2.0, could you try again and see if it fixed your problem?

@Shinapp
Copy link
Author

Shinapp commented May 23, 2019

The problem still persist. I am using log4js@4.3.0 .

@utyongu
Copy link

utyongu commented Jul 1, 2019

I have the same problem.

Windows 7
log4js 4.4.0

@nomiddlename
Copy link
Collaborator

Hey @Shinapp - are you still having this problem? Are you on windows too? I'm just trying to work out what may be the cause, date file rotation seems to work fine on my mac and for other people.

@Shinapp
Copy link
Author

Shinapp commented Jul 3, 2019

I still have the problem. I tried the code on mac, linux and window.
Seems the problem is in the rolling condition:
streamroller/lib/RollingFileWriteStream.js line:122
_shouldRoll(callback) { debug(in _shouldRoll, pattern = ${this.options.pattern}, currentDate = ${this.state.currentDate}, now = ${newNow()}); if (this.options.pattern && (format(this.options.pattern, this.state.currentDate) !== format(this.options.pattern, newNow()))) { this._roll({isNextPeriod: true}, callback); return; }

The condition is always false and the cleaning mechanism is not triggered.
I tried hard code the condition to true and it is work.

@nomiddlename
Copy link
Collaborator

Possibly related to #909

@nomiddlename nomiddlename self-assigned this Jul 3, 2019
@nomiddlename
Copy link
Collaborator

There were a lot of fixes for file rotation in log4js 5.x - please give log4js@5.1.0 a try and let me know if the problem still exists.

@nomiddlename nomiddlename added this to the 5.1.0 milestone Aug 22, 2019
@Shinapp
Copy link
Author

Shinapp commented Aug 27, 2019

Thank you for your reply but seems the problem still exists. Thank you.

├─┬ log4js@5.1.0
│ ├── date-format@2.1.0
│ ├─┬ debug@4.1.1
│ │ └── ms@2.1.2
│ ├── flatted@2.0.1
│ ├── rfdc@1.1.4
│ └─┬ streamroller@2.1.0
│ ├── date-format@2.1.0 deduped
│ ├─┬ debug@4.1.1
│ │ └── ms@2.1.2
│ └─┬ fs-extra@8.1.0
│ ├── graceful-fs@4.2.1 deduped
│ ├─┬ jsonfile@4.0.0
│ │ └── graceful-fs@4.2.1 deduped
│ └── universalify@0.1.2

@mariuswang007
Copy link

@Shinapp agree with you. the issue still exists in log4js@5.1.0
@nomiddlename I changed just one line and it seems worked, just for your reference
--streamroller V2.1.0
--RollingFileWriteStream.js
-- line 145:
-- before: this.state.currentDate !== format(this.options.pattern, newNow())
-- after: (this.state.currentDate !== format(this.options.pattern, newNow()) || this.options.numToKeep > 0)

@nomiddlename nomiddlename modified the milestones: 5.1.0, 5.2.0 Oct 4, 2019
@nomiddlename
Copy link
Collaborator

@Shinapp I think your problem is related to the pattern you're using: "yyyyMMdd" maps to all digits, which the code thinks is a file index instead of a date. As a workaround, or to test this out, try using "yyyy-MM-dd" as the pattern. I should have a fix for this issue in the next few days.

@mariuswang007 I'm not sure that your issue is the same, if changing that line fixed it. Could you post your log4js config, please?

@Shinapp
Copy link
Author

Shinapp commented Oct 8, 2019

@Shinapp I think your problem is related to the pattern you're using: "yyyyMMdd" maps to all digits, which the code thinks is a file index instead of a date. As a workaround, or to test this out, try using "yyyy-MM-dd" as the pattern. I should have a fix for this issue in the next few days.

@mariuswang007 I'm not sure that your issue is the same, if changing that line fixed it. Could you post your log4js config, please?

Tried but fail. I have created lots of log file named "filename,log.2019-10-17" etc.

@Shinapp
Copy link
Author

Shinapp commented Oct 8, 2019

@Shinapp agree with you. the issue still exists in log4js@5.1.0
@nomiddlename I changed just one line and it seems worked, just for your reference
--streamroller V2.1.0
--RollingFileWriteStream.js
-- line 145:
-- before: this.state.currentDate !== format(this.options.pattern, newNow())
-- after: (this.state.currentDate !== format(this.options.pattern, newNow()) || this.options.numToKeep > 0)

this.options.numToKeep <-- this is related to the config daysToKeep exist
What it means is make the dateChange flag to always true (if daysToKeep is applied in the config)

@nomiddlename
Copy link
Collaborator

@Shinapp could you post some step-by-step instructions on how you're testing this, please? I'm having trouble reproducing the issue. Ideally, if you could include the code you're using as well, that would be great.

@Shinapp
Copy link
Author

Shinapp commented Oct 9, 2019

  1. I have a script do some simple log such as logger.info('Server Start')
  2. Run the script by 'node <script.js>'
  3. Stop the script by SIGKILL
  4. Open the macOS 'Date & Time Preference', uncheck the 'Automatic set time'
  5. Adjust the day, usually one day after

Repeat 2-5

Suppose whenever a new log file generated will trigger the housekeeping.

@nomiddlename
Copy link
Collaborator

With alwaysIncludePattern set, it will always create a new file if the date is different - but that won't trigger clean up of the old ones until the day changes. Does that describe what you're seeing? New log file created, but the old ones stay there?

@Shinapp
Copy link
Author

Shinapp commented Oct 15, 2019

With alwaysIncludePattern set, it will always create a new file if the date is different - but that won't trigger clean up of the old ones until the day changes. Does that describe what you're seeing? New log file created, but the old ones stay there?

Yes, "New log file created, but the old ones stay there", but I expect the old ones clean up after the day passes the larger than daysToKeep.

@nomiddlename
Copy link
Collaborator

It won't clean up old files on startup, only when the day changes if it is running. Maybe try changing the date without killing the script, to see if that does what you expect it to do?

@Shinapp
Copy link
Author

Shinapp commented Nov 28, 2019

Seems the problem is solved. I am currently using log4js@5.2.1

@Shinapp Shinapp closed this as completed Nov 28, 2019
@Shinapp Shinapp reopened this Nov 28, 2019
@Shinapp
Copy link
Author

Shinapp commented Nov 28, 2019

It won't clean up old files on startup, only when the day changes if it is running. Maybe try changing the date without killing the script, to see if that does what you expect it to do?

This is really the situation I facing. I tried to run on an express services and get the expected behaviour you mentioned.

However, when I running a small script that has no date change during the script running (on cron), it does not clean up old files on startup (or termination)

Is there a way to improve my script or it is a feature request?

P.S. I am running cron jobs and I hope log files will be clean up everyday.

@nomiddlename
Copy link
Collaborator

I think the behaviour you describe would be easier to do by adding the clean up to your cron job script. I'm going to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants