Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

properties - How to make Log4j2 configurable by environment using Spring Boot 1.3.6.RELEASE

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

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

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

My log4j2 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. A file env.properties contains the following properties:

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

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>  

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.

You can find another example of properties lookup here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...