Use Log4jdbc-log4j2 to print the sql statement of mybatis

Introduction: MyBatis is used in the project. One of the more troublesome problems is that I cannot see the specific situation of the executed SQL. Therefore, I found Log4jdbc-log4j2. This is a monitoring tool based on the jdbc level, which can monitor the main operations on the database , so as to perfectly view the operations performed therein.

 

1. Log4jdbc-log4j2 version selection

2. How to import into the project?

     If the project is based on Maven tools, you can directly use the following dependencies to solve the problem perfectly:

    <!-- log4jdbc -->

<dependency>
	<groupId>org.bgee.log4jdbc-log4j2</groupId>
	<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
	<version>1.16</version>
</dependency>
  2. Related configuration files

 

log4jdbc.log4j2.properties or application-dev.properties _

log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

jdbc driver and url modified to

db.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
db.url=jdbc:log4jdbc:mysql://localhost:3306/wechat_talk?useUnicode=true&characterEncoding=utf-8

Log4j related configuration xml or properties

 logback .xml (part), because log4jdbc is old and does not support the configuration of log4j2, if it is used, it will be output in full

 

<!-- sql and execution time spent -->
<logger name="jdbc.sqltiming" level="INFO" additivity="false"></logger>
<!-- Return result after execution-->
<logger name="jdbc.resultsettable" level="INFO"></logger>
<!-- only sql -->
<logger name="jdbc.sqlonly" level="OFF"></logger>
<logger name="jdbc.audit" level="OFF"></logger>
<logger name="jdbc.resultset" level="OFF"></logger>
<!-- You can see the number of connections to the database -->
<logger name="jdbc.connection" level="OFF"></logger>
 

 

   If you use log4j2, you need to download the latest version 1.2 jar from https://code.google.com/archive/p/log4jdbc/downloads on the official website. There is no jar in the maven library.

  

   The above configuration modifies maven dependencies, adds jar packages or builds private server nexus by yourself

  replace

spring.datasource.driverClassName=net.sf.log4jdbc.DriverSpy

 

   log4j2.xml

  

<!-- sql and execution time spent -->
<logger name="jdbc.sqltiming" level="INFO" additivity="false"></logger>
<!-- Return result after execution-->
<logger name="jdbc.resultsettable" level="INFO"></logger>
<!-- only sql -->
<logger name="jdbc.sqlonly" level="OFF"></logger>
<logger name="jdbc.audit" level="OFF"></logger>
<logger name="jdbc.resultset" level="OFF"></logger>
<!-- You can see the number of connections to the database -->
<logger name="jdbc.connection" level="OFF"></logger>

 

  

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326354550&siteId=291194637