Search code examples
spring-bootlog4j2application.properties

External log4j2.xml file path in application.properties


I am using log4j2 in my project and I have externalized the application.properties and log4j2.xml and I want to provide log4j2.xml file path in application.properties

log4j2.xml and application.properties are in the config folder in the same dir as a jar.

Here is my dependancy

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Directory Structure : /tmp/myproj/conf/application.properties

/tmp/myproj/conf/log4j2.xml

/tmp/myproj/my.jar

Any help would be appreciated.


Solution

  • Spring Boot expects the log4j2-spring.xml configuration file to be on the classpath. However, you can store it in a different location and point to it using the logging.config property in application.properties.

    Try providing below configuration to you application.properties. Here log4j2-spring.xml is on project classpath. If not then try giving your full path as (suppose) "C:/tmp/myproj/conf/log4j2.xml".

    **logging.config=classpath:log4j2-spring.xml**
    logging.level.org.springframework.web=INFO
    logging.file=logs/spring-boot-logging.log
    

    The configuaration in bold should work for you.

    Then you need to configure your log4j2-spring.xml as per your requirement. (simple and also for appenders, see here https://howtodoinjava.com/log4j2/log4j-2-xml-configuration-example/ ).

    Note: I have used log4j2-spring.xml in place of you log4j2.xml. you can replace at your will.