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