Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 am running file it is throwing below error.

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

How can I fix this. Please help me.

like image 1000
Samurai Avatar asked Sep 04 '12 05:09

Samurai


People also ask

How do I use RollingFileAppender in log4j properties?

3.1. If using RollingFileAppender , then use TimeBasedRollingPolicy to specify when to roll over log files based on date time. Notice the FileNamePattern property. It defines the name pattern for rolled over files. In given example, it will rename the rollover log files with date-month in log file name.

What is SizeBasedTriggeringPolicy?

SizeBasedTriggeringPolicy looks at the size of the currently active file. If it grows larger than the specified size, it will signal the owning RollingFileAppender to trigger the rollover of the existing active file.

How do you create a new log file for each time the application runs log4j?

File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.

What is Appender rolling?

RollingFileAppender is a file appender which rolls over the log files once it has reached a certain size limit or date/time pattern no longer applies. In this post, I will show you how to use the RollingFileAppender to backup and compress the old log files based on - Date and Time.


2 Answers

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

Relevant XML 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

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

like image 182
krishnakumarp Avatar answered Sep 30 '22 20:09

krishnakumarp


As per documentation, it has to be long value for MaxFileSize. Please check at https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/SizeBasedTriggeringPolicy.html

So, the value should be 3072 instead of 3kb in this case.

like image 32
KumN Avatar answered Sep 30 '22 20:09

KumN