6.日志

6.日志

6.1日志工厂

  • Mybatis 通过使用内置的日志工厂提供日志功能。内置日志工厂将会把日志工作委托给下面的实现之一:

    • SLF4J
    • Apache Commons Logging
    • Log4j 2
    • Log4j
    • JDK logging
  • MyBatis 内置日志工厂会基于运行时检测信息选择日志委托实现。

  • 它会(按上面罗列的顺序)使用第一个查找到的实现。当没有找到这些实现时,将会禁用日志功能。

​ 第一步:使用mybatis的默认配置

<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

​ 第二步:执行测试,查看日志输出

Opening JDBC Connection  //打卡JDBC连接
Created connection 1533985074.  //创建一个连接
//设置自动提交为flase
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5b6ec132]
==>  Preparing: select * from mybatis.user//查询所有记录
==> Parameters: //参数(这里没有)
<==    Columns: id, name, email, phone, gender, password, age, create_time, update_time//数据库字段
<==        Row: 1, xuan, 1870136088@qq.com, 13927497470, 1, 123456, 111, 2020-07-11 18:15:33, 2020-07-12 13:13:07
<==        Row: 2, yingge, zzkshierzi@163.com, 11111111111, 0, 111111, 111, 2020-07-11 18:16:27, 2020-07-12 13:13:07
<==        Row: 6, yanggoushen, 5201414@qq.com, 1515155111, 1, 1111, 38, null, null
<==        Row: 7, happyday, bjfbe@qq.com, , null, , 11, 2020-07-12 14:45:18, 2020-07-12 14:45:18
<==      Total: 4
User{id=1, name='xuan', email='1870136088@qq.com', phone='13927497470', gender=1, password='123456', age=111, create_time=Sun Jul 12 02:15:33 GMT+08:00 2020, update_time=Sun Jul 12 21:13:07 GMT+08:00 2020}
User{id=2, name='yingge', email='zzkshierzi@163.com', phone='11111111111', gender=0, password='111111', age=111, create_time=Sun Jul 12 02:16:27 GMT+08:00 2020, update_time=Sun Jul 12 21:13:07 GMT+08:00 2020}
User{id=6, name='yanggoushen', email='5201414@qq.com', phone='1515155111', gender=1, password='1111', age=38, create_time=null, update_time=null}
User{id=7, name='happyday', email='bjfbe@qq.com', phone='', gender=null, password='', age=11, create_time=Sun Jul 12 22:45:18 GMT+08:00 2020, update_time=Sun Jul 12 22:45:18 GMT+08:00 2020}
//设置自动提交为true
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5b6ec132]
//关闭JDBC连接
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5b6ec132]
//将连接返回给连接池
Returned connection 1533985074 to pool.

6.2 Log4j

  • 什么是log4j?
  • Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件等
  • 我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以**通过一个配置文件来灵活地进行配置,**而不需要修改应用的代码。

​ 第一步:在pom.xml中新增依赖加入log4j支持

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

​ 第二步:在CLASSPATH下建立log4j.properties。

  • 第一种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/xuan.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
  • 第二种log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

​ 第三步:配置log4j为日志的实现

<settings>
    <setting name="logImpl" value="LOG4J"/>
</settings>

​ 第四步:测试运行

【org.apache.ibatis.transaction.jdbc.JdbcTransaction】-Opening JDBC Connection
【org.apache.ibatis.datasource.pooled.PooledDataSource】-Created connection 1659840424.
【org.apache.ibatis.transaction.jdbc.JdbcTransaction】-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@62ef27a8]
【com.xuan.mapper.UserMapper.findAll】-==>  Preparing: select * from mybatis.user
【com.xuan.mapper.UserMapper.findAll】-==> Parameters: 
【com.xuan.mapper.UserMapper.findAll】-<==      Total: 4
User{id=1, name='xuan', email='1870136088@qq.com', phone='13927497470', gender=1, password='123456', age=111, create_time=Sun Jul 12 02:15:33 GMT+08:00 2020, update_time=Sun Jul 12 21:13:07 GMT+08:00 2020}
User{id=2, name='yingge', email='zzkshierzi@163.com', phone='11111111111', gender=0, password='111111', age=111, create_time=Sun Jul 12 02:16:27 GMT+08:00 2020, update_time=Sun Jul 12 21:13:07 GMT+08:00 2020}
User{id=6, name='yanggoushen', email='5201414@qq.com', phone='1515155111', gender=1, password='1111', age=38, create_time=null, update_time=null}
User{id=7, name='happyday', email='bjfbe@qq.com', phone='', gender=null, password='', age=11, create_time=Sun Jul 12 22:45:18 GMT+08:00 2020, update_time=Sun Jul 12 22:45:18 GMT+08:00 2020}
【org.apache.ibatis.transaction.jdbc.JdbcTransaction】-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@62ef27a8]
【org.apache.ibatis.transaction.jdbc.JdbcTransaction】-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@62ef27a8]
【org.apache.ibatis.datasource.pooled.PooledDataSource】-Returned connection 1659840424 to pool.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值