Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to configure logback for Mybatis to print my SQL

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss} %-5level %logger{35} - %msg%n
            </Pattern>
        </encoder>
    </appender>
    <logger name="java.sql" level="DEBUG" >
        <appender-ref ref="STDOUT" />
    </logger>
    <root>
        <level value="ERROR" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

I want print sql and error only,but it does not print SQL in Console,can any one help me? thanks lot

like image 936
maows Avatar asked Feb 12 '14 03:02

maows


People also ask

How do I log into Mybatis queries?

To log SQL queries from a specific mapper in mybatis, add the mapper namespace to the logger level. Originally published at cheeky-chinnu.blogspot.com on January 8, 2019.

What is Logback configuration?

The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations.

Where do I put Logback XML?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

How do I edit Logback XML?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.


2 Answers

You configuration of appender is correct but logger configuration is not.

To log SQL statements for particular mybatis mapper set DEBUG (TRACE to see query parameters and results) level for logger with fully qualified mapper name

<logger name="com.mycompany.myapp.mapper.MyMapper" level="DEBUG"/>

You can log all SQL statements from all mappers if they are in the same package like this

<logger name="com.mycompany.myapp.mapper" level="DEBUG"/>
like image 170
Roman Konoval Avatar answered Sep 28 '22 04:09

Roman Konoval


     <logger name="org.mybatis">
      <level value="TRACE"/>
  </logger>

<logger name="java.sql">
    <level value="WARN"/>
</logger> 

I used the above code snippet in my logback.xml and got the sql query in logger. If require to print sql in specific logger, use tag.

like image 32
Paramesh Korrakuti Avatar answered Sep 28 '22 03:09

Paramesh Korrakuti