Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...