Use mybatis/ibatis log to print sql in SpringBoot

The console prints the sql corresponding to mybatis/ibatis

Just add the following configuration in the configuration file and adjust the log output level you want to see your sql print log in the console.
(For example, the configuration format of .properties, if it is in yaml format, you can modify it yourself)

# 配置sql日志的打印级别(这里设置成自己mapper的所在路径)
logging.level.com.xxx.jay.cust.mapper=info 

Print the sql corresponding to mybatis/ibatis in the log file of the host

After completing the first configuration, it is found that the sql corresponding to the service is still not printed in the log file (app.log or catalina.out) of the host. The reason is that the output mode of the mybatis print log configured in the configuration file uses org.apache.ibatis.logging.stdout.StdOutImpl.
Checking the StdOutImpl class, it is found that the print is the System.out.println() console output print, so the sql corresponds The log is under console.log on the host.

# 配置mybatis输出日志的方式,使用的为默认的StdOutImpl
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

You can customize the output mode of mybatis print log according to StdOutImpl, and configure it as your own output mode class in the configuration file. You can print the SQL corresponding to the service in the log file (app.log or catalina.out) of the host to facilitate the location of online problems.

/**
 * @Author: jay
 * 定义mybatis的sql打印输出方式
 * @Date: 2020/11/30 14:48
 */
public class SqlLogUtils implements Log{
    
    
    private final java.util.logging.Logger log;

    public SqlLogUtils(String clazz) {
    
    
        log = Logger.getLogger(clazz);
    }

    @Override
    public boolean isDebugEnabled() {
    
    
        return true;
    }

    @Override
    public boolean isTraceEnabled() {
    
    
        return false;
    }

    @Override
    public void error(String s, Throwable e) {
    
    
        log.log(Level.SEVERE, s, e);
    }

    @Override
    public void error(String s) {
    
    
        log.log(Level.SEVERE, s);
    }

    @Override
    public void debug(String s) {
    
    
        if(s.charAt(2) == '>' || s.charAt(0) == '<'){
    
    
            log.log(Level.INFO, s);
        }
    }

    @Override
    public void trace(String s) {
    
    
        log.log(Level.FINER, s);
    }

    @Override
    public void warn(String s) {
    
    
        log.log(Level.WARNING, s);
    }
}
# 配置mybatis输出日志的方式,指定使用自己定义的类。
mybatis.configuration.log-impl=com.xxx.jay.utils.SqlLogUtils

Complete the above two steps: 1. Define the type of mybatis output log you want. 2. The configuration file is configured as a newly defined class.
It can be realized that the sql printed when using mybatis/ibatis is output in the host log

Guess you like

Origin blog.csdn.net/weixin_49442658/article/details/110535601