差異處
這裏顯示兩個版本的差異處。
java:log4j2:timeandsizerollingappender [2022/04/10 22:41] tony [Multiple Path Conditions] |
java:log4j2:timeandsizerollingappender [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>log4j2}} | + | |
- | ====== 在Log4j2設定TimeAndSizeRollingAppender ====== | + | |
- | ===== Introduction ===== | + | |
- | 在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> | + | |
- | appender.logfile.type = RollingFile | + | |
- | appender.logfile.name = LOGFILE | + | |
- | appender.logfile.fileName = ./syslog/log.txt | + | |
- | appender.logfile.filePattern = ./syslog/log.txt.%d{yyyy-MM-dd}.%i | + | |
- | appender.logfile.layout.type = PatternLayout | + | |
- | appender.logfile.layout.pattern = %d{yyyy/MM/dd HH:mm:ss.SSS} %5p[%t] (%F:%M:%L) - %m%n | + | |
- | appender.logfile.policies.type = Policies | + | |
- | appender.logfile.policies.size.type = SizeBasedTriggeringPolicy | + | |
- | appender.logfile.policies.size.size=8000KB | + | |
- | appender.logfile.policies.time.type = TimeBasedTriggeringPolicy | + | |
- | appender.logfile.policies.time.interval = 1 | + | |
- | appender.logfile.policies.time.modulate = true | + | |
- | appender.logfile.strategy.type = DefaultRolloverStrategy | + | |
- | appender.logfile.strategy.fileIndex = nomax | + | |
- | </code> | + | |
- | * **appender.logfile.policies.time.type**: 在這使用了TimeBasedTriggeringPolicy。key中的time也是用來區別key而已,你喜歡也可以取你愛的名字。 | + | |
- | * **appender.logfile.policies.time.interval**: 滾動的間隔,需與filePattern搭配。 | + | |
- | * **appender.logfile.policies.time.modulate**: 簡單來說如果你間隔是以天為單位的話,設定為true它就會幫你從00:00開始加interval;設定為false就看你程式何時執行了.. | + | |
- | * **appender.logfile.filePattern**: 檔案名稱需要加上時間格式,以控制滾動規則。需與TimeBasedTriggeringPolicy的interval與modulate搭配。以範例來說,就是每天的00:00會滾動log一次,將前一天的log.txt變為log.txt.2022-04-10.%i。 | + | |
- | * **appender.logfile.strategy.fileIndex**: 這裡我就沒套用max index,檔案最大數量的控制交給了Delete Action。 | + | |
- | Delete Action的設定比較彈性,可以根據目錄規則、檔案名稱、修改時間、檔案數量等等去定義刪除條件,詳情可以參考[[https://logging.apache.org/log4j/2.x/manual/appenders.html#CustomDeleteOnRollover|link]]。以範例來說,我希望能做到的是: 確保syslog下的log.txt*檔案不超過10個,接下來說明各屬性意義: | + | |
- | <code properties> | + | |
- | appender.logfile.strategy.action.type = Delete | + | |
- | appender.logfile.strategy.action.basepath = ./syslog/ | + | |
- | appender.logfile.strategy.action.maxDepth = 1 | + | |
- | appender.logfile.strategy.action.IfFileName.type = IfFileName | + | |
- | appender.logfile.strategy.action.IfFileName.glob = log.txt* | + | |
- | appender.logfile.strategy.action.IfFileName.IfAccumulatedFileCount.type = IfAccumulatedFileCount | + | |
- | appender.logfile.strategy.action.IfFileName.IfAccumulatedFileCount.exceeds = 10 | + | |
- | </code> | + | |
- | * **appender.logfile.strategy.action.type**: 設定為Delete,代表要使用Delete Action。 | + | |
- | * **appender.logfile.strategy.action.basepath**: 要執行Delete動作開始掃描的路徑。 | + | |
- | * **appender.logfile.strategy.action.maxDepth**: 要掃描的資料夾深度,預設值為1,在這裡我也是指定為1。 | + | |
- | * **appender.logfile.strategy.action.IfFileName.type**: 要使用Delete Action必須要先宣告設定pathConditions或scriptCondition。以我們的需求來說,是要以檔案名稱當作條件,所以直接設定type為IfFileName。 | + | |
- | * **appender.logfile.strategy.action.IfFileName.glob**: 就著就是設定檔案名稱規則,在這使用了[[https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)|glob]]的pattern設定方式。也可以設定regex去支援regex expression的pattern。 | + | |
- | * **appender.logfile.strategy.action.IfFileName.IfAccumulatedFileCount.type**: 基於IfFileName的條件,多加了IfAccumulatedFileCount去限制檔案數量。 | + | |
- | * **appender.logfile.strategy.action.IfFileName.IfAccumulatedFileCount.exceeds**: 設定檔案數量不得超過10,超過就會執行刪除符合glob樣式的檔案。 | + | |
- | Note. 如果你要快速測試TimeBasedTriggeringPolicy設定檔是否正確,可以把size限制調小,並且filePattern改為以下內容,它會以每分鐘的方式去滾動,而不需要等到天: | + | |
- | <code properties> | + | |
- | appender.logfile.filePattern = ./syslog/log.txt.%d{yyyy-MM-dd-HH-mm}.%i | + | |
- | </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> | + | |
- | 目前實驗起來上面設定等價於,代表最上層是屬於and的關係: | + | |
- | <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> | + | |
- | 就看你個人需求為何了。 | + | |
- | ===== 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~~ | + |