MyBatis笔记(五)——log4j导入,让运行的程序将SQL日志输出出来

 前面介绍了这么多操作,但是发现没层次运行mybaitis的数据库操作的时候没有SQL日志输出到控制台,这不利于我们在以后的实际开发中调试程序。

今天介绍一种方法,利用log4j来输出SQL日志。首先在Maven项目下加入log4j的依赖。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.happyBKs.mybatis</groupId>
  <artifactId>C1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>C1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>

	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.3.0</version>
	</dependency>


	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.35</version>
	</dependency>

	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>
	
  </dependencies>
</project>

然后在src/main/resources下新建log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
         <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
                   <layout class="org.apache.log4j.PatternLayout">
                            <param name="ConversionPattern"
                                     value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" />
                   </layout>
         </appender>
         <logger name="java.sql">
                   <level value="debug" />
         </logger>
         <logger name="org.apache.ibatis">
                   <level value="debug" />
         </logger>
         <root>
                   <level value="debug" />
                   <appender-ref ref="STDOUT" />
         </root>
</log4j:configuration>

或者,不用xml,也可以用properties文件。

         log4j.properties,
         log4j.rootLogger=DEBUG, Console
         #Console
         log4j.appender.Console=org.apache.log4j.ConsoleAppender
         log4j.appender.Console.layout=org.apache.log4j.PatternLayout
         log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
         log4j.logger.java.sql.ResultSet=INFO
         log4j.logger.org.apache=INFO
         log4j.logger.java.sql.Connection=DEBUG
         log4j.logger.java.sql.Statement=DEBUG
         log4j.logger.java.sql.PreparedStatement=DEBUG



输出结果已经开始有大量的日志了。

DEBUG 07-04 17:10:43,677 Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.  (LogFactory.java:135) 
DEBUG 07-04 17:10:43,755 Class not found: org.jboss.vfs.VFS  (VFS.java:109) 
DEBUG 07-04 17:10:43,755 JBoss 6 VFS API is not available in this environment.  (JBoss6VFS.java:142) 
DEBUG 07-04 17:10:43,756 Class not found: org.jboss.vfs.VirtualFile  (VFS.java:109) 
DEBUG 07-04 17:10:43,757 VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.  (VFS.java:70) 
DEBUG 07-04 17:10:43,757 Using VFS adapter org.apache.ibatis.io.DefaultVFS  (VFS.java:84) 
DEBUG 07-04 17:10:43,758 Find JAR URL: file:/E:/Users/sunsun/Documents/workspace/STS/mybatis/C1/target/test-classes/com/happyBKs/mybatis/C1  (DefaultVFS.java:222) 
DEBUG 07-04 17:10:43,758 Not a JAR: file:/E:/Users/sunsun/Documents/workspace/STS/mybatis/C1/target/test-classes/com/happyBKs/mybatis/C1  (DefaultVFS.java:248) 
DEBUG 07-04 17:10:43,905 Reader entry: AppTest.class  (DefaultVFS.java:102) 
DEBUG 07-04 17:10:43,906 Reader entry: TestCrud.class  (DefaultVFS.java:102) 
DEBUG 07-04 17:10:43,907 Listing file:/E:/Users/sunsun/Documents/workspace/STS/mybatis/C1/target/test-classes/com/happyBKs/mybatis/C1  (DefaultVFS.java:113) 
DEBUG 07-04 17:10:43,907 Find JAR URL: file:/E:/Users/sunsun/Documents/workspace/STS/mybatis/C1/target/test-classes/com/happyBKs/mybatis/C1/AppTest.class  (DefaultVFS.java:222) 
DEBUG 07-04 17:10:43,908 Not a JAR: file:/E:/Users/sunsun/Documents/workspace/STS/mybatis/C1/target/test-classes/com/happyBKs/mybatis/C1/AppTest.class  (DefaultVFS.java:248) 
DEBUG 07-04 17:10:43,909 Reader entry: ����



你可能感兴趣的:(MyBatis笔记(五)——log4j导入,让运行的程序将SQL日志输出出来)