MyBatis Output SQL

Source: Internet
Author: User
Tags log4j

1. Guide Package

Download a log4j-1.2.17.jar, put it under Web-inf Lib, and add the build path

2. Create a configuration file

Create a log4j.properties under SRC and fill in the following:

log4j.rootlogger=debug,console#consolelog4j.appender.console= org.apache.log4j.consoleappenderlog4j.appender.console.layout= org.apache.log4j.patternlayoutlog4j.appender.console.layout.conversionpattern=%d [%t]%-5p [%c]-%m% Nlog4j.logger.org.apache=infolog4j.logger.com.ibatis = ERROR Log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = Error Log4j.logger.com.ibatis.common.jdbc.ScriptRunner = Error Log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = errorlog4j.logger.java.sql.connection= Debuglog4j.logger.java.sql.statement=debuglog4j.logger.java.sql.preparedstatement=debug Log4j.logger.java.sql.resultset=debug

The focus is on the next four sentences, as far as the previous configuration, I have not fully understood.

3. Execution procedures

At this point, you can see that the console prints the corresponding SQL statement each time the database operation is performed

If you need to manually add some log tags after some code snippets, you can also use:

Logger Logger = Logger.getlogger (the current class name.  Class); // using the default configuration information, do not need to write log4j.properties basicconfigurator.configure (); // set the log output level to info, which overrides the level set in the configuration file Logger.setlevel (level.info); // the following message will be output logger.info ("This was an info"), Logger.warn ("This is a warn"); Logger.error (" This was an error "), Logger.fatal (" This is a Fatal ");


If you do not call Basicconfigurator.configure (), propertyconfigurator.configure (), or Domconfigurator.configure () method, log4j will Automatically loads a configuration file named Log4j.properties under Classpath . (So the above code does not call this)

If you change this profile to a different name, such as My.properties, the program still works, but it will report a hint that the log4j system could not be initialized correctly. This can be added in the program:

Propertyconfigurator.configure ("Classes/my.properties");

MyBatis Output SQL

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.