日志是什么
1、STDOUT_LOGGING
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
他会把里面一些关键的信息输出在我们的控制台
比如jdbc连接,执行的sql,参数,结果,关闭连接
2、log4j
- 1、导入log4j依赖
<!--log4j-->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
- 2、写一个配置文件log4j.properties(通过配置文件灵活配置)
#将等级为DEBUG的日志信息输出到console(控制台)和file(文件)这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/liu.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=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、配置log4j为日志的实现
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
- 4、测试数据库查询日志会输出到一个文件里面和控制台里面
[DEBUG][22-03-20][org.apache.ibatis.logging.LogFactory]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[DEBUG][22-03-20][org.apache.ibatis.logging.LogFactory]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[DEBUG][22-03-20][org.apache.ibatis.io.VFS]Class not found: org.jboss.vfs.VFS
[DEBUG][22-03-20][org.apache.ibatis.io.JBoss6VFS]JBoss 6 VFS API is not available in this environment.
[DEBUG][22-03-20][org.apache.ibatis.io.VFS]Class not found: org.jboss.vfs.VirtualFile
[DEBUG][22-03-20][org.apache.ibatis.io.VFS]VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
[DEBUG][22-03-20][org.apache.ibatis.io.VFS]Using VFS adapter org.apache.ibatis.io.DefaultVFS
[DEBUG][22-03-20][org.apache.ibatis.io.DefaultVFS]Find JAR URL: file:/F:/idea%20project/mybatis-study/mybatis-03/target/classes/com/liu/pojo
[DEBUG][22-03-20][org.apache.ibatis.io.DefaultVFS]Not a JAR: file:/F:/idea%20project/mybatis-study/mybatis-03/target/classes/com/liu/pojo
[DEBUG][22-03-20][org.apache.ibatis.io.DefaultVFS]Reader entry: User.class
[DEBUG][22-03-20][org.apache.ibatis.io.DefaultVFS]Listing file:/F:/idea%20project/mybatis-study/mybatis-03/target/classes/com/liu/pojo
[DEBUG][22-03-20][org.apache.ibatis.io.DefaultVFS]Find JAR URL: file:/F:/idea%20project/mybatis-study/mybatis-03/target/classes/com/liu/pojo/User.class
[DEBUG][22-03-20][org.apache.ibatis.io.DefaultVFS]Not a JAR: file:/F:/idea%20project/mybatis-study/mybatis-03/target/classes/com/liu/pojo/User.class
[DEBUG][22-03-20][org.apache.ibatis.io.DefaultVFS]Reader entry: ���� 4 @
[DEBUG][22-03-20][org.apache.ibatis.io.ResolverUtil]Checking to see if class com.liu.pojo.User matches criteria [is assignable to Object]
[DEBUG][22-03-20][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-03-20][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-03-20][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-03-20][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-03-20][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Opening JDBC Connection
[DEBUG][22-03-20][org.apache.ibatis.datasource.pooled.PooledDataSource]Created connection 1403704789.
[DEBUG][22-03-20][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@53aad5d5]
[DEBUG][22-03-20][com.liu.mapper.UserMapper.selectUserById]==> Preparing: select * from user where id=?
[DEBUG][22-03-20][com.liu.mapper.UserMapper.selectUserById]==> Parameters: 7(Integer)
[DEBUG][22-03-20][com.liu.mapper.UserMapper.selectUserById]<== Total: 1
[DEBUG][22-03-20][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@53aad5d5]
[DEBUG][22-03-20][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@53aad5d5]
[DEBUG][22-03-20][org.apache.ibatis.datasource.pooled.PooledDataSource]Returned connection 1403704789 to pool.
想要自定义输出日志
- 1、在类里面导入log4j的包
import org.apache.log4j.Logger;
- 2、创建logger对象,参数为当前类的calss
Logger logger = Logger.getLogger(UserDaoTest.class);
*3、自定义输出日志(三种级别)
logger.info("info");
logger.debug("debug");
logger.error("error");