日志工厂

日志工厂:

如果一个数据库操作出现了异常,我们需要排错,所以要用到日志

logImpl:指定MyBatis所用日志的具体实现,未指定时自动查找

  • SLF4J
  • LOG4J (常用日志)
  • LOG4J2
  • JDK_LOGGING
  • COMMONS_LOGGING
  • STDOUT_LOGGING (标准日志工厂)
  • NO_LOGGING

1.STDOUT_LOGGING

在MyBatis中,具体使用那个日志实现,在mybatis-config.xml设置中设定

<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 753321708.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2ce6c6ec]
==>  Preparing: select * from mybatis.User where id = ?
==> Parameters: 1(Integer)
<==    Columns: id, name, pwd
<==        Row: 1, 张三, 123456
<==      Total: 1
User{id=1, name='张三', password='123456'}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2ce6c6ec]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2ce6c6ec]
Returned connection 753321708 to pool.

2.Log4J:

1.先导入Log4J的包

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

2.创建log4j.properties文件并配置
日志工厂

log4j.properties
#将等级为DEBUG的日志信息输出到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/mybatis.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>
<setting name="logImpl" value="log4j"/>
</settings>

4.使用log4j

[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 337533935.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@141e5bef]
[Mapper.UserMapper.getUserById]-==>  Preparing: select * from mybatis.User where id = ?
[Mapper.UserMapper.getUserById]-==> Parameters: 1(Integer)
[Mapper.UserMapper.getUserById]-<==      Total: 1
User{id=1, name='张三', password='123456'}
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@141e5bef]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@141e5bef]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 337533935 to pool.

简单使用log4j:

1.在要使用log4j的类中导包

import org.apache.log4j.Logger;

2.在类中生成日志对象,加载参数为当前类的class

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

3.使用log4j对象调用方法输出

  • info:普通信息
  • debug:需要调试
  • error:紧急错误
@Test
public void testlog4j(){
logger.info("info:进入了testlog4j方法"); //提示级别
logger.debug("debug:进入了testlog4j方法");//debug级别
logger.error("error:进入了log4jtest方法");//紧急错误,一般在try/catch中使用
}

日志工厂
在log4j.properties文件中配置的生成文件位置可以找到生成的log日志文件
日志工厂

上一篇:浏览器访问项目资源时,报HTTP Status 500 – Internal Server Error


下一篇:阿里巴巴Java方向面试题汇总(含答案)