MyBatis的日志工厂 | MyBatis系列

共 13152字,需浏览 27分钟

 ·

2021-07-22 12:52

前言

  • 在实际开发中,项目上线之后,如果发生问题,我们定位问题不可能一直去看控制台日志(即nohup文件),正确的做法当然是看日志啦!

  • logImpl指定 MyBatis 所用日志的具体实现,未指定时将自动查找。SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING未设置
  • MyBatis支持多种日志工厂:

    • SLF4J

    • LOG4J 【重点】

    • LOG4J2

    • JDK_LOGGING

    • COMMONS_LOGGING

    • STDOUT_LOGGING 【重点】

    • NO_LOGGING

  • 在这里只介绍LOG4JSTDOUT_LOGGING 其他的工作中用的少,有兴趣的同学可以自行研究一下!

一、STDOUT_LOGGING  日志

  • 这个比较简单,无需引入jar包啥的,内置的日志工厂!

  • 我们先看下,不加日志工厂的话控制台会输出哪些内容:

  • 位置不要放错哦~ 还记得上一篇的位置吧,哈哈

  • 具体代码如下:

    •     <settings>
      <setting name="logImpl" value="STDOUT_LOGGING"/>
      </settings>
      复制代码
  • 再次运行结果如下:

    • Class not found: org.jboss.vfs.VFS
      JBoss 6 VFS API is not available in this environment.
      Class not found: org.jboss.vfs.VirtualFile
      VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
      Using VFS adapter org.apache.ibatis.io.DefaultVFS
      Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo
      Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo
      Reader entry: User.class
      Listing file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo
      Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo/User.class
      Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo/User.class
      Reader entry: ���� 4 m
      Checking to see if class com.dy.pojo.User matches criteria [is assignable to Object]
      PooledDataSource forcefully closed/removed all connections.
      PooledDataSource forcefully closed/removed all connections.
      PooledDataSource forcefully closed/removed all connections.
      PooledDataSource forcefully closed/removed all connections.
      Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper
      Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper
      Reader entry: UserMapper.class
      Reader entry: UserMapper.xml
      Listing file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper
      Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.class
      Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.class
      Reader entry: ���� 4
      Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.xml
      Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.xml
      Reader entry: <?xml version
      ="1.0" encoding="UTF-8" ?>
      Checking to see if class com.dy.mapper.UserMapper matches criteria [is assignable to Object]
      Opening JDBC Connection
      Created connection 1159114532.
      Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4516af24]
      ==> Preparing: select * from user
      ==> Parameters:
      <== Columns: id, name, age, like
      <== Row: 30, 丁大大, 23, 钓鱼
      <== Row: 31, 面条, 22, 王者荣耀
      <== Row: 32, 面条, 50, 王者荣耀
      <== Total: 3
      User(id=30, name=丁大大, age=23, like=钓鱼)
      User(id=31, name=面条, age=22, like=王者荣耀)
      User(id=32, name=面条, age=50, like=王者荣耀)
      Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4516af24]
      Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@4516af24]
      Returned connection 1159114532 to pool.

      Process finished with exit code 0

      复制代码
    • 主要看以下内容:

    • Opening JDBC Connection  //打开一个JDBC链接
      Created connection 1159114532. //创建链接
      Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4516af24]
      //在JDBC Connection上将自动提交设置为false
      ==> Preparing: select * from user //查询语句
      ==> Parameters: //查询参数
      <== Columns: id, name, age, like
      //下面是结果
      <== Row: 30, 丁大大, 23, 钓鱼
      <== Row: 31, 面条, 22, 王者荣耀
      <== Row: 32, 面条, 50, 王者荣耀
      <== Total: 3
      复制代码
  • 现在我们练习的项目很小,sql啥的比较少,出问题很容易找到。

  • 但是在实际开发过程中,项目大,sql多,不看日志定位问题的话比较耗时耗力!

  • 所以推荐大家从一开始就要养成配置日志工厂的习惯!这对我们定位问题有很大的帮助!!!

二、LOG4J 日志

①、MyBatis中使用

  • 什么是Log4j?

    • 有问题不懂找百度   百度百科

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

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

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

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

  • 这个就不是内置的日志了,先导包:

    • <!-- https://mvnrepository.com/artifact/log4j/log4j -->
      <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
      </dependency>

      复制代码
  • 更改核心配置文件

    •     <settings>
      <setting name="logImpl" value="LOG4J"/>
      </settings>
      复制代码
  • 新增log4j.properties配置文件

    • 首页可以发现,我们的项目中出现了一个新的目录:log,目录下的就是我们的日志文件dayu.log

    • 打开瞅瞅里面是啥?

    • [DEBUG][21-07-08][org.apache.ibatis.logging.LogFactory]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
      [DEBUG][21-07-08][org.apache.ibatis.logging.LogFactory]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
      [DEBUG][21-07-08][org.apache.ibatis.io.VFS]Class not found: org.jboss.vfs.VFS
      [DEBUG][21-07-08][org.apache.ibatis.io.JBoss6VFS]JBoss 6 VFS API is not available in this environment.
      [DEBUG][21-07-08][org.apache.ibatis.io.VFS]Class not found: org.jboss.vfs.VirtualFile
      [DEBUG][21-07-08][org.apache.ibatis.io.VFS]VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
      [DEBUG][21-07-08][org.apache.ibatis.io.VFS]Using VFS adapter org.apache.ibatis.io.DefaultVFS
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Reader entry: User.class
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Listing file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo/User.class
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/pojo/User.class
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Reader entry: ���� 4 m
      [DEBUG][21-07-08][org.apache.ibatis.io.ResolverUtil]Checking to see if class com.dy.pojo.User matches criteria [is assignable to Object]
      [DEBUG][21-07-08][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
      [DEBUG][21-07-08][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
      [DEBUG][21-07-08][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
      [DEBUG][21-07-08][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Reader entry: UserMapper.class
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Reader entry: UserMapper.xml
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Listing file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.class
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.class
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Reader entry: ���� 4
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Find JAR URL: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.xml
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Not a JAR: file:/D:/workSpace/dyj-MyBatis-Project/MyBatis-01-properties/target/classes/com/dy/mapper/UserMapper.xml
      [DEBUG][21-07-08][org.apache.ibatis.io.DefaultVFS]Reader entry: <?xml version
      ="1.0" encoding="UTF-8" ?>
      [DEBUG][21-07-08][org.apache.ibatis.io.ResolverUtil]Checking to see if class com.dy.mapper.UserMapper matches criteria [is assignable to Object]
      [DEBUG][21-07-08][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Opening JDBC Connection
      [DEBUG][21-07-08][org.apache.ibatis.datasource.pooled.PooledDataSource]Created connection 624271064.
      [DEBUG][21-07-08][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25359ed8]
      [DEBUG][21-07-08][com.dy.mapper.UserMapper.getUserInfo]
      ==> Preparing: select * from user
      [DEBUG][21-07-08][com.dy.mapper.UserMapper.getUserInfo]==> Parameters:
      [DEBUG][21-07-08][com.dy.mapper.UserMapper.getUserInfo]<== Total: 3
      [DEBUG][21-07-08][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25359ed8]
      [DEBUG][21-07-08][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@25359ed8]
      [DEBUG][21-07-08][org.apache.ibatis.datasource.pooled.PooledDataSource]Returned connection 624271064 to pool.
      复制代码
    • 再看控制台输出的:这里只截取了重点日志信息。

    • [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
      [org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 624271064.
      [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25359ed8]
      [com.dy.mapper.UserMapper.getUserInfo]-==> Preparing: select * from user
      [com.dy.mapper.UserMapper.getUserInfo]-==> Parameters:
      [com.dy.mapper.UserMapper.getUserInfo]-<== Total: 3
      User(id=30, name=丁大大, age=23, like=钓鱼)
      User(id=31, name=面条, age=22, like=王者荣耀)
      User(id=32, name=面条, age=50, like=王者荣耀)
      [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25359ed8]
      [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@25359ed8]
      [org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 624271064 to pool.

      Process finished with exit code 0
      复制代码
    • #将等级为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/dayu.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
      复制代码
    • 执行看结果:

    • 清清爽爽,一目了然!程序的所有执行信息都成功打印出来了!

②、正常项目中业务层使用

  • 先使用静态引入包

    • static Logger logger = Logger.getLogger(MyTest.class); //这个MyTest.class要和当前所在类名称保持一致!
      复制代码
    • 注意,这里导包不要导错了,一定要是import org.apache.log4j.Logger;,看图说话

  • 业务层:

    •     @Test
      public void selectUser1() {
      logger.info("我是info日志");
      logger.error("我是error日志");
      logger.debug("我是debug日志");
      }
      复制代码
  • 执行瞅瞅结果:

    • 控制台输出:

    • 日志文件追加:

  • 哪里需要打日志可以直接打出来,方便后面出问题好定位!比我们debug排查问题要快很多吧!哈哈哈!

③、log4j.properties配置文件配置详细解释

  • 这些网络上都能找到,而且特别杂,我们不可能记住所有的内容。

  • 只需要记住一些重点的东西!然后将其记到自己的小本本上,后面哪里需要用到直接capy过来即可。因为都是固定的格式的!

  • 这里只提供一个通用的简单模板,具体需要配置啥自己去log4j百度百科自行查找,上面解释的很详细!

  • 模板:

    • #设置日志级别,设定日志记录的最低级别,可设的值有OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者自定义组别,Log4j建议只使用中间四个级别。
      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/dayu.log
      log4j.appender.file.MaxFileSize=10mb
      ##输出DEBUG级别以上的日志
      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
      复制代码



作者:大鱼丶
链接:https://juejin.cn/post/6986451654765707295
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。



浏览 51
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报