差異處
這裏顯示兩個版本的差異處。
Both sides previous revision 前次修改 下次修改 | 前次修改 | ||
java:log4j2:indicateconfigurationfile [2022/04/10 11:34] tony [指定設定檔] |
java:log4j2:indicateconfigurationfile [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 58: | 行 58: | ||
appender.logfile.filePattern = ${basePath}/${appLog}.%i | appender.logfile.filePattern = ${basePath}/${appLog}.%i | ||
</code> | </code> | ||
- | 這裡做了一些重構,將共用的const抽為property宣告。另外與log4j1不同的是,log4j2支援許多prefix,去避免properties名稱的衝突問題。以我們的例子來說,我們使用的是system properties的path.base,所以變數名稱就要叫${sys:path.base};假如你要引入系統環境變數,就會是${env:path.base},更多可以參考[[https://logging.apache.org/log4j/2.x/manual/configuration.html#Property%20Substitution|link]]。對了,它支援default value: ${sys:some.property:-default_value}。 | + | 這裡做了一些重構,將共用的const抽為property宣告。另外與log4j1不同的是,log4j2支援許多prefix,去避免properties名稱的衝突問題。以我們的例子來說,我們使用的是system properties的path.base,所以變數名稱就要叫${sys:path.base};假如你要引入系統環境變數,就會是${env:path.base},更多可以參考[[https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution|link]]。對了,它支援default value: ${sys:some.property:-default_value}。 |
==== 設定檔搜尋規則 ==== | ==== 設定檔搜尋規則 ==== | ||
- | 根據[[https://logging.apache.org/log4j/2.x/manual/configuration.html#Automatic%20Configuration|link]]描述,設定檔的搜尋規則與順序如下: | + | 根據[[https://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration|link]]描述,設定檔的搜尋規則與順序如下: |
- 找log4j2.configurationFile所設定的檔案位置。 | - 找log4j2.configurationFile所設定的檔案位置。 | ||
+ | - 找檔案名稱為log4j2-test.properties的設定檔。根據不同格式的支援,順序為YAML>JSON>XML>properties。 | ||
+ | - 找檔案名稱為log4j2.properties的設定檔。根據不同格式的支援,順序為YAML>JSON>XML>properties。 | ||
+ | - 如果都找不到就是看log4j的預設值了。目前是ERROR Level+Console。 | ||
+ | 這也是為什麼如果test code和production code都有設定檔時會先找到test code部分的原因。 | ||
===== Reference ===== | ===== Reference ===== | ||
- | * [[https://logging.apache.org/log4j/2.x/manual/configuration.html#Automatic%20Configuratio|官方網站檔案設定介紹]] | + | * [[https://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguratio|官方網站檔案設定介紹]] |
* [[https://stackoverflow.com/questions/7126709/how-do-i-set-log4j-level-on-the-command-line|How do I set log4j level on the command line?]] | * [[https://stackoverflow.com/questions/7126709/how-do-i-set-log4j-level-on-the-command-line|How do I set log4j level on the command line?]] | ||
===== ===== | ===== ===== |