Mybatis之日志(Mybatis log)

Mybatis

6、日志

6.1、日志工厂

  • 如果一个数据库操作,出现了异常,我们需要排错。日志就是最好的助手!
  • 曾经:sout、debug
  • 现在:日志工厂!
  • SLF4J
  • **LOG4J **
  • LOG4J2
  • JDK_LOGGING
  • COMMONS_LOGGING
  • **STDOUT_LOGGING **
  • NO_LOGGING
  • 在Mybatis中具体使用那个一日志实现,在设置中设定!

    STDOUT_LOGGING标准日志输出
    在mybatis核心配置文件中,配置我们的日志!

  • STDOUT_LOGGING标准日志输出
  • 在mybatis核心配置文件中,配置我们的日志!
  • “C:\Program Files\Java\jdk1.8.0_101\bin\java.exe” -ea -Didea.test.cyclic.buffer.size=1048576 “-javaagent:F:\IDEA\IntelliJ IDEA
    PooledDataSource forcefully closed/removed all connections.
    PooledDataSource forcefully closed/removed all connections.
    PooledDataSource forcefully closed/removed all connections.
    PooledDataSource forcefully closed/removed all connections.
    Opening JDBC Connection
    Created connection 156545103.
    Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@954b04f]
    ==> Preparing: select * from user where id= ?;
    ==> Parameters: 2(Integer)
    <== Columns: id, name, pwd <== Row: 2, 6, 6 <== Total: 1 User{id=2, name='6', password='6'} Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@954b04f] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@954b04f] Returned connection 156545103 to pool. Process finished with exit code 0

6.2、LOG4J

6.2.1、什么是LOG4J

  • LOG4J是Apache的一个开源项目,通过使用LOG4J,我们可以控制日志信息输送的目的地是控制台、文件、GUl组件
  • ]我们也可以控制每一条日志的输出格式
  • 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程
  • 通过一个配置文件来灵活地进行配置,而不需要修改应用的代码

6.2.2、先导入log4j的包


  • log4j
    log4j
    1.2.17

  • ### set log levels ###
    log4j.rootLogger = DEBUG,Console,File

    ### 输出到控制台 ###
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.Target=System.out
    1og4j.appender.Console.Threshold=DEBUG
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}]-%l:%m%n

    ### 输出到日志文件 ###
    log4j.appender.File=org.apache.log4j.RollingFileAppender
    log4j.appender.File.File=${project}src\\main\\resources\\jcooling.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{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

    ### 日志输出级别 ###
    1og4j.logger.org.mybatis=DEBUG
    1og4j.logger.java.sq1=DEBUG
    1og4j.logger.java.sq1.Statement=DEBUG
    1og4j.logger.java.sql.ResultSet=DEBUG
    1og4j.logger.java.sq1.PreparedStatement=DEBUG

  • 配置log4j为日志的实现





6.2.3、log4j的使用

  • 直接测试运行刚才的查询
  • [21/11/23 12:45:01:658]-org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:105):Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl’ adapter.
    [21/11/23 12:45:01:660]-org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:105):Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl’ adapter.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:727]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136):Opening JDBC Connection
    [21/11/23 12:45:01:874]-org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:424):Created connection 852445367.
    [21/11/23 12:45:01:875]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100):Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@32cf48b7]
    [21/11/23 12:45:01:878]-org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143):==> Preparing: select * from user where id= ?;
    [21/11/23 12:45:01:898]-org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143):==> Parameters: 2(Integer)
    [21/11/23 12:45:01:910]-org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143):<== Total: 1 User{id=2, name='6', password='6'} [21/11/23 12:45:01:911]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122):Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@32cf48b7] [21/11/23 12:45:01:911]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90):Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@32cf48b7] [21/11/23 12:45:01:912]-org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:381):Returned connection 852445367 to pool. Process finished with exit code 0
  • 简单使用

    在要使用Log4j的类中,导入包 import org.apache.log4j.Logger;

    日志对象,参数为当前类的class

    static Logger logger = Logger.getLogger(UserMapperTest.class);

    日志级别

    @Test
    public void testLog4j(){
    logger.info(“info:进入了testLog4j”);
    logger.debug(“debug:进入了testLog4j”);
    logger.error(“error:进入了testLog4j”);
    }

  • 在要使用Log4j的类中,导入包 import org.apache.log4j.Logger;
  • 日志对象,参数为当前类的class
  • static Logger logger = Logger.getLogger(UserMapperTest.class);

  • 日志级别

    @Test
    public void testLog4j(){
    logger.info(“info:进入了testLog4j”);
    logger.debug(“debug:进入了testLog4j”);
    logger.error(“error:进入了testLog4j”);
    }

  • @Test
    public void testLog4j(){
    logger.info(“info:进入了testLog4j”);
    logger.debug(“debug:进入了testLog4j”);
    logger.error(“error:进入了testLog4j”);
    }

————————

Mybatis

6. Log

6.1 log factory

  • If an exception occurs in a database operation, we need to troubleshoot it. Log is the best assistant!
  • 曾经:sout、debug
  • Now: log factory!
  • SLF4J
  • **LOG4J **
  • LOG4J2
  • JDK_LOGGING
  • COMMONS_LOGGING
  • **STDOUT_LOGGING **
  • NO_LOGGING
  • Which log implementation is used in mybatis? Set it in settings!
    STDOUT_ Logging standard log output
    In the mybatis core configuration file, configure our logs!
  • STDOUT_LOGGING标准日志输出
  • In the mybatis core configuration file, configure our logs!
  • “C:\Program Files\Java\jdk1.8.0_101\bin\java.exe” -ea -Didea.test.cyclic.buffer.size=1048576 “-javaagent:F:\IDEA\IntelliJ IDEA
    PooledDataSource forcefully closed/removed all connections.
    PooledDataSource forcefully closed/removed all connections.
    PooledDataSource forcefully closed/removed all connections.
    PooledDataSource forcefully closed/removed all connections.
    Opening JDBC Connection
    Created connection 156545103.
    Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@954b04f]
    ==> Preparing: select * from user where id= ?;
    ==> Parameters: 2(Integer)
    <== Columns: id, name, pwd <== Row: 2, 6, 6 <== Total: 1 User{id=2, name='6', password='6'} Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@954b04f] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@954b04f] Returned connection 156545103 to pool. Process finished with exit code 0

6.2、LOG4J

6.2.1 what is log4j

  • Log4j is an open source project of Apache. By using log4j, we can control that the destination of log information transmission is console, file and Gul components
  • ]We can also control the output format of each log
  • By defining the level of each log information, we can control the log generation process in more detail
  • It can be configured flexibly through a configuration file without modifying the application code

6.2.2. Import log4j package first


  • log4j
    log4j
    1.2.17

  • ### set log levels ###
    log4j.rootLogger = DEBUG,Console,File

    ### 输出到控制台 ###
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.Target=System.out
    1og4j.appender.Console.Threshold=DEBUG
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}]-%l:%m%n

    ### 输出到日志文件 ###
    log4j.appender.File=org.apache.log4j.RollingFileAppender
    log4j.appender.File.File=${project}src\\main\\resources\\jcooling.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{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

    ### 日志输出级别 ###
    1og4j.logger.org.mybatis=DEBUG
    1og4j.logger.java.sq1=DEBUG
    1og4j.logger.java.sq1.Statement=DEBUG
    1og4j.logger.java.sql.ResultSet=DEBUG
    1og4j.logger.java.sq1.PreparedStatement=DEBUG

  • 配置log4j为日志的实现





6.2.3 use of log4j

  • Run the query directly
  • [21/11/23 12:45:01:658]-org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:105):Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl’ adapter.
    [21/11/23 12:45:01:660]-org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:105):Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl’ adapter.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:674]-org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:353):PooledDataSource forcefully closed/removed all connections.
    [21/11/23 12:45:01:727]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:136):Opening JDBC Connection
    [21/11/23 12:45:01:874]-org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:424):Created connection 852445367.
    [21/11/23 12:45:01:875]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:100):Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@32cf48b7]
    [21/11/23 12:45:01:878]-org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143):==> Preparing: select * from user where id= ?;
    [21/11/23 12:45:01:898]-org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143):==> Parameters: 2(Integer)
    [21/11/23 12:45:01:910]-org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:143):<== Total: 1 User{id=2, name='6', password='6'} [21/11/23 12:45:01:911]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:122):Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@32cf48b7] [21/11/23 12:45:01:911]-org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:90):Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@32cf48b7] [21/11/23 12:45:01:912]-org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:381):Returned connection 852445367 to pool. Process finished with exit code 0
  • 简单使用

    在要使用Log4j的类中,导入包 import org.apache.log4j.Logger;

    日志对象,参数为当前类的class

    static Logger logger = Logger.getLogger(UserMapperTest.class);

    日志级别

    @Test
    public void testLog4j(){
    logger.info(“info:进入了testLog4j”);
    logger.debug(“debug:进入了testLog4j”);
    logger.error(“error:进入了testLog4j”);
    }

  • 在要使用Log4j的类中,导入包 import org.apache.log4j.Logger;
  • Log object. The parameter is the class of the current class
  • static Logger logger = Logger.getLogger(UserMapperTest.class);

  • 日志级别

    @Test
    public void testLog4j(){
    logger.info(“info:进入了testLog4j”);
    logger.debug(“debug:进入了testLog4j”);
    logger.error(“error:进入了testLog4j”);
    }

  • @Test
    public void testLog4j(){
    logger.info(“info:进入了testLog4j”);
    logger.debug(“debug:进入了testLog4j”);
    logger.error(“error:进入了testLog4j”);
    }