如何使用Spring Boot 1.3.6.RELEASE使log4j2在环境中可配置 [英] How to make log4j2 configurable by environment using spring boot 1.3.6.RELEASE

查看:77
本文介绍了如何使用Spring Boot 1.3.6.RELEASE使log4j2在环境中可配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的application.properties更改log4j2.xml文件中的某些属性,例如,定义一些属性,然后在log4j2中替换作为参数的那些属性.

I would like to change some properties from the log4j2.xml file depending on the my application.properties, so for example define some properties and then substitute in the log4j2 those properties that are parameters.

我采用了不同的方法,但是我仍然没有得到正确的结果.我想根据环境(DEV,QA或PROD)进行不同的配置.有人可以指导我如何做到这一点吗?

I ran different approaches but I still not get the right thing. I would like to have different configs depending on the environment (DEV, QA or PROD). Can someone guide me how to accomplish this?

因此,我正在尝试将其添加到我的属性中

So, I'm trying to have this in my properties

#Place holders for log4j2.xml file
log.file.path=/opt/tomcat/logs
log.file.name=dummydummy
log.file.size=100 MB
log.level=DEBUG

请在下面找到我的log4j2示例...

Please find my log4j2 sample below...

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="PID">????</Property>
        <property name="name">my-log</property>
    </Properties>
    <Appenders>
        <RollingFile name="file" fileName="${log.file.path}${log.file}.log"
            filePattern="${log.file.path}${log.file}-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout
                pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %c{1}(%M:%L) : %m%n%wEx" />
            <Policies>
                <TimeBasedTriggeringPolicy /><!-- Rotated everyday -->
                <SizeBasedTriggeringPolicy size="${log.file.size}" /> <!-- Or every 100 MB -->
            </Policies>
        </RollingFile>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout
                pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%t]}{faint} %clr{%c{1}(%M:%L)}{cyan} %clr{:}{faint} %m%n%wEx" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.hibernate.validator.internal.util.Version"
            level="warn" />
        <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
        <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
        <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" />
        <Logger name="org.springframework.web" level="error" />

        <Root level="${log.level}">
            <AppenderRef ref="Console" />
            <AppenderRef ref="file" />
        </Root>
    </Loggers>
</Configuration>

谢谢

推荐答案

properties查找元素允许从log4j配置中的外部属性文件引用属性. 对于您的示例,应该是这样的:

The properties lookup element allows to refer properties from an external properties file in the log4j configuration. For your example it should be something like this:

  1. 文件 env.properties 包含以下属性:

log.file.path=/opt/tomcat/logs
log.file.name=dummydummy
log.file.size=100 MB
log.level=DEBUG

属性查找应定义为 log4j2.xml 的属性:

The properties lookup should be defined as properties of the log4j2.xml:

<Configuration>  
  <Properties>  
      <property name="log.file.path">${bundle:env:log.file.path}</property>  
      <property name="log.file.name">${bundle:env:log.file.name}</property>  
      <property name="log.file.size">${bundle:env:log.file.size}</property>  
      <property name="log.level">${bundle:env:log.level}</property>   
  </Properties>  

现在,属性可以在附加器中以$ {property_name}表示法引用.每个属性引用都将使用 env.properties 中的实际值进行插值.

Now the properties may be referred in appenders with ${property_name} notation. Each property reference will be interpolated with the real value from the env.properties.

您可以在此处.

这篇关于如何使用Spring Boot 1.3.6.RELEASE使log4j2在环境中可配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆