Jazz
Jazz

Reputation: 317

Spring 5 Framework Logging with slf4j/log4j2

I have a application that I've put together to become familiar with Spring Framework 5, using Maven and Java 8. Once executed, it extracts some information from a PostgreSQL database table and logs it to the console (via slf4j/log4j2).

The actual application logic is working fine - I'm seeing the info retrieved from the database on the console as expected. However, I'm not seeing any of the expected Spring/DBCP2 logging. After the application finishes executing, there are only 15 lines printed on the console - all lines that I specifically logged via logger.debug("...").

The dependencies section of my pom.xml looks like this:

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jdbc</artifactId>
        <version>2.3.1</version>
    </dependency>
    
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.9.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.17.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.17.1</version>
    </dependency>
</dependencies>

My log4j2.xml file looks like this...

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Configuration>
<Configuration status="INFO" strict="true">
    <Properties>
        <Property name="CONSOLE_PATTERN">%-5level %c{1} %M - %m%n</Property>
    </Properties>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="${CONSOLE_PATTERN}" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="local.spring5" level="DEBUG" additivity="false">
            <AppenderRef ref="STDOUT" />
        </Logger>
        <Logger name="org.apache.commons.dbcp2" level="DEBUG" additivity="false">
            <AppenderRef ref="STDOUT" />
        </Logger>
        <Logger name="org.springframework" level="INFO" additivity="false">
            <AppenderRef ref="STDOUT" />
        </Logger>
        <Root level="INFO">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>

Note that if I change the org.springframework logging level to DEBUG or TRACE, I see spring logging appropriate for those levels. Setting the log level to INFO, WARN or ERROR results in no logging from spring at all. I'm sceptical, to say the least, that spring doesn't log anything at those levels.

Does anyone have any ideas on how to get the spring framework logging to appear?

Upvotes: 2

Views: 1590

Answers (0)

Related Questions