[英]Externalize property configuration for log4j.xml
背景:我為基於Spring的應用程序配置了log4j.xml文件,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="FATAL" shutdownHook="disable" packages="com.gemstone.gemfire.internal.logging.log4j">
<Properties>
<Property name="gemfire-pattern">[%level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid %C{1.}] %message%n%throwable%n< /Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="${gemfire-pattern}"/>
</Console>
<RollingFile name="eventLogFile" fileName="/opt/data/service/logs/events.log"
filePattern="/opt/data/service/logs/events-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS} %p - %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="20" fileIndex="max"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.gemstone" level="INFO" additivity="true">
<filters>
<MarkerFilter marker="GEMFIRE_VERBOSE" onMatch="DENY" onMismatch="NEUTRAL"/>
</filters>
</Logger>
<Logger name="com.app.mypackage" level="INFO" additivity="true">
<AppenderRef ref="eventLogFile"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
現在,我希望log4j用“ countryName”寫日志語句。 並且,此“ countryName”應通過外部屬性文件進行配置。
例如,“ gemfire模式”將具有此外部化的屬性名稱$$ {countryName}。
<Property name="gemfire-pattern">[$${countryName} %level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid %C{1.}] %message%n%throwable%n< /Property>
考慮到此log4j系統屬性 ,在我的情況下,log4j不會選擇log4j.component.properties
。
關於如何從log4j.xml的外部屬性文件中獲取屬性值的任何想法?
參考文獻:
提前致謝。
log4j.component.properties
用於添加特定於log4j的系統屬性,例如log4j.configurationFile
, org.apache.logging.log4j.level
等。
要引用用戶定義的屬性,請將屬性文件包含在logback配置內,並使用${KEY}
引用鍵
<configuration> <property file="country.properties" /> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>${countryName}</file> ...
Logback還允許您使用文件包含功能來外部化配置的某些部分。 https://logback.qos.ch/manual/configuration.html#fileInclusion
<configuration> <include file="src/main/java/chapters/configuration/includedConfig.xml"/> ...
<included> </included>
標簽
注意-系統屬性(-Dcountry =“”)和環境變量也可以在回送配置中使用${PROPERTY_NAME}
進行引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.