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.

Samurai
  • 843
  • 6
  • 23
  • 44
  • 2
    Just a guess, but did you try to replace 3kb with 3072? In the JavaDoc (http://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. – Ethan Leroy Oct 18 '12 at 11:54
  • See my answer to a similar question: http://stackoverflow.com/questions/13936021/rolling-logs-by-size-and-time/14202093#14202093 – David Peleg Jan 07 '13 at 18:48
  • @Samurai, have you found the solution for this? – Rajeev Apr 24 '17 at 11:37

4 Answers4

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.

krishnakumarp
  • 8,967
  • 3
  • 49
  • 55
4

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.

KumN
  • 41
  • 3
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.

Community
  • 1
  • 1
Gabriel Ruiu
  • 2,753
  • 2
  • 19
  • 23
0

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.

username
  • 3,378
  • 5
  • 44
  • 75