Home > OS >  How to get data for a particular log4j2 property from an alternative source (application.properties)
How to get data for a particular log4j2 property from an alternative source (application.properties)

Time:06-21

Having a standard configuration for log4j2 and spring property-file on classpath application.property.

log4j2.xml

<Properties>
        ...
        <Property name="APP_LOG_ROOT">${bundle:application:log.root.dir}</Property>
        ...
</Properties>

application.properties

...
log.root.dir=/opt/tomcat/logs
...

The data is read into the log4j2.xml correctly, but what if I want to get an alternative property when creating an artifact with maven and put diferent application.property:

mvn clean install -Dapplication.properties.path=file:/some_path/application.properties

? After that, I can correctly read the new properties.

@Value("${log.root.dir}")
    private String ololo;

but the log4j2 cannot do this on its own.

CodePudding user response:

If you want to use any value from Spring's Environment in a Log4j2 configuration file, you need to use the Spring Boot Lookup.

After adding log4j-spring-boot to your classpath, you just need to replace

${bundle:application:log.root.dir}

with

${spring:log.root.dir}
  • Related