java 如何在 Log4j 中使用 SizeBasedTriggeringPolicy 和 TimeBasedRollingPolicy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12257477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:11:57  来源:igfitidea点击:

How to use SizeBasedTriggeringPolicy with TimeBasedRollingPolicy in Log4j?

javalog4jrollingfileappender

提问by Samurai

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

嗨,我正在使用 Log4j 进行日志记录。下面是我的配置。

<?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.

我怎样才能解决这个问题。请帮我。

回答by krishnakumarp

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

如果您使用的是Log4j 2,则可以以 KB 或 MB 为单位指定大小。

Relevant XML below.

下面的相关 XML。

<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.htmlfor more details.

有关更多详细信息,请参阅https://logging.apache.org/log4j/2.x/manual/appenders.html

回答by KumN

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

根据文档,MaxFileSize 必须是长值。请查看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.

因此,在这种情况下,该值应为 3072 而不是 3kb。

回答by Gabriel Ruiu

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

将 MaxFileSize 参数设置为以字节为单位的值,因此对于您的示例,您可以将其设置为

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

Hereis a similar question where this solution is confirmed.

是一个类似的问题,其中确认了此解决方案。