MyBatis Printing SQL logs with log4j

Source: Internet
Author: User
Tags log4j

MyBatis Printing SQL logs with log4j

1.Maven referencing Jar Package

The default MyBatis cannot print out the SQL log, it is not easy to see debugging, need to combine with log4jdbc-log4j2 to be able to input SQL debugging information completely.

pom.xml Configuring maven. Note the following 3 are required

        <dependency>            <groupId>org.bgee.log4jdbc-log4j2</groupId>            <artifactId> log4jdbc-log4j2-jdbc4.1</artifactid>            <version>1.16</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactid>slf4j-api</ artifactid>            <version>1.7.13</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            < Version>1.7.13</version>        </dependency>

2. Configuration information


Log4jdbc.log4j2.prpperties

Log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.slf4jspylogdelegator


Log4j.prpperties

# # # Set Logger output level and output Destination # # Debug more specific, assuming that the printed table data will not be displayed if it is set to info. There's also logfilelog4j.rootlogger=debug,stdout### to output log information to the console # # # log4j.appender.stdout= Org.apache.log4j.ConsoleAppender #log4j. Appender.stdout.target=system.err log4j.appender.stdout.layout= Org.apache.log4j.SimpleLayout # # # to output log information to a file: Jbit.log # # # #log4j. Appender.logfile=org.apache.log4j.fileappender # Log4j.appender.logfile.file=jbit.log #log4j. Appender.logfile.layout=org.apache.log4j.patternlayout # Log4j.appender.logfile.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%F%p%m%n # # #显示SQL语句部分 # Log4j.logger.com.mybatis=debug#log4j.logger.com.mybatis.common.jdbc.simpledatasource=debug # Log4j.logger.com.mybatis.common.jdbc.scriptrunner=debug # Log4j.logger.com.mybatis.sqlmap.engine.impl.sqlmapclientdelegate=debug #log4j. logger.java.sql.connection=debug# log4j.logger.java.sql.statement=debug#log4j.logger.java.sql.preparedstatement=debug# Log4j.logger.java.sql.resultset=debug

Configuration file Placement:

Change the MyBatis configuration file

Mysql

<!--do not output sql<property name= "Driver" value= "Com.mysql.jdbc.Driver"/><property name= "url" value= "JDBC: mysql://"/>                --><!--output sql--><property name=" Driver " Net.sf.log4jdbc.sql.jdbcapi.DriverSpy "/><property name=" url "value=" jdbc:log4jdbc:mysql://... "/>

Sql server

<!--do not output SQL configuration <property name= "Driver" value= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"/>-->< Property name= "url" value= "jdbc:sqlserver://..."/>--><!--output SQL configuration--><property name= "Driver" value= " Net.sf.log4jdbc.sql.jdbcapi.DriverSpy "/><property name=" url "value=" jdbc:log4jdbc:sqlserver://... "/>


Run SQL Output SQL

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Need to pay attention

Assuming that the configuration log4j.rootlogger=info,console does not output strings in the SQL table, it must be log4j.rootlogger=debug,console enough

3. Customizing the Thin Log

There are too many print logs to see, so it is easy to configure the unwanted log packets to Log4j.logger if not required . You can do it later.

Log4j.logger.org.springframework=error


The following is a minimal configuration that only outputs SQL and table data:

Log4j.rootlogger=debug,console  #Console  log4j.appender.console=org.apache.log4j.consoleappender  Log4j.appender.console.target=system.outlog4j.appender.console.layout=org.apache.log4j.patternlayout  log4j.appender.console.layout.conversionpattern=%d [%t]%-5p [%c]-%m%n  log4j.logger.org.apache= errorlog4j.logger.org.mybatis=errorlog4j.logger.org.springframework=error# this need log4j.logger.log4jdbc.debug= errorlog4j.logger.com.gk.mapper=errorlog4j.logger.jdbc.audit=errorlog4j.logger.jdbc.resultset=error# This print SQL statement is very important log4j.logger.jdbc.sqlonly=debuglog4j.logger.jdbc.sqltiming=errorlog4j.logger.jdbc.connection= FATAL

References:

LOG4JDBC-LOG4J2 Configuration Précis-writers

Logback + log4jdbc-log4j2 output JPA SQL parameters actual value

Http://blog.sina.com.cn/s/blog_5bba80460102vagg.html

http://blog.csdn.net/blueheart20/article/details/26471019

http://blog.csdn.net/zht666/article/details/7869624

http://zhenggm.iteye.com/blog/1289455

http://blog.sina.com.cn/s/blog_42971b9e0100p6ua.html NetBeans Configuration Tomcat needs to verify the username password. Tomcat Manager Application

http://blog.csdn.net/ycpanda/article/details/39769737

Http://www.cnblogs.com/xdp-gacl/p/4081848.html


4. Debug the SQL statement directly

Just break in the MyBatis and debug the SQL you need to see.

Simpleexecutor This class below


MyBatis Printing SQL logs with log4j

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.