6.1 日志工厂

如果一个数据库操作出现了异常,我们需要定位问题,所以要写日志,方便后期debug。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--导入外部配置文件-->
  7. <properties resource="db.properties"/>
  8. <settings>
  9. <!-- 配置日志实现 -->
  10. <setting name="logImpl" value="STDOUT_LOGGING"/>
  11. </settings>
  12. <typeAliases>
  13. <package name="com.lu.entity"/>
  14. </typeAliases>
  15. <environments default="development">
  16. <environment id="development">
  17. <transactionManager type="JDBC"/>
  18. <dataSource type="POOLED">
  19. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  20. <property name="url" value="jdbc:mysql://localhost:3306/${mysql.db}"/>
  21. <property name="username" value="${mysql.username}"/>
  22. <property name="password" value="${mysql.password}"/>
  23. </dataSource>
  24. </environment>
  25. </environments>
  26. <mappers>
  27. <!-- input your mapper here -->
  28. <package name="com.lu.dao"/>
  29. </mappers>
  30. </configuration>

在 mybatis核心配置文件中,添加 settings logImpl来制定对应的日志工厂。

  • STDOUT_LOGGING
  • log4j

    6.2 log4j

    og4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务 器、NT的事件记录器、UNIX Syslog守护进程等

    导入依赖

    1. <dependency>
    2. <groupId>log4j</groupId>
    3. <artifactId>log4j</artifactId>
    4. <version>1.2.17</version>
    5. </dependency>

    添加配置文件

    log4j.properties ```

    日志模式

    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/debug.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

  1. <a name="O3gMs"></a>
  2. ### 启用log4j
  3. ```xml
  4. <settings>
  5. <setting name="logImpl" value="log4j"/>
  6. </settings>

查看日志

  1. org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/Volumes/software/Project/java/mybatis-study/mybatis-05/target/classes/com/lu/dao/TodoMapper.xml
  2. [org.apache.ibatis.io.DefaultVFS]-Reader entry: <?xml version="1.0" encoding="UTF-8"?>
  3. [org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.lu.dao.TodoMapper matches criteria [is assignable to Object]
  4. [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
  5. [org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 2001223946.
  6. [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7748410a]
  7. [com.lu.dao.TodoMapper.queryAll]-==> Preparing: select * from todo;
  8. [com.lu.dao.TodoMapper.queryAll]-==> Parameters:
  9. [com.lu.dao.TodoMapper.queryAll]-<== Total: 3
  10. Todo(id=2, content=睡觉, status=0)
  11. Todo(id=3, content=打豆豆, status=1)
  12. Todo(id=4, content=打豆豆, status=1)
  13. [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7748410a]
  14. [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7748410a]
  15. [org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 2001223946 to pool.

使用log4j

  1. public class AppTest {
  2. static Logger logger = Logger.getLogger(AppTest.class);
  3. /**
  4. * Rigorous Test :-)
  5. */
  6. @Test
  7. public void testLog4j() {
  8. logger.info("info:日志信息");
  9. logger.debug("debug:debug信息");
  10. logger.error("err:错误信息");
  11. }
  12. }

查看日志

debug.log

  1. [INFO][21-12-15][com.lu.AppTest]info:日志信息
  2. [DEBUG][21-12-15][com.lu.AppTest]debug:debug信息
  3. [ERROR][21-12-15][com.lu.AppTest]err:错误信息