6、日志.

6.1 日志工厂

如果一个数据库操作,出现了异常,我们需要排错,日志就是最好的助手!

  • 曾经:sout、debug

  • 现在:日志工厂

在这里插入图片描述

  • SLF4J
  • LOG4J 【掌握】
  • LOG4J2
  • JDK_LOGGING
  • COMMONS_LOGGING
  • STDOUT_LOGGING 【掌握】
  • NO_LOGGING

在MyBatis中具体使用哪一个日志实现,在设置中设定

STDOUT_LOGGING标准日志输出

在mybatis核心配置文件中,配置我们的日志!

手动配置日志

顺序不能出错
在这里插入图片描述

name和value不能有错误

<!--日志-->
<settings>
  <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

输出结果

Opening JDBC Connection  // 打开jdbc连接
Created connection 422330142. // 创建connection连接对象,本质还是jdbc
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@192c3f1e] // 设置自动提交的事务为 false
==>  Preparing: select * from user where id = ? // 预编译的sql
==> Parameters: 1(Integer) // sql传递的参数
<==    Columns: id, name, pwd // 查询的列
<==        Row: 1, 张三, 123 // 查询的记录
<==      Total: 1 // 总共多少记录
User{id=1, name='张三', password='123'} // 结果
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@192c3f1e] // 设置自动提交事务为 true
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@192c3f1e] // 关闭JDBC连接
Returned connection 422330142 to pool. // 把connection返回池子

错误

ClassNotFoundException

  • 包名/类名,写错了

  • 没有导包

6.2 Log4j

什么是Log4j?

Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件;

我们也可以控制每一条日志的输出格式;

通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程;

通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。

配置

  1. 先导入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/rzp.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.sq1.PreparedStatement=DEBUG
    
  3. 配置settings为log4j实现

    <settings>
      <setting name="logImpl" value="LOG4J"/>
    </settings>
    
  4. 测试运行

    [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:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo
    [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo
    [org.apache.ibatis.io.DefaultVFS]-Reader entry: User.class
    [org.apache.ibatis.io.DefaultVFS]-Listing file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo
    [org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo/User.class
    [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/IDEA%e6%95%b0%e6%8d%ae/mybatis-study/mybatis-04/target/classes/com/yin/pojo/User.class
    [org.apache.ibatis.io.DefaultVFS]-Reader entry: 漱壕   4 <
    [org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.yin.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 255334292.
    [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f381794]
    [com.yin.dao.UserMapper.getUserById]-==>  Preparing: select * from user where id = ?
    [com.yin.dao.UserMapper.getUserById]-==> Parameters: 1(Integer)
    [com.yin.dao.UserMapper.getUserById]-<==      Total: 1
    User{id=1, name='张三', password='123'}
    [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f381794]
    [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@f381794]
    [org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 255334292 to pool.
    
    Process finished with exit code 0
    

Log4j简单使用

  1. 在要使用Log4j的类中,导入包

     import org.apache.log4j.Logger
    
  2. 日志对象,参数为当前类的class对象

    static Logger logger = Logger.getLogger(UserDaoTest.class);
    
  3. 日志级别

    @Test
    public void testLog4j(){
      logger.info("info:进入了testLog4j"); //info:信息
      logger.debug("debug:进入了testLog4j");
      logger.error("error:进入了testLog4j");
    }
    =================================================
    [com.yin.dao.UserDaoTest]-info:进入了testLog4j
    [com.yin.dao.UserDaoTest]-debug:进入了testLog4j
    [com.yin.dao.UserDaoTest]-error:进入了testLog4j
    

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小尹^_^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值