差異處
這裏顯示兩個版本的差異處。
下次修改 | 前次修改 | ||
java:log4j2:timeandsizerollingappender [2022/04/10 21:53] tony 建立 |
java:log4j2:timeandsizerollingappender [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
{{tag>log4j2}} | {{tag>log4j2}} | ||
====== 在Log4j2設定TimeAndSizeRollingAppender ====== | ====== 在Log4j2設定TimeAndSizeRollingAppender ====== | ||
- | + | ===== Introduction ===== | |
- | 這個是基於前一種RollingFileAppender多增加以時間為滾動條件的設定方式。舉例來說,當到隔天的00:00時,我想產生新的log file就會用到這個條件。在log4j1時,要做到這個需求,需要引入另外一個支援TimeAndSizeRollingAppender的library;而在logj2中,則可直接透過TimeBasedTriggeringPolicy做到這個需求。除此之外,如果要做到最大檔案數量控制的話,要多使用Delete action的特性: | + | 在Log4j1中,可以引入支援TimeAndSizeRollingAppender的library去支援基於檔案大小+時間+檔案數量三個條件去滾動檔案。當到隔天的00:00時,我想產生新的log file就會使用到這種設定方式。本篇要分享在log4j2的設定方法。 |
+ | ===== TimeBasedTriggeringPolicy+Delete Action ===== | ||
+ | 在logj2中,提供了TimeBasedTriggeringPolicy讓你可以完成這個需求。然而,如果要做到最大檔案數量控制的話,則要多使用Delete action的特性。我首先說明與一般[[java:log4j2:appenders#rollingfileappender_with_size_and_file_count_conditions|RollingFileAppender只有檔案大小與檔案數量]]設定的不同之處: | ||
<code properties> | <code properties> | ||
appender.logfile.type = RollingFile | appender.logfile.type = RollingFile | ||
行 29: | 行 31: | ||
appender.logfile.strategy.action.basepath = ./syslog/ | appender.logfile.strategy.action.basepath = ./syslog/ | ||
appender.logfile.strategy.action.maxDepth = 1 | appender.logfile.strategy.action.maxDepth = 1 | ||
- | appender.logfile.strategy.action.IfFileName.type = IfFileName | + | appender.logfile.strategy.action.ifFileName.type = IfFileName |
- | appender.logfile.strategy.action.IfFileName.glob = log.txt* | + | appender.logfile.strategy.action.ifFileName.glob = log.txt* |
- | appender.logfile.strategy.action.IfFileName.IfAccumulatedFileCount.type = IfAccumulatedFileCount | + | appender.logfile.strategy.action.ifFileName.IfAccumulatedFileCount.type = IfAccumulatedFileCount |
- | appender.logfile.strategy.action.IfFileName.IfAccumulatedFileCount.exceeds = 10 | + | appender.logfile.strategy.action.ifFileName.IfAccumulatedFileCount.exceeds = 10 |
</code> | </code> | ||
* **appender.logfile.strategy.action.type**: 設定為Delete,代表要使用Delete Action。 | * **appender.logfile.strategy.action.type**: 設定為Delete,代表要使用Delete Action。 | ||
行 45: | 行 47: | ||
appender.logfile.filePattern = ./syslog/log.txt.%d{yyyy-MM-dd-HH-mm}.%i | appender.logfile.filePattern = ./syslog/log.txt.%d{yyyy-MM-dd-HH-mm}.%i | ||
</code> | </code> | ||
+ | ===== Multiple Path Conditions ===== | ||
+ | 前面所提到的pathConditions,是可以支援多個條件的。以前面的例子來說,是設了一組nested的condition,假如改設定成多組會長這個樣子: | ||
+ | <code properties> | ||
+ | appender.logfile.strategy.action.ifFileName.type = IfFileName | ||
+ | appender.logfile.strategy.action.ifFileName.glob = log.txt* | ||
+ | appender.logfile.strategy.action.ifAccumulatedFileCount.type = IfAccumulatedFileCount | ||
+ | appender.logfile.strategy.action.ifAccumulatedFileCount.exceeds = 10 | ||
+ | </code> | ||
+ | 根據文件描述與目前實驗起來,上面的關係是屬於IfAll的關係。另外記錄一下,目前我們使用的log4j2版本為2.17.2,我發現當設定下面兩組properties時,雖然合理但其實沒有作用。第一組是把key從ifFileName改成IfFileName: | ||
+ | <code properties> | ||
+ | appender.logfile.strategy.action.IfFileName.type = IfFileName | ||
+ | appender.logfile.strategy.action.IfFileName.glob = log.txt* | ||
+ | appender.logfile.strategy.action.ifAccumulatedFileCount.type = IfAccumulatedFileCount | ||
+ | appender.logfile.strategy.action.ifAccumulatedFileCount.exceeds = 10 | ||
+ | </code> | ||
+ | 第二組是在最上層設定了IfAll是沒有作用的.. | ||
+ | <code properties> | ||
+ | appender.logfile.strategy.action.ifAll.type = IfAll | ||
+ | appender.logfile.strategy.action.ifAll.ifFileName.type = IfFileName | ||
+ | appender.logfile.strategy.action.ifAll.ifFileName.glob = log.txt* | ||
+ | appender.logfile.strategy.action.ifAll.ifAccumulatedFileCount.type = IfAccumulatedFileCount | ||
+ | appender.logfile.strategy.action.ifAll.ifAccumulatedFileCount.exceeds = 10 | ||
+ | </code> | ||
+ | 這就不曉得log4j2是否存在某種潛規則了.. | ||
+ | ===== Reference ===== | ||
+ | * [[https://www.cnblogs.com/yeyang/p/7944899.html|Log4j2中RollingFile的文件滚动更新机制]] | ||
+ | * [[https://logging.apache.org/log4j/2.x/manual/appenders.html#CustomDeleteOnRollover|官方Delete Action介紹]] | ||
+ | ===== ===== | ||
+ | ---- | ||
+ | \\ | ||
+ | ~~DISQUS~~ |