Mybatis的學習3______使用log4j打印日誌信息

(本案例在maven項目中作的測試:)java

1.導包:在pom.xml中mysql

        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

2.在src/resources下進行的 log4j.properties文件的編寫:web

### Log4j配置 ###
### 與Spring結合須要在web.xml中指定此文件位置,並添加監聽器 ###
#定義log4j的輸出級別和輸出目的地(目的地能夠自定義名稱,和後面的對應)
#[ level ] , appenderName1 , appenderName2
log4j.rootLogger=DEBUG,console,file

#-----------------------------------#
#1 定義日誌輸出目的地爲控制檯
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
####能夠靈活地指定日誌輸出格式,下面一行是指定具體的格式 ###
#%c: 輸出日誌信息所屬的類目,一般就是所在類的全名
#%m: 輸出代碼中指定的消息,產生的日誌具體信息
#%n: 輸出一個回車換行符,Windows平臺爲"/r/n",Unix平臺爲"/n"輸出日誌信息換行
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#-----------------------------------#
#2 文件大小到達指定尺寸的時候產生一個新的文件
log4j.appender.file = org.apache.log4j.RollingFileAppender
#日誌文件輸出目錄
log4j.appender.file.File=log/tibet.log
#定義文件最大大小
log4j.appender.file.MaxFileSize=10mb
###輸出日誌信息###
#最低級別
log4j.appender.file.Threshold=ERROR
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

#-----------------------------------#
#3 druid
log4j.logger.druid.sql=INFO
log4j.logger.druid.sql.DataSource=info
log4j.logger.druid.sql.Connection=info
log4j.logger.druid.sql.Statement=info
log4j.logger.druid.sql.ResultSet=info

#4 mybatis 顯示SQL語句部分
log4j.logger.org.mybatis=DEBUG
#log4j.logger.cn.tibet.cas.dao=DEBUG
#log4j.logger.org.mybatis.common.jdbc.SimpleDataSource=DEBUG
#log4j.logger.org.mybatis.common.jdbc.ScriptRunner=DEBUG
#log4j.logger.org.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
#log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

3.mybatis核心文件中進行配置:sql

  <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
 
(注意configuration 的參數順序的編寫)

一個測試,日誌的輸出以下:apache

[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[org.apache.ibatis.io.VFS]-Class not found: org.jboss.vfs.VFS
[org.apache.ibatis.io.JBoss6VFS]-JBoss 6 VFS API is not available in this environment.
[org.apache.ibatis.io.VFS]-Class not found: org.jboss.vfs.VirtualFile
[org.apache.ibatis.io.VFS]-VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
[org.apache.ibatis.io.VFS]-Using VFS adapter org.apache.ibatis.io.DefaultVFS
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/E:/IdeaProjects/Mybatis_7_24_zhujie/target/classes/com/xbf/pojo
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/E:/IdeaProjects/Mybatis_7_24_zhujie/target/classes/com/xbf/pojo
[org.apache.ibatis.io.DefaultVFS]-Reader entry: User.class
[org.apache.ibatis.io.DefaultVFS]-Listing file:/E:/IdeaProjects/Mybatis_7_24_zhujie/target/classes/com/xbf/pojo
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/E:/IdeaProjects/Mybatis_7_24_zhujie/target/classes/com/xbf/pojo/User.class
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/E:/IdeaProjects/Mybatis_7_24_zhujie/target/classes/com/xbf/pojo/User.class
[org.apache.ibatis.io.DefaultVFS]-Reader entry: ����   1 <
[org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.xbf.pojo.User matches criteria [is assignable to Object]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 996796369.
[com.xbf.dao.UserMapper.getAllUser]-==>  Preparing: select * from user 
[com.xbf.dao.UserMapper.getAllUser]-==> Parameters: 
[com.xbf.dao.UserMapper.getAllUser]-<==      Total: 5
[User{id=1, name='狂神', pwd='123456'}, User{id=2, name='老七', pwd='250'}, User{id=3, name='李四', pwd='987654'}, User{id=4, name='張三', pwd='250'}, User{id=5, name='老五', pwd='666'}]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3b69e7d1]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 996796369 to pool.

Process finished with exit code 0
相關文章
相關標籤/搜索