Use of mybatis - mybatis prints sql logs with log4j

Keywords: Attribute log4j SQL JDBC

1.Maven refers to the jar package

The default mybatis can't print out the SQL log, which is not easy to view and debug. It needs to be combined with log4jdbc-log4j2 to complete the input of the debugging information of SQL.

Configure maven with pom.xml. Note that all three of the following are required

  1. <dependency>    
  2.     <groupId>org.bgee.log4jdbc-log4j2</groupId>    
  3.     <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>    
  4.     <version>1.16</version>    
  5. </dependency>    
  6. <dependency>    
  7.     <groupId>org.slf4j</groupId>    
  8.     <artifactId>slf4j-api</artifactId>    
  9.     <version>1.7.13</version>    
  10. </dependency>    
  11. <dependency>    
  12.     <groupId>org.slf4j</groupId>    
  13.     <artifactId>slf4j-log4j12</artifactId>    
  14.     <version>1.7.13</version>    
  15. </dependency>  

2. Configuration information

log4jdbc.log4j2.prpperties
  1. log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator   

log4j.prpperties
  1. ### Set up Logger Output Level and Output Destination ###Deug is more detailed. If set to info, the printed table data will not be displayed when it encounters a string. In addition, there is logfile.
  2. log4j.rootLogger=debug,stdout    
  3.     
  4. ### Export log information to console ###     
  5. log4j.appender.stdout=org.apache.log4j.ConsoleAppender     
  6. #log4j.appender.stdout.Target=System.err     
  7. log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout     
  8.     
  9. ### Export log information to files: jbit.log ###     
  10. #log4j.appender.logfile=org.apache.log4j.FileAppender     
  11. #log4j.appender.logfile.File=jbit.log     
  12. #log4j.appender.logfile.layout=org.apache.log4j.PatternLayout     
  13. #log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n     
  14.     
  15. ###Display the SQL statement section
  16. #log4j.logger.com.mybatis=DEBUG    
  17. #log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG     
  18. #log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG     
  19. #log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG     
  20. #log4j.logger.java.sql.Connection=DEBUG    
  21. #log4j.logger.java.sql.Statement=DEBUG    
  22. #log4j.logger.java.sql.PreparedStatement=DEBUG    
  23. #log4j.logger.java.sql.ResultSet=DEBUG   

Modify myBatis configuration file (MYSQL version)
  1. <!--No output SQL    
  2. <property name="driver" value="com.mysql.jdbc.Driver" />    
  3. <property name="url" value="jdbc:mysql://..." />                    
  4. -->    
  5. <!--output SQL-->    
  6. <property name="driver" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />    
  7. <property name="url" value="jdbc:log4jdbc:mysql://..." />   

SQL server version:
  1. <!--No output sql Configuration    
  2. <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />-->    
  3. <property name="url" value="jdbc:sqlserver://..." />-->    
  4. <!--output sql Configuration-->    
  5. <property name="driver" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />    
  6. <property name="url" value="jdbc:log4jdbc:sqlserver://..." />   


Need attention

If log4j. rootLogger = info is configured, Console will not output strings in the SQL table. It must be log4j. rootLogger = DEBUG before Console can output strings.

If you print too many logs, it's easy to configure unnecessary log packages to log4j.logger. if you don't need them.
  1. log4j.logger.org.springframework=error  

The following is a simplest configuration that outputs only SQL and table data:
  1. log4j.rootLogger=DEBUG,Console    
  2.       
  3. #Console      
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender      
  5. log4j.appender.console.Target=System.out    
  6. log4j.appender.Console.layout=org.apache.log4j.PatternLayout      
  7. log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n      
  8.     
  9. log4j.logger.org.apache=ERROR    
  10. log4j.logger.org.mybatis=ERROR    
  11. log4j.logger.org.springframework=ERROR    
  12. #This need.
  13. log4j.logger.log4jdbc.debug=ERROR    
  14. log4j.logger.com.gk.mapper=ERROR    
  15.     
  16. log4j.logger.jdbc.audit=ERROR    
  17. log4j.logger.jdbc.resultset=ERROR    
  18. #This print SQL statement is very important.
  19. log4j.logger.jdbc.sqlonly=DEBUG    
  20. log4j.logger.jdbc.sqltiming=ERROR    
  21. log4j.logger.jdbc.connection=FATAL  

Posted by dmb on Mon, 11 Feb 2019 11:45:19 -0800