0

I am learning Spring now and currently trying to configure slf4j with logback to log MySQL statements in test environment. As far as I understand I configured everything to get MySQL logs, but I do not see anything. Logging works for other loggers, except for Connector/J driver. What should I fix in my code? My configs are below:

pom.xml contains these dependencies:

    <!-- Logging with SLF4J & LogBack -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
        <scope>runtime</scope>
    </dependency>

logback-test.xml

<configuration>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
    <resetJUL>true</resetJUL>
</contextListener>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <charset>UTF-8</charset>
        <pattern>%d{HH:mm:ss.SSS} %-5level %class{50}.%M:%L - %msg%n</pattern>
    </encoder>
</appender>

<logger name="com.mysql" level="debug"/>
<logger name="org.springframework.jdbc" level="info"/>
<logger name="ru.javawebinar.topjava" level="debug"/>

<root level="INFO">
    <appender-ref ref="console"/>
</root>

I am using Spring's JDBC template to access DB like that:

public List<Meal> getAll(int userId) {
    return jdbcTemplate.query(
            "SELECT * FROM meals WHERE user_id=? ORDER BY date_time DESC", ROW_MAPPER, userId);
}

and my connection string url is

jdbc:mysql://localhost:3306/topjava?logger=Slf4JLogger

Sorry for the dumb question, but I really stuck here.

3
  • Does this answer your question? Seeing the underlying SQL in the Spring JdbcTemplate? Commented Nov 2, 2021 at 13:19
  • Partially. I managed to configure "org.springframework.jdbc" logger and I see queries in log, but I want to know if I can retrieve queries from JDBC driver directly in my scenario, not using Spring? Commented Nov 2, 2021 at 14:43
  • The package name for MySQL JDBC logger is com.mysql.jdbc instead of com.mysql. Have you tried updating your config to <logger name="com.mysql.jdbc" level="DEBUG"/> ? Commented Jan 21, 2024 at 14:47

1 Answer 1

0
+50

To enable logging with Connector/J and SLF4J, use a connection URL like the following:

jdbc:mysql://server/database?logger=com.mysql.cj.log.Slf4JLogger&profileSQL=true

Note that older versions of the driver were using another package (as pointed out in this comment).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.