Log4j : How to resolve Warning : Unrecognized element - rollingPolicy , triggeringPolicy

心不动则不痛 提交于 2019-12-13 08:38:29

问题


My Java project contains following packages - com.main.log4j.main , com.main.log4j.other.

As per my requirement, I have to exclude all log lines of package "com.main.log4j.other" from console and created a new log file "OTHER_LOG.log"

The below config.xml is working properly.

Now, I want, if it ("OTHER_LOG.log") exceeds certain size lets say 1 MB, it will create new file, keeping the previous file as backup but with timestamp.

Like, "OTHER_LOG.20190221155085.log"

That's why I have added "rollingPolicy", "triggeringPolicy".

But It gives warning -
log4j:WARN Unrecognized element rollingPolicy
log4j:WARN Unrecognized element triggeringPolicy

<appender name="CONSOLE-LOG" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="..." />
</layout>
 </appender>
 <appender name="OTHER-LOG"
    class="org.apache.log4j.FileAppender">
    <param name="Threshold" value="DEBUG" />
    <param name="File" value="logs/OTHER_LOG.log" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="fileNamePattern" value="logs/OTHER_LOG.%d{yyyyMMddHHmmss}.%i.log" />
    </rollingPolicy>
    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="maxFileSize" value="1000000" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="..." />
    </layout>
</appender>

<category name="com.main.log4j.other" additivity="false">
   <appender-ref ref="OTHER-LOG" />
</category>
<root>
<level value="debug" />
   <appender-ref ref="CONSOLE-LOG" />
</root>

I have tried with log4j version - 1.2.16,1.2.17; Still getting the warning and my purpose is not fulfilled. Please let me know where I have done the mistake.


回答1:


For OTHER_LOG appender I have used org.apache.log4j.FileAppender, but rollingPolicy will not work with FileAppender, rather we should use org.apache.log4j.rolling.RollingFileAppender.



来源:https://stackoverflow.com/questions/54805122/log4j-how-to-resolve-warning-unrecognized-element-rollingpolicy-triggeri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!