我正在使用log4j进行应用程序日志记录。我的应用程序部署在weblogic服务器中。我使用 RollingFileAppender 来每天轮换日志。但是,日志文件会覆盖并为每2MB创建 app.log,app.log.1 ,并且基于日期的日志轮换不起作用。任何想法如何解决问题。请在
下面找到我的 log4j.xml<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="MESSAGEDEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="File" value="/u02/appdata/logs/app.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%5p] %c{2} %m%n" />
</layout>
</appender>
<logger name="MessageLogger">
<level value="debug"/>
<appender-ref ref="MESSAGEDEBUG"/>
</logger>
</log4j:configuration>
答案 0 :(得分:0)
进入你的log4j.properties并设置:
log4j.appender.file.MaxFileSize=10MB // File max size
log4j.appender.file.MaxBackupIndex=5 // Max number of files.
关于命名日志文件,请尝试检查此线程: log4j log file names?
还有一个你可以找到有用的链接:
https://logging.apache.org/lo/org/ag4j/1.2/apidocspache/log4j/RollingFileAppender
编辑#2
我想你现在要添加:
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<!-- The file to roll to, this is a fairly intelligent parameter, if the file
ends in .gz, it gzips it, based on the date stamp it rolls at that time,
default is yyyy-MM-dd, (rolls at midnight)
See: http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html -->
<param name="FileNamePattern" value="/applogs/myportal/portal.%d.log.gz" />
</rollingPolicy>