Mybatis日志(5)

泪湿孤枕 提交于 2020-08-17 01:16:36

日志工厂

如果数据库出现异常错误,排错的时候,日志便是最好的工具助手

SLF4J、LOG4J、LOG4J2、JDK_LOGGING、COMMONS_LOGGING、STDOUT_LOGGING、NO_LOGGING

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

在MyBatis中使用哪个日志实现,在设置中进行配置

标准日志的实现

例如:使用STDOUT_LOGGING

  • 首先在配置文件中配置日志
 <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
  • 结果(日志打印):
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
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 1658699134.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@62ddbd7e]
==>  Preparing: select id,name,pwd as password from mybatis.user where name like ? 
==> Parameters: %哈%(String)
<==    Columns: id, name, password
<==        Row: 4, 哈哈哈哈, 45444
<==        Row: 5, 哈士奇, 121122
<==      Total: 2
User{id=4, name='哈哈哈哈', password='45444'}
User{id=5, name='哈士奇', password='121122'}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@62ddbd7e]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@62ddbd7e]
Returned connection 1658699134 to pool.

Process finished with exit code 0

LOG4J

简单介绍:

  • Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;
  • 我们也可以控制每一条日志的输出格式;
  • 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
  • 最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。

步骤

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/malajava.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>
        <!--&lt;!&ndash;标准日志的实现&ndash;&gt;-->
        <!--<setting name="logImpl" value="STDOUT_LOGGING"/>-->
        <setting name="logImpl" value="LOG4J"/>
    </settings>

4、测试使用

G:\Environments\jdk1.8\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:G:\application\ideaIU-2018.3.5.win\lib\idea_rt.jar=5462:G:\application\ideaIU-2018.3.5.win\bin -Dfile.encoding=UTF-8 -classpath G:\application\ideaIU-2018.3.5.win\lib\idea_rt.jar;G:\application\ideaIU-2018.3.5.win\plugins\junit\lib\junit-rt.jar;G:\application\ideaIU-2018.3.5.win\plugins\junit\lib\junit5-rt.jar;G:\Environments\jdk1.8\jre\lib\charsets.jar;G:\Environments\jdk1.8\jre\lib\deploy.jar;G:\Environments\jdk1.8\jre\lib\ext\access-bridge-64.jar;G:\Environments\jdk1.8\jre\lib\ext\cldrdata.jar;G:\Environments\jdk1.8\jre\lib\ext\dnsns.jar;G:\Environments\jdk1.8\jre\lib\ext\jaccess.jar;G:\Environments\jdk1.8\jre\lib\ext\jfxrt.jar;G:\Environments\jdk1.8\jre\lib\ext\localedata.jar;G:\Environments\jdk1.8\jre\lib\ext\nashorn.jar;G:\Environments\jdk1.8\jre\lib\ext\sunec.jar;G:\Environments\jdk1.8\jre\lib\ext\sunjce_provider.jar;G:\Environments\jdk1.8\jre\lib\ext\sunmscapi.jar;G:\Environments\jdk1.8\jre\lib\ext\sunpkcs11.jar;G:\Environments\jdk1.8\jre\lib\ext\zipfs.jar;G:\Environments\jdk1.8\jre\lib\javaws.jar;G:\Environments\jdk1.8\jre\lib\jce.jar;G:\Environments\jdk1.8\jre\lib\jfr.jar;G:\Environments\jdk1.8\jre\lib\jfxswt.jar;G:\Environments\jdk1.8\jre\lib\jsse.jar;G:\Environments\jdk1.8\jre\lib\management-agent.jar;G:\Environments\jdk1.8\jre\lib\plugin.jar;G:\Environments\jdk1.8\jre\lib\resources.jar;G:\Environments\jdk1.8\jre\lib\rt.jar;F:\masterMybatis\Mybatis\mybatis-04\target\test-classes;F:\masterMybatis\Mybatis\mybatis-04\target\classes;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar;C:\Users\Administrator\.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;C:\Users\Administrator\.m2\repository\org\mybatis\mybatis\3.5.3\mybatis-3.5.3.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.malajava.dao.UserDaoTest,getUserLike
[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.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 210156003.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@c86b9e3]
[com.malajava.dao.UserMapper.getUserLike]-==>  Preparing: select id,name,pwd as password from mybatis.user where name like ? 
[com.malajava.dao.UserMapper.getUserLike]-==> Parameters: %哈%(String)
[com.malajava.dao.UserMapper.getUserLike]-<==      Total: 2
User{id=4, name='哈哈哈哈', password='45444'}
User{id=5, name='哈士奇', password='121122'}
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@c86b9e3]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@c86b9e3]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 210156003 to pool.

简单使用

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

2、日志对象加载参数为当前类的class

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

3、日志级别

[com.malajava.dao.UserDaoTest]-info:进入testLog4j方法
[com.malajava.dao.UserDaoTest]-debug:进入testLog4j方法
[com.malajava.dao.UserDaoTest]-error:进入testLog4j方法
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!