12
votes

Is it possible with any of the log4j appenders to write the logs with specific rotation time and retention limit.
The goal would be:

  • to have a log file for each day; create a new file at midnight for the new logs
  • to keep the log files and delete them automatically after a certain amount of time; so delete log files older than X days (e.g. 30 days)

It seem that the rotation is possible but the limit of the retention time is not possible with log4j

The log4j version is 1.2.

4

4 Answers

7
votes

this log4J properties work for me

log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
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

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=/opt/cronjob/logs/cronlogs.log
log4j.appender.file.MaxFileSize=1028MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
4
votes

My answer is based on logback, not log4j (sorry for the confuse..)


You can achieve that log rotation by using TimeBasedRollingPolicy.

for example)

<appender name="SYSTEMLOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>./logs/system.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>./logs/system.log.%d{yyyy-MM-dd}</fileNamePattern>
    <!-- keep last 30 days of logs -->
    <maxHistory>30</maxHistory>
  </rollingPolicy>
  <encoder>
    <charset>UTF-8</charset>
      <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %msg %n</Pattern>
  </encoder>
</appender>

It will rotate at midnight, and will delete log files older than 30 days.

fileNamePattern: The rollover period is inferred from the value of fileNamePattern

maxHistory: The optional maxHistory property controls the maximum number of archive files to keep, asynchronously deleting older files. For example, if you specify monthly rollover, and set maxHistory to 6, then 6 months worth of archives files will be kept with files older than 6 months deleted. Note as old archived log files are removed, any folders which were created for the purpose of log file archiving will be removed as appropriate.

You can check more information on TimeBasedRollingPolicy

3
votes

yes we can!

file: project->src->main->resources->logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>folderName/logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>folderName/archive/logFile.%d{dd-MM-yyyy}.log</fileNamePattern>
      <!-- keep last 30 days of logs -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <!-- filter by level (optional) -->
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>DEBUG</level>
    </filter>

    <encoder>
      <pattern>%date %-5level [%thread] %file:%L [%M] - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- you can filter by type of level (optional) -->
  <root level="DEBUG">
    <appender-ref ref="FILE"/>
  </root>
</configuration>

for more info see documentation: TimeBasedRollingPolicy

JavaDoc: TimeBasedRollingPolicy

3
votes

Most of the answers are based on logback. But the question is about log4j 1.2 (old...)

the answer mentioning DailyRollingFileAppender will not work either. org.apache.log4j.DailyRollingFileAppender does not support the MaxBackupIndex property see http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html (this is for RollingFileAppender )

you may be interested in : Use MaxBackupIndex in DailyRollingFileAppender -log4j

and for a possible answer : Log4j Rollingpolicy and MaxbackupIndex

But you should probably use the slf4j log4j "emulation" (http://www.slf4j.org/legacy.html#log4j-over-slf4j) and route your log through logback (without any code change) where it's far easier to implements.