简体   繁体   中英

Externalize property configuration for log4j.xml

Background: I have log4j.xml file configured for our spring based application, which looks like below.

<?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'. And, this 'countryName' should be configured via external property file.

For eg, the "gemfire-pattern" will have this externalised property name $${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.

Any thoughts on how to fetch a property value from external properties file in 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.

To refer to user defined properties, include the property file inside the logback configuration and refer to the keys using ${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. 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

Note- System properties(-Dcountry="") and Environment variables can also be referred using ${PROPERTY_NAME} inside the logback configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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