简体   繁体   English

外部化log4j.xml的属性配置

[英]Externalize property configuration for log4j.xml

Background: I have log4j.xml file configured for our spring based application, which looks like below. 背景:我为基于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} &lt;%thread&gt; 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>

Now, I want log4j to write log statements with let's say 'countryName'. 现在,我希望log4j用“ countryName”写日志语句。 And, this 'countryName' should be configured via external property file. 并且,此“ countryName”应通过外部属性文件进行配置。

For eg, the "gemfire-pattern" will have this externalised property name $${countryName}. 例如,“ gemfire模式”将具有此外部化的属性名称$$ {countryName}。

<Property name="gemfire-pattern">[$${countryName} %level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} &lt;%thread&gt; tid=%tid %C{1.}] %message%n%throwable%n<    /Property>

Considering this log4j system properties , in my case, the log4j.component.properties is not being picked up by log4j. 考虑到此log4j系统属性 ,在我的情况下,log4j不会选择log4j.component.properties

Any thoughts on how to fetch a property value from external properties file in log4j.xml? 关于如何从log4j.xml的外部属性文件中获取属性值的任何想法?

References: 参考文献:

Thanks in advance. 提前致谢。

log4j.component.properties is used to add log4j specific system properties like log4j.configurationFile , org.apache.logging.log4j.level etc. log4j.component.properties用于添加特定于log4j的系统属性,例如log4j.configurationFileorg.apache.logging.log4j.level等。

To refer to user defined properties, include the property file inside the logback configuration and refer to the keys using ${KEY} 要引用用户定义的属性,请将属性文件包含在logback配置内,并使用${KEY}引用键

 <configuration> <property file="country.properties" /> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>${countryName}</file> ... 

Logback also allows you to externalise parts of the configuration using File inclusion feature. Logback还允许您使用文件包含功能来外部化配置的某些部分。 https://logback.qos.ch/manual/configuration.html#fileInclusion https://logback.qos.ch/manual/configuration.html#fileInclusion

 <configuration> <include file="src/main/java/chapters/configuration/includedConfig.xml"/> ... 
Make sure the content in the external xml file is encolsed with <included> </included> tag 确保外部xml文件中的内容带有<included> </included>标签

Note- System properties(-Dcountry="") and Environment variables can also be referred using ${PROPERTY_NAME} inside the logback configuration. 注意-系统属性(-Dcountry =“”)和环境变量也可以在回送配置中使用${PROPERTY_NAME}进行引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM