Including org.springframework logs in log4j

I’m using log4j to manage logging in my application using the following log4j file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="C:/Temp/debug.log" />
        <param name="Append" value="true" />
        <param name="MaxFileSize" value="10MB" />
        <param name="MaxBackupIndex" value="10" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%t] %C.%M() - %m%n" />
        </layout>
    </appender>

    <logger name="org.springframework" additivity="false">
        <level value="debug"/>
        <appender-ref ref="DEBUG"/>
    </logger>

    <root>
        <appender-ref ref="DEBUG"/>
    </root>

</log4j:configuration>

It logs all of the Logger instances in the code perfectly i.e:

private static final Logger log = Logger.getLogger(Construct.class);

However there are no instances of the springframework logs. I know the springframework is producing logs as I can see them in the IDE console. Am I missing a trick here?

Answer

Your config file is getting ignored, because Spring Boot uses Logback by default.

You have to explicitly exclude the spring-boot-starter-logging dependency in your pom (it is brought in as a transitive dependency by several of the starters), and add in a spring-boot-starter-log4j dependency.

Also ensure that the exclusion happens before any other transitive dependency on spring-boot-starter-logging is declared, e.g. via spring-boot-starter-test – this has tripped me up before.

Leave a Reply

Your email address will not be published. Required fields are marked *