5

I want to create size based log files. I am setting the following entry in the log4j.properties file:

log4j.appender.UserFileAppenderDebug=org.apache.log4j.RollingFileAppender

log4j.appender.UserFileAppenderDebug.Threshold=TRACE

log4j.appender.UserFileAppenderDebug.File=../log/coordinator-debug.log

log4j.appender.UserFileAppenderDebug.MaxFileSize=1KB

log4j.appender.UserFileAppenderDebug.MaxBackupIndex=7

log4j.appender.UserFileAppenderDebug.layout=org.apache.log4j.PatternLayout

log4j.appender.UserFileAppenderDebug.layout.ConversionPattern=%m%n

multiple log files are created based on size but with following names:

-rw-r--r-- 1 root root   32 Aug  6 11:28 coordinator-debug.log

-rw-r--r-- 1 root root 1.1K Aug  6 11:28 coordinator-debug.log.1

-rw-r--r-- 1 root root 1.1K Aug  6 11:28 coordinator-debug.log.2

-rw-r--r-- 1 root root 1.2K Aug  6 11:28 coordinator-debug.log.3

-rw-r--r-- 1 root root 1.1K Aug  6 11:28 coordinator-debug.log.4

-rw-r--r-- 1 root root 1.1K Aug  6 11:28 coordinator-debug.log.5

-rw-r--r-- 1 root root 1.1K Aug  6 11:28 coordinator-debug.log.6

-rw-r--r-- 1 root root 1.1K Aug  6 11:28 coordinator-debug.log.7

I would like to have the file name as follows

coordinator-debug.log.2013-08-01 11:28:39, 232

I would appreciate if you please share your comments/suggestions.

Thanks.

rana
  • 1,041
  • 2
  • 11
  • 16
  • 1
    Look at this thread. I think it will help you a lot :) [http://stackoverflow.com/questions/192456/setting-a-log-file-name-to-include-current-date-in-log4j][1] [1]: http://stackoverflow.com/questions/192456/setting-a-log-file-name-to-include-current-date-in-log4j – Kiril Aleksandrov Aug 06 '13 at 13:28
  • Kiril that link will work when using log4j xml configuration, Rana uses only properties file configuration ;) – MrSimpleMind Aug 06 '13 at 13:30

1 Answers1

4

Have you tried:

log4j.appender.UserFileAppenderDebug.DatePattern='.'yyyy-MM-dd_HH-mm-ss

(remember that your filename will not allow : e.g 11:28:39, you will need to replace them to e.g. 11-28-39 )

(edited)

Sorry, I thought you used the *Daily*RollingFileAppender...

There are RollingPolicys that you might use to get filename pattern for the RollingFileAppender.

E.g.

log4j.appender.UserFileAppenderDebug.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy

log4j.appender.UserFileAppenderDebug.RollingPolicy.FileNamePattern=../log/coordinator-debug.log.%d{yyyy-MM-dd-HH-mm-ss}
MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45