首页 > 其他分享 >mybatis-日志(log4j)

mybatis-日志(log4j)

时间:2022-08-17 10:37:38浏览次数:56  
标签:ibatis apache io mybatis org 日志 com log4j

### 日志

#### 1、日志:

如果数据库操作出现异常,需要拍错。日志就是最好的助手。

以前 sout debug

现在 日志

<setting>配置

| logImpl | 指定 MyBatis 所用日志的具体实现,未指定时将自动查找。 | SLF4J \| LOG4J \| LOG4J2 \| JDK_LOGGING \| COMMONS_LOGGING \| STDOUT_LOGGING \| NO_LOGGING | |
| ------- | ----------------------------------------------------- | ------------------------------------------------------------ | ---- |
| | | | |

掌握:

**LOG4J**

**STDOUT_LOGGING**

在mybatis中具体使用哪一个,在设置中设定。

##### 1.1 STDOUT_LOGGING(标准日志)实现

mybatis-config.xml中配置STDOUT_LOGGING:

```xml
<settings>
<!--标准的日志工厂实现-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
```

就可以用了

##### 1.2 LOG4J 实现

###### **什么是LOG4J?**

Log4j是[Apache](https://baike.baidu.com/item/Apache/8512995)的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是[控制台](https://baike.baidu.com/item/控制台/2438626)、文件、[GUI](https://baike.baidu.com/item/GUI)组件;

我们也可以控制每一条日志的输出格式;

过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。

可以通过一个[配置文件](https://baike.baidu.com/item/配置文件/286550)来灵活地进行配置,而不需要修改应用的代码。

###### 实现

(1)先导入log4g的包

```xml
<!-- 加入log4j支持 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
```

(2)log4j.properties

```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/logFile.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为日志的实现

```xml
<settings>
<!--标准的日志工厂实现-->
<!--<setting name="logImpl" value="STDOUT_LOGGING"/>-->
<!--log4j实现-->
<setting name="logImpl" value="log4j"/>
</settings>
```

 

(4)log4j使用,直接运行测试类

```txt
Connected to the target VM, address: '127.0.0.1:55236', transport: 'socket'
[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.io.VFS]-Class not found: org.jboss.vfs.VFS
[org.apache.ibatis.io.JBoss6VFS]-JBoss 6 VFS API is not available in this environment.
[org.apache.ibatis.io.VFS]-Class not found: org.jboss.vfs.VirtualFile
[org.apache.ibatis.io.VFS]-VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
[org.apache.ibatis.io.VFS]-Using VFS adapter org.apache.ibatis.io.DefaultVFS
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/workspace/Mybatis-Study/mybatis-03/target/classes/com/zy/pojo
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/workspace/Mybatis-Study/mybatis-03/target/classes/com/zy/pojo
[org.apache.ibatis.io.DefaultVFS]-Reader entry: User.class
[org.apache.ibatis.io.DefaultVFS]-Listing file:/D:/workspace/Mybatis-Study/mybatis-03/target/classes/com/zy/pojo
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/workspace/Mybatis-Study/mybatis-03/target/classes/com/zy/pojo/User.class
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/workspace/Mybatis-Study/mybatis-03/target/classes/com/zy/pojo/User.class
[org.apache.ibatis.io.DefaultVFS]-Reader entry: ���� 4 @ .
[org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.zy.pojo.User matches criteria [is assignable to Object]
[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 1523649562.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5ad10c1a]
[com.zy.dao.UserMapper.getUserList]-==> Preparing: select * from user
[com.zy.dao.UserMapper.getUserList]-==> Parameters:
[com.zy.dao.UserMapper.getUserList]-<== Total: 6
User{id=1, name='zy', password='123456'}
User{id=3, name='张六', password='595959'}
User{id=4, name='王五', password='7789'}
User{id=6, name='阿里', password='258789'}
User{id=7, name='王三', password='7789'}
User{id=9, name='李一', password='zasa458'}
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5ad10c1a]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5ad10c1a]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 1523649562 to pool.

```

 

###### 简单使用

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

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

```java
public class UserTest {
static Logger logger = Logger.getLogger(UserTest.class);
@Test
public void getUserList(){
```

3、日志级别

Log4j建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG

标签:ibatis,apache,io,mybatis,org,日志,com,log4j
From: https://www.cnblogs.com/weijie1215/p/16594114.html

相关文章