How to use SizeBasedTriggeringPolicy with TimeBasedRollingPolicy in Log4j?

Hi, I am using Log4j for logging. Below is my configuration.

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="FileAppender_Comp3" class="org.apache.log4j.rolling.RollingFileAppender"> <rollingPolicy name="file" class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="FileNamePattern" value="log/Comp3_%d{dd-MM-yyyy HH-mm-ss}.log" /> </rollingPolicy> <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy"> <param name="MaxFileSize" value="3kb"/> </triggeringPolicy> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %5p [%t] %c (%F:%L) - %m%n"/> </layout> </appender> 

But when I run the file, it throws an error below.

 log4j:WARN Failed to set property [maxFileSize] to value "3kb". 

How can I fix this. Please help me.

+7
source share
2 answers

If you use Log4j 2 , you can specify the size in KB or MB.

The corresponding XML is below.

 <Policies> <!-- Starts a new log on tomcat start --> <OnStartupTriggeringPolicy /> <!-- Starts a new file when size reaches threshold --> <SizeBasedTriggeringPolicy size="10 MB" /> <!-- causes a rollover once the date/time pattern no longer applies to the active file --> <TimeBasedTriggeringPolicy /> </Policies 

See https://logging.apache.org/log4j/2.x/manual/appenders.html for more details.

+2
source

just ran into this question and thought I should share a solution:

set the MaxFileSize parameter to in bytes, so for your example you should set it as

 <param name="MaxFileSize" value="3072"/> 

Here is a similar question, where is this decision confirmed.

0
source

All Articles