【问题标题】:Externalize property configuration for log4j.xml外部化 log4j.xml 的属性配置
【发布时间】:2017-08-14 12:17:19
【问题描述】:

背景:我为基于 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>

现在,我希望 log4j 使用“countryName”来编写日志语句。而且,这个 'countryName' 应该通过外部属性文件进行配置。

例如,“gemfire-pattern”将具有此外部化属性名称 $${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>

考虑到这个log4j system properties,就我而言,log4j.component.properties 没有被 log4j 拾取。

关于如何从 log4j.xml 中的外部属性文件中获取属性值有什么想法吗?

参考资料:

提前致谢。

【问题讨论】:

    标签: java xml logging log4j gemfire


    【解决方案1】:

    log4j.component.properties 用于添加 log4j 特定的系统属性,如log4j.configurationFileorg.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"/>
    ...
    确保外部 xml 文件中的内容被 &lt;included&gt; &lt;/included&gt; 标签所包围

    注意-系统属性(-Dcountry="")和环境变量也可以在logback配置中使用${PROPERTY_NAME}来引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2011-02-05
      • 2017-08-18
      • 2015-11-11
      • 2016-12-30
      相关资源
      最近更新 更多