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.

ko4evneg
  • 119
  • 6
  • Does this answer your question? [Seeing the underlying SQL in the Spring JdbcTemplate?](https://stackoverflow.com/questions/1932208/seeing-the-underlying-sql-in-the-spring-jdbctemplate) – Tristan Nov 02 '21 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? – ko4evneg Nov 02 '21 at 14:43

0 Answers0