java 按大小和时间滚动日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13936021/
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
Rolling logs by both size and time
提问by Michael
I use RollingFileAppender
of log4j 1.2.16
, which rolls log files, when they reach a certain size. Now I would like to roll log files daily andwhen they reach a certain size. Thus there will be one or more log files per day.
我用RollingFileAppender
的log4j 1.2.16
,这卷日志文件,当他们达到一定规模。现在我想每天滚动日志文件,并在它们达到一定大小时滚动。因此,每天将有一个或多个日志文件。
For example,
例如,
myapp.log myapp-17.12.2013.log myapp-16.12.2012.log myapp-16.12.2012.1.log myapp-16.12.2012.2.log
Is there an off-the-shelf appender, which does it already?
是否有现成的 appender,它已经做到了?
采纳答案by David Peleg
There are indeed two options:
确实有两种选择:
- use LogBack with its size and time triggering policy: http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
- there is TimeAndSizeRollingAppender for Log4J from here: http://www.simonsite.org.uk/
- 使用 LogBack 及其大小和时间触发策略:http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
- Log4J 的 TimeAndSizeRollingAppender 来自这里:http: //www.simonsite.org.uk/
Keep in mind that both options use file renames. Consider this carefully if there's another script automatically moving these files. File rename is risky when two processes deal with the same file.
请记住,这两个选项都使用文件重命名。如果有另一个脚本自动移动这些文件,请仔细考虑这一点。当两个进程处理同一个文件时,文件重命名是有风险的。
My suggestion is to directly write to immutable log file name in the pattern: myapp-{dd.MM.yyyy}.{X}.log. That way "rolling" is simply closing one file and opening a new one. No renames. No background threads.
我的建议是直接写入模式中的不可变日志文件名:myapp-{dd.MM.yyyy}.{X}.log。这种“滚动”方式只是关闭一个文件并打开一个新文件。没有重命名。没有后台线程。
回答by izaera
The quick answer is "no". Looking at log4j's javadoc: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/FileAppender.html
快速回答是“不”。查看 log4j 的 javadoc:https: //logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/FileAppender.html
There are only two out-of-the-box file appenders: DailyRollingFileAppender and RollingFileAppender (and the first one is not recommended because it has synchronization issues).
只有两个开箱即用的文件附加程序:DailyRollingFileAppender 和 RollingFileAppender(不推荐第一个,因为它存在同步问题)。
To achieve what you want, you should create your own appender, extending RollingFileAppender and modifying it to roll the file if the day changes. The modification would be in method:
为了实现你想要的,你应该创建你自己的 appender,扩展 RollingFileAppender 并修改它以在日期改变时滚动文件。修改将在方法中:
protected void subAppend(LoggingEvent event)
You can see its source here: http://www.docjar.com/html/api/org/apache/log4j/RollingFileAppender.java.html(line 274).
您可以在此处查看其来源:http: //www.docjar.com/html/api/org/apache/log4j/RollingFileAppender.java.html(第 274 行)。
You just need to copy and paste the code and change the if calling rollOver to suit your needs.
您只需要复制和粘贴代码并更改 if 调用 rollOver 以满足您的需要。
回答by brajesh kumar
Below configuration xml will do the job: JAR required: log4j-rolling-appender-20150607-2059
下面的配置 xml 将完成这项工作:需要 JAR:log4j-rolling-appender-20150607-2059
<?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="file"
class="uk.org.simonsite.log4j.appender.TimeAndSizeRollingAppender">
<param name="File" value="D:\App.log" />
<param name="Threshold" value="DEBUG" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<param name="MaxFileSize" value="1KB" />
<param name="MaxRollFileCount" value="100" />
<param name="ScavengeInterval" value="30000" />
<param name="BufferedIO" value="false" />
<param name="CompressionAlgorithm" value="GZ" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="file" />
</root>
</log4j:configuration>