随笔 - 33,  文章 - 0,  评论 - 0,  阅读 - 8297

### 日志

#### 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

posted on   可惜君已逝i  阅读(101)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示