前言
日志作为我们开发中的辅助工具,早期我们开发只是使用 System.out.println()
来输出内容,但是在实际开发中我们更青睐于日志系统,Mybatis 也支持日志系统的配置,我们来看看日志系统是如何配置启动的。
设置日志系统配置
日志系统的配置相当简单,只需要在 mybatis-config.xml
文件中加入 <settings>
标签即可,注意这里的 <setting>
中 name
和 value
都不能写错,必须和官方提供的文档相同,否则就会有不知名的原因报错,我们为了减少报错,我一般的习惯是直接打开官方文档复制即可。
支持的类型有:
- SLF4J
- LOG4J 【掌握】
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING【掌握】
- NO_LOGGING
我们复用 Mybatis-入门(五)
项目的代码,在代码的基础上,加入这个配置选项,我们先使用系统 STDOUT_LOGGING
标准输出。
1 |
|
2 |
|
3 |
|
4 |
|
5 | |
6 | <configuration> |
7 | <properties resource="db.properties"/> |
8 | |
9 | <settings> |
10 | <!-- 标准的日志工厂 --> |
11 | <setting name="logImpl" value="STDOUT_LOGGING"/> |
12 | </settings> |
13 | |
14 | <typeAliases> |
15 | <typeAlias type="com.yubulang.pojo.User" alias="User"/> |
16 | </typeAliases> |
17 | |
18 | <environments default="development"> |
19 | <environment id="development"> |
20 | <transactionManager type="JDBC"/> |
21 | <dataSource type="POOLED"> |
22 | <property name="driver" value="${driver}"/> |
23 | <property name="url" value="${url}"/> |
24 | <property name="username" value="${username}"/> |
25 | <property name="password" value="${password}"/> |
26 | </dataSource> |
27 | </environment> |
28 | </environments> |
29 | |
30 | <mappers> |
31 | <mapper resource="com/yubulang/dao/UserMapper.xml"/> |
32 | </mappers> |
33 | </configuration> |
配置之后我们运行对应的测试,这样我们就可以看到命令行中就出现了很多日志信息:
1 | Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter. |
2 | PooledDataSource forcefully closed/removed all connections. |
3 | PooledDataSource forcefully closed/removed all connections. |
4 | PooledDataSource forcefully closed/removed all connections. |
5 | PooledDataSource forcefully closed/removed all connections. |
6 | Opening JDBC Connection |
7 | Created connection 266437232. |
8 | Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@fe18270] |
9 | ==> Preparing: select * from users where id = ?; |
10 | ==> Parameters: 1(Integer) |
11 | <== Columns: id, name, password |
12 | <== Row: 1, 鱼不浪, 666666 |
13 | <== Total: 1 |
14 | User{id=1, name='鱼不浪', password='666666'} |
15 | Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@fe18270] |
16 | Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@fe18270] |
17 | Returned connection 266437232 to pool. |
LOG4J 日志
通常我们使用的最多的就是 LOG4J
这个日志,学习一个东西的时候需要知道它是什么
Log4j 是 Apache 的一个开源项目,通过使用 Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI 组件
定义日志的输出格式
定义每一条的日志的级别
通过修改配置文件进行配置,不需要修改代码,这个才是我最喜欢的
先导入 LOG4J 的包,注意这里在配置文件当中必须大写且完全一样,直接 pom.xml 中添加
1 | <!-- log4j --> |
2 | <dependency> |
3 | <groupId>log4j</groupId> |
4 | <artifactId>log4j</artifactId> |
5 | <version>1.2.17</version> |
6 | </dependency> |
配置完成后,maven 安装一下对应的包,当然我们还要配置对应的 log4j 属性,我们把它放到 resources
的 log4j.properties
中去。
1 | # 设置rootLogger的level等级为DEBUG,后面跟输出目的地 console, file |
2 | log4j.rootLogger=DEBUG,console,file |
3 | |
4 | # 控制台输出的相关配置 |
5 | log4j.appender.console=org.apache.log4j.ConsoleAppender |
6 | log4j.appender.console.Target=System.out |
7 | log4j.appender.console.Threshold=DEBUG |
8 | log4j.appender.console.layout=org.apache.log4j.PatternLayout |
9 | log4j.appender.console.layut.ConversionPattern=[%c]-%m%n |
10 | |
11 | # 文件输出的相关设置 |
12 | log4j.appender.file = org.apache.log4j.RollingFileAppender |
13 | log4j.appender.file.File = ./log/yubulang.log |
14 | log4j.appender.file.MaxFileSize = 10mb |
15 | log4j.appender.file.Threshold = DEBUG |
16 | log4j.appender.file.layout = org.apache.log4j.PatternLayout |
17 | log4j.appender.file.layout.ConversionPattern = [%p][%d{yy-MM-dd}][%c]%m%n |
18 | |
19 | |
20 | # 日志输出级别 |
21 | log4j.logger.org.mybatis=DEBUG |
22 | log4j.logger.java.sql=DEBUG |
23 | log4j.logger.java.sql.Statement=DEBUG |
24 | log4j.logger.java.sql.ResultSet=DEBUG |
25 | log4j.logger.java.sql.PreparedStatement=DEBUG |
将 <setting name="logImpl" value="STDOUT_LOGGING" />
改为 <setting name="logImpl" value="LOG4J" />
这样运行我们的测试文件就已经可以正常运行了。输出日志如下:
1 | Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter. |
2 | Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter. |
3 | PooledDataSource forcefully closed/removed all connections. |
4 | PooledDataSource forcefully closed/removed all connections. |
5 | PooledDataSource forcefully closed/removed all connections. |
6 | PooledDataSource forcefully closed/removed all connections. |
7 | Opening JDBC Connection |
8 | Created connection 300031246. |
9 | Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@11e21d0e] |
10 | ==> Preparing: select * from users where id = ?; |
11 | ==> Parameters: 1(Integer) |
12 | <== Total: 1 |
13 | User{id=1, name='鱼不浪', password='666666'} |
14 | Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@11e21d0e] |
15 | Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@11e21d0e] |
16 | Returned connection 300031246 to pool. |
Log4j 的简单使用
我们在测试类中 com/yubulang/dao/UserMapperTest.java
添加以下方法
1 | package com.yubulang.dao; |
2 | |
3 | import com.yubulang.pojo.User; |
4 | import com.yubulang.utils.MybatisUtils; |
5 | import org.apache.ibatis.session.SqlSession; |
6 | import org.apache.log4j.Logger; |
7 | import org.junit.Test; |
8 | |
9 | public class UserMapperTest { |
10 | static Logger logger = Logger.getLogger(UserMapperTest.class); |
11 | |
12 | |
13 | public void testGetUserById() { |
14 | SqlSession sqlSession = MybatisUtils.getSqlSession(); |
15 | |
16 | UserMapper mapper = sqlSession.getMapper(UserMapper.class); |
17 | |
18 | User user = mapper.getUserById(1); |
19 | |
20 | System.out.println(user); |
21 | |
22 | sqlSession.close(); |
23 | } |
24 | |
25 | |
26 | public void testLog4j() { |
27 | // 普通信息 |
28 | logger.info("info:进入了testLog4j"); |
29 | logger.debug("debug:进入了testLog4j"); |
30 | logger.error("error:进入了testLog4j"); |
31 | } |
32 | } |
总结
日志在我们的项目开发中十分的重要,但是好运的是使用他并不困难,值要配置以下就能轻松的使用。