Log4j disable rolling create files based on size

log4j

I trying to find a ready made jar/class that gives me the option to
enable log4j write to files endlessly based on file size , what i mean is this:
configure log4j to :
1. write to log file until file size is 10 mega
2. when file reach to 10 mega it will be renamed to "file_"+current date suffix and thats it .
3. start new logfile go to 1

each time the file reach to 10240KB it rename the file engine.log to engine_1.log
and then when the main engine.log reach again to 10240KB
it rename the engine_1.log to engine_2.log
and engine.log to engine_1.log and so on ..
this is the action i like to AVOID !
the problem it cause for example :
so if i look at content in engine_1.log and after 10 min's the content in the same file will be changed

i just want simple writing to logs without renaming the files each time.
i hope i made my self clear .

Best Answer

Achieving this is not possible with the standard Log4j library, but it is implemented in the Log4j Extras Companion library, which is a separate JAR file and can be downloaded from here.

Standard RollingFileAppender performs log rotation (or rolling) based on the log file size. If the log file reached the specified size limit, a new log file is started and the old log file will be renamed to have a _1 suffix. Existing archive logs will also be rotated with _2, _3, etc. suffixes.

What you need is to override this default log file naming policy to use current date in archive log file names instead of a counter. This can be configured with the TimeBasedRollingPolicy filter (here is the documentation).

Modify your Log4J configuration file (log4j.xml):

<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender"> 
    <param name="Threshold" value="DEBUG"/>   
    <param name="File" value="engine.log"/>
    <param name="Append" value="true"/>
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="engine_%d.log" />
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %t %-5p [%c{1}] %m%n "/>
    </layout>
</appender>

I've taken this example configuration from the official Log4J wiki (see Filter configuration and More examples sections at the bottom of the page).

Update:

The RollingFileAppender class from the extras library does not have the appropriate attribute for setting the maximum log file size as the old RollingFileAppender had. This property can be specified using a triggering policy, in this case the SizeBasedTriggeringPolicy should be used (documentation is here). The configuration of the triggering policy is the same as for the rolling policy (I show only the relevant part):

<appender ...>
    ...
    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="MaxFileSize" value="10000000" />
    </triggeringPolicy>
    ...
</appender>
Related Topic