Get the App
SLTechnology News&Howtos  ›  Development  › 

How to display the mybatis query log file in logback and write

Shulou Source: shulou.com Published: 2022-06-01 04:03:25 10月04日 Update

In this article Xiaobian for you to introduce in detail "how to display mybatis query log file in logback and write", the content is detailed, the steps are clear, the details are handled properly, I hope this article "how to display mybatis query log file in logback and write" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Display mybatis query log in logback 1. Configuration file

There are many different implementations. Here is how to output the log to the console.

Mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl II. Log of the customized package levellogging: level: # your repository package com.kingboy.repository: debug 3. Through the logback-spring.xml file

Add the following configuration to the file

/ / add this section and change it to your own package path to record the operation database sql in the log file

Springboot+mybatis

Mybatis: # Mark the xml file location of the mapper to be parsed mapper-locations: classpath:mapper/*.xml configuration: # org.apache.ibatis.logging.slf4j.Slf4jImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

The specific implementation is to rewrite the StdOutImpl class

The implementation is as follows:

Package com.emily.infrastructure.datasource.log;import com.emily.infrastructure.logback.factory.LogbackFactory;import org.apache.ibatis.logging.Log / * * @ Description: record the mybatis sql statement in the log file to implement the class, which is a replacement for the org.apache.ibatis.logging.stdout.StdOutImpl class * @ Author: Emily* @ create: 2021/8/22*/public class LogBackImpl implements Log {public LogBackImpl (String clazz) {/ / Do Nothing} @ Override public boolean isDebugEnabled () {return true;} @ Override public boolean isTraceEnabled () {return true } @ Override public void error (String s, Throwable e) {LogbackFactory.module ("database", "database", s); e.printStackTrace (System.err);} @ Override public void error (String s) {LogbackFactory.module ("database", "database", s);} @ Override public void debug (String s) {LogbackFactory.module ("database", "database", s) } @ Override public void trace (String s) {LogbackFactory.module ("database", "database", s);} @ Override public void warn (String s) {LogbackFactory.module ("database", "database", s);}}

For the rewritten implementation class to take effect, replace the configuration with the implementation class, as follows:

Mybatis: # Mark the xml file location of mapper to be parsed mapper-locations: classpath:mapper/*.xml configuration: # org.apache.ibatis.logging.slf4j.Slf4jImpl log-impl: com.emily.infrastructure.datasource.log.LogBackImpl. This article "how to display and write mybatis query log files in logback" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Files logs queries content articles configurations locations methods different appropriate multiple ideas console data databases new knowledge more steps knowledge knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux MySQL OPPO Reno NVidia