差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

java:log4j2:helloworld [2022/04/09 23:54]
tony [全域定義]
java:log4j2:helloworld [2023/06/25 09:48]
行 1: 行 1:
-{{tag>​log4j2}} 
-====== Log4j2 - Hello World! ====== 
-===== Introduction ===== 
-本篇文章主要針對Log4j2設定的基本結構做介紹,讓大家可以快速進入Log4j2的世界。Log4j2的configuration file提供了XML、JSON、YAML與properties設定的方式,為了利於Log4j1轉換到Log4j2的格式銜接,我選擇了properties格式做介紹。 
-===== Example ===== 
-一個Log4j2的設定檔,幾乎都會有以下三個元素,包含**全域定義**、**Appender定義**與**Logger定義**。接下來將會針對這三個元素做基本介紹。 
-<code properties>​ 
-# part1  全域定義 
-status = warn 
-name= CommonLogSettingForUnitTests 
-rootLogger = DEBUG, CONSOLE 
  
-# part2 Appender定義 
-appender.console.type = Console 
-appender.console.name = CONSOLE 
-appender.console.layout.type = PatternLayout 
-appender.console.layout.pattern = %d{mm:​ss,​SSS}|%5p[%t] (%F:%M:%L) %m %n 
-appender.console.filter.threshold.type = ThresholdFilter 
-appender.console.filter.threshold.level = ALL 
- 
-# part3 Logger定義 
-logger.reflections = DEBUG, CONSOLE 
-logger.reflections.name = org.reflections.Reflections 
-logger.reflections.additivity = false 
- 
-logger.default = DEBUG, CONSOLE 
-logger.default.name = org.tonylin.practice 
-logger.default.additivity = false 
-</​code>​ 
-===== 全域定義 ===== 
-全域定義我把它當成一種預設值的宣告。以本篇範例所提及的項目來說:​ 
-<code properties>​ 
-status = warn 
-name= CommonLogSettingForUnitTests 
-rootLogger = DEBUG, CONSOLE 
-</​code>​ 
-  * **status**: 設定log4j本身的事件的level,會以console形式輸出。如果你懷疑log4j設定有問題,可以改這個設定成trace或debug去看問題出在哪,也可以透過設定system properties "​log4j2.debug=true"​去enable所有log內容。 
-  * **name**: configuration file所對應的configuration名稱。這個目前看起來有將設定分在多個檔案的情況才比較有用,可以參考[[https://​logging.apache.org/​log4j/​2.x/​manual/​configuration.html#​XInclude|XInclude]]與[[https://​logging.apache.org/​log4j/​2.x/​manual/​configuration.html#​CompositeConfiguration|CompositeConfiguration]]。 
-  * **rootLogger**:​ 等同於log4j1的log4j.rootLogger,沒有設定logger的預設行為。以我的範例來說,就是希望針對DEBUG level以上的所有情況,去使用CONSOLE appender定義的方式做log。 
-除了我介紹的三個項目以外,目前我有看到的全域定義還有[[https://​logging.apache.org/​log4j/​2.x/​manual/​configuration.html#​Configuring%20Filters|filter]]、[[https://​logging.apache.org/​log4j/​2.x/​manual/​configuration.html#​Property%20Substitution|properties]],可用於定義預設log過濾規則與變數定義,有興趣的人可以先自行研究,之後文章有用到我再說明。 
- 
- 
-===== Reference ===== 
-  * [[https://​logging.apache.org/​log4j/​2.x/​manual/​configuration.html|官方網站檔案設定介紹]] 
-=====    ===== 
----- 
-\\ 
-~~DISQUS~~