gpt4 book ai didi

java - Log4j - DailyRollingFileAppender - 滚动文件是否在一段时间后被删除?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:27 48 4
gpt4 key购买 nike

This answer表示日志保留 7 天。这是可配置的吗?

Java Ranch Pos t 表示文件永远不会被删除。

本网站还同意永远不会删除日志文件:

If you are trying to use the Apache Log4J DailyRollingFileAppender for a daily log file, you may need to want to specify the maximum number of files which should be kept. Just like rolling RollingFileAppender supports maxBackupIndex. But the current version of Log4j (Apache log4j 1.2.16) does not provide any mechanism to delete old log files if you are using DailyRollingFileAppender. I tried to make small modifications in the original version of DailyRollingFileAppender to add maxBackupIndex property. So, it would be possible to clean up old log files which may not be required for future usage.

Source

我找不到权威的答案,我不想等 7 天才能看到我的日志是否被删除。

最佳答案

查看此 this post关于 Log4J 删除。简而言之,dailyRollingFileAppender 似乎不能。但也许你可能想看看切换到 Logback .它是由同一个人编写的,可以满足您的需求。

我使用以下附加程序来维护 30 天的 HTML 日志:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>Logs\logFile.html</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.html</fileNamePattern>

<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>

<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
</layout>
</encoder>
</appender>

relevant section手册中关于 maxHistory 的说明如下:

The optional maxHistory property controls the maximum number of archive files to keep, deleting older files. For example, if you specify monthly rollover, and set maxHistory to 6, then 6 months worth of archives files will be kept with files older than 6 months deleted. Note as old archived log files are removed, any folders which were created for the purpose of log file archiving will be removed as appropriate.

Logback 也有一个可用的 Log4J 属性转换器 here帮助过渡。以及 entire chapter in their manual致力于从 Log4J 切换。

关于java - Log4j - DailyRollingFileAppender - 滚动文件是否在一段时间后被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13864899/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com