0

I use the bellow codes.When I run project create MyLogFile.log file.

Next day I run project and log4j renames MyLogFile to MyLogFile.log_Yesterday.log and create new MyLogFile file and start to write this file.

Why It doesn't crate MyLogFile.log_ToDay.log file? Why it renames fileName? Thank in advance

log4j.rootLogger=DEBUG, stdout   
log4j.rootLogger=DEBUG, RollingAppender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=d:/Logs/MyLogFile.log
log4j.appender.RollingAppender.DatePattern='_'yyyy-MM-dd'.log' 
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n

1 Answers1

1

The appender being used is org.apache.log4j.DailyRolling‌​FileAppender which takes a back up of the current log file whenever the date changes.

2017-08-29, The log file will be created with the name MyLogFile.log and logs will be written to it.

2017-08-30, Whenever the code encounters something to be logged, the appender will rename the file created on the previous day to MyLogFile_2017-08-29.log and create a fresh MyLogFile.log for the current day.

Please refer to the Javadoc for DailyRollingFileAppender for more details on how it works.

Aniket V
  • 3,183
  • 2
  • 15
  • 27
  • How can I do, every day log4f create daily file and writes to that file? – Mehman Bashirov Aug 29 '17 at 06:27
  • You need not do anything. `log4j` handles the daily rollover. If you do not want the file to be rolled over daily, you can change the appender to `RollingFileAppender` and use a different policy for rollover. There is `MaxFileSize` property which will keep logging in the same file till the specified size of the file is reached. Please check the `log4j` documentation for more details. – Aniket V Aug 29 '17 at 06:34
  • Your requirement is not very clear. Please rephrase your question or explain your requirement in detail with possibly some examples. – Aniket V Aug 29 '17 at 06:41
  • I research but I can't find any way for this issue and I wrote here. – Mehman Bashirov Aug 29 '17 at 06:42
  • You wrote. The appender will rename the file created on the previous day to MyLogFile_2017-08-29.log and create a fresh MyLogFile.log for the current day. I don't wont to rename file.I wont to create file as every day name. – Mehman Bashirov Aug 29 '17 at 06:44
  • There is no appender that will create a file with current date unless you try to add some variable in your file name which gets the date. However whichever appender you use, there will be rollover happening in case you specify it and the rollover files will follow some specified pattern. In the end, there will be files having patternized names. – Aniket V Aug 29 '17 at 06:48
  • Thank you so much. I will think another way :) – Mehman Bashirov Aug 29 '17 at 06:59