[英]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.