2

Say I have log4j2-spring.xml file with all the configuration of the appenders. There is a way to inject to the xml value from application.properties file?

My spring-log4j2.xml look like that:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
    <Syslog name="ElkLogs" host="${logstash.host}" port="5002" protocol="UDP"> 
    </Syslogs>
    <Async name="AsyncElkLogs">
    <AppenderRef ref="ElkLogs"
    </Async> 
</Appenders>
<Loggers>
    <Root>
        <AppenderRef ref="AsyncElkLogs"/>
    </Root>
</Loggers>
</Configuration>

my application-dev.properties file:

logstash.host=elk-dev-1

my application-prod.properties file:

logstash.host=elk-prod-1
Ron Badur
  • 1,873
  • 2
  • 15
  • 34
  • Possible duplicate of [Accessing the application properties in logback.xml](https://stackoverflow.com/questions/36743386/accessing-the-application-properties-in-logback-xml) – Patrick Feb 06 '18 at 09:54
  • 1
    I am looking for a solution to log4j2 framework , the answer you mentioned is for logback framework – Ron Badur Feb 06 '18 at 09:55

2 Answers2

1

use below code

class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 <property name="locations">
<list>
  <value>file:/relase/location1env/env1.properties</value>
  <value>file:/relase/location2env/env2.properties</value>
</list>

spandey
  • 1,034
  • 1
  • 15
  • 30
  • The problem is that I have 2 application.properties files , 1 for each enviroment. – Ron Badur Feb 06 '18 at 09:57
  • Provide location with multiple values in below manner. in any env only one file will be there and that will be picked up.. classpath:env1.properties classpath:env2.properties – spandey Feb 06 '18 at 10:01
1

I found another solution to my problem that decrease the coupling between the enviroments.

I created two configuration files, one for each enviroment with the names spring-log4j2-dev.xml and spring-log4j2-prod.xml

and edit my application.properties files to look like that:

application-dev.properties:

logging.config=classpath:spring-log4j2-dev.xml

application.properties:

logging.config=classpath:spring-log4j2-prod.xml
Ron Badur
  • 1,873
  • 2
  • 15
  • 34