Java 如何在 Log4j 中根据时间而不是大小来轮换日志文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1711423/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:47:24  来源:igfitidea点击:

How to rotate log files based on time rather than size in Log4j?

javalog4j

提问by DivideByHero

I use Log4j with the RollingFileAppenderto create a log rotation based on size.

我使用 Log4j 和RollingFileAppender来创建基于大小的日志轮换。

How can I configure it to log to each file for a certain amount of timebefore rotating?

如何将其配置为在旋转前记录到每个文件一定时间

For example, so that each log file contains one hour of logs, rotating at the top of each hour?

例如,让每个日志文件包含一小时的日志,在每小时的顶部轮换?

I configure Log4j programatically in Java using a Propertiesobject (as opposed to a log4j.propertiesfile)

我使用Properties对象(而不是log4j.properties文件)在 Java 中以编程方式配置 Log4j

采纳答案by Rob Hruska

You probably want to use a DailyRollingFileAppender. To roll them hourly, for example, you'd use a DatePattern of '.'yyyy-MM-dd-HH. For a log4j.properties file:

您可能想要使用DailyRollingFileAppender。例如,要每小时滚动一次,您可以使用 DatePattern '.'yyyy-MM-dd-HH。对于 log4j.properties 文件:

log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH
...

Or for your programmatic configuration:

或者对于您的编程配置:

DailyRollingFileAppender appender = new DailyRollingFileAppender();
appender.setDatePattern("'.'yyyy-MM-dd-HH");

Logger root = Logger.getRootLogger();
root.addAppender(appender);

Unfortunately, using a DailyRollingFileAppender means that you can't limit the file size - this could be problematic if you have tons of logs in the given rolled period.

不幸的是,使用 DailyRollingFileAppender 意味着您无法限制文件大小 - 如果您在给定的滚动期内有大量日志,这可能会出现问题。

回答by ChssPly76

Use a DailyRollingFileAppender.

使用DailyRollingFileAppender

In particular, setting its 'datePattern' property to '.'yyyy-MM-dd-HHwould cause file to rotate every hour.

特别是,将其 'datePattern' 属性设置为'.'yyyy-MM-dd-HH会导致文件每小时轮换一次。

回答by Nathan Feger

The other thing to be careful of with any rolling file appender is to make sure only one JVM access a particular log file at a time. This is because log4j caches the log file size for performance reasons, and your 'rolling' will get wonky if multiple JVMs access the same files.

使用任何滚动文件附加器要注意的另一件事是确保一次只有一个 JVM 访问特定的日志文件。这是因为 log4j 出于性​​能原因缓存日志文件大小,如果多个 JVM 访问相同的文件,您的“滚动”将变得不稳定。

回答by Tendayi Mawushe

You need to use the DailyRollingFileAppender. Despite its misleading name it can be configured in a to run at configurable time periods up to minutes.

您需要使用DailyRollingFileAppender。尽管它的名称具有误导性,但它可以配置为在最多几分钟的可配置时间段内运行。

回答by pradeekrathnayaka

Additionally,

此外,

log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
**log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH**

The following list shows all the date patterns which have defined by log4j,

以下列表显示了 log4j 定义的所有日期模式,

Minutely  '.'yyyy-MM-dd-HH-mm application.log.2013-02-28-13-54
Hourly '.'yyyy-MM-dd-HH application.log.2013-02-28-13
Half-daily '.'yyyy-MM-dd-a application.log.2013-02-28-AM app.log.2013-02-28-PM
Daily '.'yyyy-MM-dd application.log.2013-02-28
Weekly '.'yyyy-ww application.log.2013-07 app.log.2013-08 
Monthly '.'yyyy-MM application.log.2013-01 app.log.2013-02