0

I Have following log4j RollingFileAppender based on file size. I wanted to customize the roll over file names. The following snippet producing the following filenames

test.log
test.log.1
test.log.2

But, I wanted to customize it to include the date and time such as

test.log
test.log.2017-10-03-14-00
test.log.2017-10-03-13-00
test.log.2017-10-03-12-00

<appender name="file" class="org.apache.log4j.RollingFileAppender">
    <param name="maxFileSize" value="10KB" />
    <param name="File" value="log/test.log" />
    <param name="threshold" value="info" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{[dd.MM.yyyy] HH:mm:ss]} - %m%n" />
    </layout>
</appender>

Can some one guide me how to do that ?

user3470629
  • 511
  • 1
  • 8
  • 20

1 Answers1

0

You should use org.apache.log4j.DailyRollingFileAppender instead

<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="log/test.log"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd-HH-mm"/>
    ...
</appender>

You may also check on

<dependency>
    <groupId>log4j</groupId>
    <artifactId>apache-log4j-extras</artifactId>
    <version>1.2.17</version>
</dependency>

Seems like https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html may provide you with the functionality you seek.

Aleh Maksimovich
  • 2,622
  • 8
  • 19
  • That creates a log file every minute right. But I want to limit to file size and roll over file to be rename to include date and time – user3470629 Oct 03 '17 at 21:17
  • I think you will have to code it yourself in this case. Please check on https://stackoverflow.com/questions/794987/how-do-you-get-log4j-to-roll-files-based-on-date-and-size – Aleh Maksimovich Oct 03 '17 at 21:22