如何通过log4j控制日志文件的每日滚动和最大文件大小?
我想创建可以在第二天开始时滚动的日志文件,或者如果达到指定的文件大小,并且日志文件必须包含在日期文件夹内。 文件夹的格式为 YYYYMMDD
(/20111103/mylogfile.log
)
是否可以通过 Log4j 执行此操作而不实现自定义类?
现在我使用 log4j 和 log4j-extra, 我将 Log4j API 中定义的 FileNamePattern 属性设置为每天滚动我的文件 并将最大文件大小设置为 50 MB。
我的 log4j.xml
是:
<appender name="MYAPPENDER" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="append" value="true" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="${catalina.home}/logs/MY-APP/%d{yyyyMMdd}/MY-APP_%d{yyyyMMddHHmmss}.log" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="maxFileSize" value="50000000" />
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yyyy HH\:mm\:ss}] %-5p [%c.%M(),%4L] - %m%n" />
</layout>
</appender>
上述设置的结果是日志文件不会在接下来的几天开始时滚动,但如果文件的大小达到 50 MB,日志文件将被滚动。
请帮忙给我建议。毫米
I would like to create log file that can be rolled at the beginning of the next day or if it's reached to specified file size and log file must be contained inside date folder.
Format of folder is YYYYMMDD
(/20111103/mylogfile.log
)
Is it possible to do this by Log4j without implementing custom class?
Now I am using log4j and log4j-extra,
I set FileNamePattern attribute as defined in log4j API to rolling my file everyday
and set max file size to 50 MB.
My log4j.xml
is:
<appender name="MYAPPENDER" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="append" value="true" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="${catalina.home}/logs/MY-APP/%d{yyyyMMdd}/MY-APP_%d{yyyyMMddHHmmss}.log" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="maxFileSize" value="50000000" />
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yyyy HH\:mm\:ss}] %-5p [%c.%M(),%4L] - %m%n" />
</layout>
</appender>
Result of above setting is that log file is not rolled at the beginning of next days but if file's size reached to 50 MB, log file will be rolled.
Please help to advise me. m(_ _)m
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Daily 对我来说适用,有问题的 xml 仅转换为 log4j.properties 相当于在 100KB 后滚动(用于测试目的):
这将创建一个带有时间戳的目录(每天一个目录 - 可选),其日志具有以下格式(每小时一个)出于测试目的 - 更改 FileNamePattern 以满足您的需求):
myCompanyLog_201602031030.log
myCompanyLog_201602031130.log
Daily works for me with xml in question only transformed into log4j.properties equivalent to roll over after 100KB (for testing purposes):
This will create a timestamped directory (one directory per day - optional) with logs with the following format (one per hour for testing purposes - change FileNamePattern to suit your needs):
myCompanyLog_201602031030.log
myCompanyLog_201602031130.log
要启用每日滚动: class="org.apache.log4j.DailyRollingFileAppender"
并启用最大文件大小和备份文件数量
,但不能设置
MaxFileSize
与DailyRolling
一起使用,因此您可以使用滚动文件附加程序示例:
或者您可以执行以下操作 http://wiki.apache.org/logging-log4j/DailyRollingFileAppender
To enable the daily rolling: class="org.apache.log4j.DailyRollingFileAppender"
And to enable the max file size and the number of backup files
But you can not put
MaxFileSize
withDailyRolling
, so you can use rolling file appenderAn example:
Or you can do this http://wiki.apache.org/logging-log4j/DailyRollingFileAppender
来自 RollingFileAppender 文档
因此,由于上面配置了 TimeBasedRollingPolicy,因此您的 SizeBasedTriggeringPolicy 会被忽略。满足您的要求的唯一方法是自定义类实现。
另外,如果日志文件的大小确实很重要,您可以考虑使用 自动 gzip 压缩,以消除 SizeBasedTriggeringPolicy,并且只让您的日志每天滚动。
From the RollingFileAppender documentation
Hence, your SizeBasedTriggeringPolicy gets ignored since TimeBasedRollingPolicy is configured above. The only way to accommodate your requirement will be a custom classes implementation.
Also, If size of a log file really matters, you may consider using automatic gzip compression to eliminate SizeBasedTriggeringPolicy and only have your logs roll every day.