17

I want daily logs with the log file appended with the date in yyyy-dd-mm format. When I use DailyRollingFileAppender, a new log file is not created. The logs are written to the same log file. Also, the date Pattern provided is not considered. The log file created is LoggerFile.log. And every content(even on the next day) is written to this file.

I am using the log4j-1.2.17 jar. I am developing in Netbeans 7.3.1 in Java.

Is there anyone using this JAR and facing such a problem. Please help!

Here is the content of the properties file I use:

# Root logger option
log4j.rootLogger=ERROR,FILE,stdout

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender

log4j.appender.FILE.File=.//..//logs//LoggerFile.log

# Define the layout for file appender
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS zzz} %5p     %c{1}:%L - %m%n

log4j.appender.FILE.MaxFileSize=10MB


# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -  %m%n`

3 Answers3

6

I had similar requirements of daily log file rotation.. (Though the question is older, thought the answer would help others)..

Key points:

  1. First of all, we can avoid using DailyRollingFileAppender. Why? DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender. Reference: Documentation of Daily Rolling File Appender

  2. Apache extras log4j, Would suggest to use org.apache.log4j.rolling.RollingFileAppender with the Time based rolling policy

  3. May find a sample configuration for time based rotation of log.

Hope this helps.

Santosh Kumar Arjunan
  • 3,600
  • 3
  • 23
  • 24
3

First of all you have an error because the DailyRollingFileAppender does not support the property MaxFileSize.

Then you can try to remove the char ' from the DatePattern:

Try changing

log4j.appender.FILE.DatePattern='.'yyyy-MM-dd

to

log4j.appender.FILE.DatePattern=.yyyy-MM-dd
David Maust
  • 8,080
  • 3
  • 32
  • 36
Enrico Suci
  • 35
  • 1
  • 7
  • 1
    removing `'` may not be the right solution. `This pattern should follow the SimpleDateFormat conventions. In particular, you must escape literal text within a pair of single quotes. `, per the [document](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html) – XoXo Mar 24 '16 at 13:22
-1

Try to add another line log4j.appender.file.Append=true

The full code looks like below

            # Root logger option
            log4j.rootLogger=DEBUG, file, stdout

            # Daily rolling file appender
            log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
            log4j.appender.file.File=logs/mylogs.log
            log4j.appender.file.Append=true
            log4j.appender.file.DatePattern='.'dd-MM-yyyy
            log4j.appender.file.MaxFileSize=10MB
            log4j.appender.file.MaxBackupIndex=100
            log4j.appender.file.encoding=UTF-8
            log4j.appender.file.layout=org.apache.log4j.PatternLayout
            log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
Jay
  • 9,189
  • 12
  • 56
  • 96
  • I am using log4j 1.2.16. I get the following warnings using your properties: `log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.DailyRollingFileAppender.` and `log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.` Should those properties be included in your answer? – stackoverflowuser2010 May 07 '15 at 01:58
  • 1
    @stackoverflowuser2010 When you use DailyRollingFileAppender with DatePattern, you cannot use MaxFileSize nor MaxBackupIndex. Just remove those two properties, then the warnings will go away. You would be able to use MaxFileSize and MaxBackupIndex if you do not use DatePattern property. – wolf97084 Aug 05 '15 at 20:23
  • This is not for for me. – Thilina Sampath Nov 17 '15 at 12:19