How to use SizeBasedTriggeringPolicy with TimeBasedRollingPolicy in Log4j?
Asked Answered
A

5

9

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.

Aggravation answered 4/9, 2012 at 5:50 Comment(3)
Just a guess, but did you try to replace 3kb with 3072? In the JavaDoc (goo.gl/ahbl8) I found "Sets rollover threshold size in bytes", so I think you can't pass the value with KB, MB or another suffix.Cryostat
See my answer to a similar question: #13936521Honest
@Samurai, have you found the solution for this?Unequaled
U
5

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.

Unknowable answered 3/7, 2015 at 17:12 Comment(2)
What if we don't specify KB/MB and just a number size=10 What would it take default?Nicole
it's either KB, MB, and GB now.Diner
H
5

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.

Headforemost answered 16/10, 2018 at 18:49 Comment(0)
C
1

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.

Capstan answered 16/11, 2022 at 22:57 Comment(0)
S
1

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

Smokestack answered 15/9, 2023 at 5:29 Comment(0)
B
0

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.

Bear answered 11/6, 2014 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.