2
votes

In a Spring Boot Web app, I have the following logging properties in application.properties.

logging.level.guru.springframework.controllers=DEBUG
logging.level.org.springframework.web=DEBUG
logging.file=logs/spring-boot-logging.log

Things work fine - DEBUG messages of both internal Spring Boot and the application gets logged to logs/spring-boot-logging.log. But, even if I add log4j2-spring.xml with different logging levels, the levels of application.properties still get picked.

My log4j2-spring.xml is this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
  <Properties>
    <Property name="log-path">applogs</Property>
</Properties>
<Appenders>
    <Console name="Console-Appender" target="SYSTEM_OUT">
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>>
        </PatternLayout>
    </Console>
    <File name="File-Appender" fileName="${log-path}/app_log.log" >
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
        </PatternLayout>
    </File>
</Appenders>
<Loggers>
    <Logger name="org.springframework.web" level="info">
        <AppenderRef ref="File-Appender"/>
    </Logger>
    <Logger name="guru.springframework.controllers" level="info">
        <AppenderRef ref="File-Appender"/>
    </Logger>
    <Root level="info">
        <AppenderRef ref="Console-Appender"/>
    </Root>
  </Loggers>
</Configuration>

With these settings, debug (Defined in application.properties) messages are sent to applogs/app_log.log(Defined in log4j2-spring.xml)

On commenting out the logging.level.* in application properties, the log levels (info) in log4j2-spring.xml are used. I assume that by default properties of application.properties have precedence over log4j2-spring.xml. Is there any way I can configure Spring Boot to use log4j2-spring.xml properties instead of application.properties. Also, If application.properties have greater precedence, why the file appender of log4j2-spring.xml is getting used instead of the one in logging.file?

1
Move the log4j2-spring.xml file to the root of your project folder. Add this to your application.properties file logging.config=log4j2-spring.xml. Spring boot will directly pick it up from the root of the project directory.Krishna

1 Answers

1
votes

It should use your config file if you define it via this Spring Boot property:

logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback