Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j file renaming strategy

Tags:

java

log4j

I'm using log4j 1.2.15 and I wonder if someone has a remedy to the annoying file renaming strategy log4j is using.

I'll try to explain: I am using the following RollingFileAppender that creates 11 log files each one 3KB size.

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=server.log log4j.appender.logfile.MaxFileSize=3KB log4j.appender.logfile.MaxBackupIndex=10

The problem is that while creating a new log file, log4j creates server.log.1, server.log.2 etc...

Can I configure log4j to keep the ".log" suffix, and create for instance server.1.log?

Thanks in advance Guy

like image 745
Guy Avatar asked Apr 10 '11 12:04

Guy


People also ask

How do I rename a log4j file?

renameTo() to change the filename.

Can we rename log4j properties?

You can rename the log4j properties file to any other name and then through your code, you can use the PropertyConfigurator to load that file.

What is rolling file Appender in log4j?

RollingFileAppender extends FileAppender to backup the log files when they reach a certain size. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org. apache. log4j. rolling.


1 Answers

I'm doing this using an xml configuration (might be possible without) and Apache Extras Companion™ for Apache log4j™ (an extra library for log4j). I don't think this works without the extra's, but it might.

The key is to use the FileNamePattern.

<appender name="logger" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
        <param name="FileNamePattern" value="/server.%i.log" />
        <param name="MaxIndex" value="10" />
    </rollingPolicy>

    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="MaxFileSize" value="3000" /> 
    </triggeringPolicy>
</appender>
like image 87
Thirler Avatar answered Oct 06 '22 02:10

Thirler