2

I'm using log4j and log4j.extras to create a RollingFileAppender that rolls on two conditions:

  1. The working filesize exceeds a MaxFileSize threshold.
  2. The system's date changes.

Following this guide, the appender needs both a TimeBasedRollingPolicy and a SizeBasedTriggeringpolicy, for example:

<appender name="file_logger" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="ActiveFileName" value="/fxh/logs/D3FIXFeeds.log" />
        <param name="FileNamePattern" value="/fxh/logs/D3FIXFeeds.%d{HH-mm}.%i.log" />        
    </rollingPolicy>
    <triggeringPolicy
        class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="MaxFileSize" value="50000" /> <!--  in bytes -->
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
    </layout>
</appender>

How can I set a maximum limit on the total disk space of the log output? For example, say our server admin allots 40gb for logs to this application, is there anyway to specify this given the dual rolling policies?

So far the closest I've found is the maxBackupIndex parameter. I can't event get this to work... For example, neither of these truncates the logs at only five files:

<appender name="file_logger" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="MaxBackupIndex" value="5"/>    
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
           <param name="ActiveFileName" value="/fxh/logs/D3FIXFeeds.log" />
           <param name="FileNamePattern" value="/fxh/logs/D3FIXFeeds.%d{HH-mm}.%i.log" />     
           ...

nor

<appender name="file_logger" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
           <param name="ActiveFileName" value="/fxh/logs/D3FIXFeeds.log" />
           <param name="FileNamePattern" value="/fxh/logs/D3FIXFeeds.%d{HH-mm}.%i.log" />   
           <param name="MaxBackupIndex" value="5"/>    
           ...
Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
  • I would say using log4j v1 you can't control overall log file size, only in log4j2 v2.5 they have added Delete action which can be used to delete logs based on overall size or timestamp of the last log file. – Babl Aug 01 '17 at 14:43

2 Answers2

3

As @Arigion says, doesn't look like log4j can do what you're asking, but fortunately Logback can. It provides the SizeAndTimeBasedRollingPolicy and the totalSizeCap to allow you to create logs that roll with size and time and obey a total storage limit, as below:

<appender name="file_logger" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/fxh/logs/D3FIXFeeds.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  <!-- rollover daily -->
  <fileNamePattern>/fxh/logs/D3FIXFeeds.%d{HH-mm}.%i.log</fileNamePattern>
   <maxFileSize>500KB</maxFileSize>    
   <maxHistory>60</maxHistory>
   <totalSizeCap>40GB</totalSizeCap>
</rollingPolicy>
<encoder>
  <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n</pattern>
</encoder>
</appender>

This is not a working example as I just used best guess for the fields, but let me know how it goes.

Rossiar
  • 2,416
  • 2
  • 23
  • 32
1

According to this post TimeBasedRollingPolicy does not work with MaxBackupIndex.

But you could use the TimeAndSizeRollingAppender. It has a Log file rollover by time or date, Maximum file size and a Maximum number of backup files parameter.

I don't think there is a log4j appender which observes the total directory size. It seems to me that this is more a housekeeping job and not one for a logger. Not even logrotate which is more generic that a log appender offers a directory size limit. You could use an external script (eg. triggered by cron) which does directory cleanup like that.

Arigion
  • 3,267
  • 31
  • 41