Samurai
Samurai

Reputation: 843

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.

Upvotes: 9

Views: 31236

Answers (5)

Curtis Yallop
Curtis Yallop

Reputation: 7309

The size-based trigger won't work unless you add "%i" to FileNamePattern. Otherwise the filename doesn't change so the size-based trigger does not cause a roll.

(in addition to the already-mentioned MaxFileSize being a long "bytes" value without units)

One good source: https://www.baeldung.com/java-logging-rolling-file-appenders#5-rolling-based-on-size-and-time

Upvotes: 1

psurikov
psurikov

Reputation: 3458

A bit more details about how you can specify the size for log4j2:

<Policies>
    <SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>

If you check the source code for file size parsing, the regular expression that is used to parse the value is the following:

/**
* Pattern for string parsing.
*/
private static final Pattern VALUE_PATTERN = 
Pattern.compile("([0-9]+([.,][0-9]+)?)\\s*(|K|M|G|T)B?", Pattern.CASE_INSENSITIVE);

I haven't seen this anywhere in documentation, but basically you can either specify a number (then it will denote a total number of bytes), or use KB,MB,GB,TB to specify units of measurement. Also you can use fractional numbers and have a whitespace between the number and the unit. The units are case-insensitive, so it should be possible to specify kb, or KB.

Upvotes: 1

KumN
KumN

Reputation: 51

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.

Upvotes: 5

krishnakumarp
krishnakumarp

Reputation: 9295

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.

Upvotes: 5

Gabriel Ruiu
Gabriel Ruiu

Reputation: 2803

just happened to come across this question and thought I should share the solution:

set the MaxFileSize param to a value in bytes, so for your example you would set it as

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

Here is a similar question where this solution is confirmed.

Upvotes: 0

Related Questions