How to configure log4j to only keep log files for

2019-01-10 18:47发布

I have the following logging problem with several Java applications using log4j for logging:

I want log files to be rotated daily, like

log.2010-09-10
log.2010-09-09
log.2010-09-08
log.2010-09-07
log.2010-09-06
log.2010-09-05
log.2010-09-04

But for data security reasons we are not allowed to keep log files for longer than seven days at my company. So the generation of the next next log file log.2010-09-11 should trigger the deletion of log.2010-09-04. Is it possible to configure such a behaviour with log4j? If not, do you know another elegant solution for this kind of logging problem?

13条回答
欢心
2楼-- · 2019-01-10 19:20

The class DailyRollingFileAppender uses the DatePattern option to specify the rolling schedule. This pattern should follow the SimpleDateFormat conventions from Std. Ed. v1.4.2. So, we have to use E option (Day in week). For example:

<param name="DatePattern" value="'.'EEE"/>

See more about DailyRollingFileAppender class from log4j javadoc here. Unfortunately the Java 1.4.2 documentation is no longer online, but you can download a copy here.

查看更多
登录 后发表回答