mybatis日志工厂

mybatis日志工厂

如果一个数据库操作出现了一个异常,我们需要进行排除。日志就是最好的助手。

在以前,我们在程序拍错时使用的是sout获得debug

现在,我们就能够使用日志来进行排错了

日志_MyBatis中文网

日志我们只需要了解两种:STDOUT_LOGGINGLOG4J

STDOUT_LOGGING的使用

STDOUT_LOGGING 的使用十分方便 只需要在核心配置文件(mybatis-config.xml)中配置settings就行

1
2
3
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

配置完就可以直接进行测试

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Class not found: org.jboss.vfs.VFS
JBoss 6 VFS API is not available in this environment.
Class not found: org.jboss.vfs.VirtualFile
VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
Using VFS adapter org.apache.ibatis.io.DefaultVFS
Find JAR URL: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/pojo
Not a JAR: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/pojo
Reader entry: student.class
Listing file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/pojo
Find JAR URL: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/pojo/student.class
Not a JAR: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/pojo/student.class
Reader entry: ���� < :
Checking to see if class com.dwx.pojo.student matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Find JAR URL: file:/E:/java/MyBatisStudy/Mybatis03/target/test-classes/com/dwx/mapper
Not a JAR: file:/E:/java/MyBatisStudy/Mybatis03/target/test-classes/com/dwx/mapper
Reader entry: test.class
Listing file:/E:/java/MyBatisStudy/Mybatis03/target/test-classes/com/dwx/mapper
Find JAR URL: file:/E:/java/MyBatisStudy/Mybatis03/target/test-classes/com/dwx/mapper/test.class
Not a JAR: file:/E:/java/MyBatisStudy/Mybatis03/target/test-classes/com/dwx/mapper/test.class
Reader entry: ���� < U
Find JAR URL: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/mapper
Not a JAR: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/mapper
Reader entry: StudentMapper.class
Reader entry: StudentMapper.xml
Listing file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/mapper
Find JAR URL: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/mapper/StudentMapper.class
Not a JAR: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/mapper/StudentMapper.class
Reader entry: ���� <   com/dwx/mapper/StudentMapper  java/lang/Object getStudentList ()Ljava/util/List; Signature *()Ljava/util/List<Lcom/dwx/pojo/student;>;
Find JAR URL: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/mapper/StudentMapper.xml
Not a JAR: file:/E:/java/MyBatisStudy/Mybatis03/target/classes/com/dwx/mapper/StudentMapper.xml
Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
Checking to see if class com.dwx.mapper.test matches criteria [is assignable to Object]
Checking to see if class com.dwx.mapper.StudentMapper matches criteria [is assignable to Object]
Opening JDBC Connection
Created connection 473153915.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1c33c17b]
==> Preparing: select * from mybatis.student
==> Parameters:
<== Columns: id, name, password
<== Row: 1, 张三, welcome
<== Row: 2, 李四, welcome
<== Row: 4, 王五, 123456
<== Row: 17, dwx, welcome
<== Row: 100, 李一百, welcome
<== Total: 5
student{id=1, name='张三', pwd='welcome'}
student{id=2, name='李四', pwd='welcome'}
student{id=4, name='王五', pwd='123456'}
student{id=17, name='dwx', pwd='welcome'}
student{id=100, name='李一百', pwd='welcome'}

Process finished with exit code 0

这种日志的特点在于:

  • 使用简单 能实现基本功能
  • 只在控制台输出 不可以DIy 不可以控制输出格式

LOG4J的使用

LOG4J应该是使用更加广泛地,但是他的使用需要进行配置,使用起来有点麻烦

LOG4J的使用步骤:

  • 导入LOG4J的依赖
  • 在resource文件下新增一个配置文件(log4j.properties)
  • 编写log4j配置文件(网上搜 不用记)(会在博客中写的)
  • 设置log4j为日志的实现
  • 使用log4j

导入LOG4J的依赖

1
2
3
4
5
6
7
8
<dependencies>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

在子程序的pom.xml文件中配置

在resource文件下新增一个配置文件(log4j.properties)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
# priority  :debug<info<warn<error
#you cannot specify every priority with different file for log4j
log4j.rootLogger=debug,stdout,info,debug,warn,error

#console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= [%d{yyyy-MM-dd HH:mm:ss a}]:%p %l%m%n
#info log
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.info.File=./logs/log.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#debug log
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.debug.File=./logs/log.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#warn log
log4j.logger.warn=warn
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.warn.File=./logs/log.log
log4j.appender.warn.Append=true
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#error
log4j.logger.error=error
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.error.File =./logs/log.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n

设置log4j为日志的实现

在核心配置文件中写

1
2
3
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>

使用log4j

使用log4j有两种使用办法:

  • 直接使用
  • 编写使用方法

直接使用

在进行过以上的操作时,直接在操作类中写Logger logger = Logger.getLogger("测试方法类.class")

直接使用即可

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
package com.dwx.mapper;

import com.dwx.pojo.student;
import com.dwx.untils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.List;

public class test {
//注意 此处导包要导Apache的包
Logger logger = Logger.getLogger(test.class);

@Test
public void getStudentListTest(){
//首先需要获取到 mapper
//以下的操作都是固定的 不需要进行任何改变
MyBatisUtils myBatisUtils = new MyBatisUtils();
SqlSession sqlSession = myBatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<student> studentList = mapper.getStudentList();
for (student student : studentList) {
System.out.println(student.toString());
}

}
}

注意:创建Logger 导包是一定要导Apache log4j的包

在运行后,会产生一个logs文件,里边有log日志

文件的产生

文件中的内容和在控制台中输出的内容一致

文件中的内容

使用测试方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.dwx.mapper;

import com.dwx.pojo.student;
import com.dwx.untils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.List;

public class test {
//注意 此处导包要导Apache的包
Logger logger = Logger.getLogger(test.class);
@Test
public void log4jTest(){
logger.info("###################infor#################");
logger.debug("##################debug#################");
logger.error("##################error##################");
}
}

这样使用的好处是,在log文件中只会出现我们设置的这几个数据

控制台中的输出

1
2
3
4
com.dwx.mapper.test,log4jTest
[2022-01-11 21:01:04 下午]:INFO com.dwx.mapper.test.log4jTest(test.java:30)###################infor#################
[2022-01-11 21:01:04 下午]:DEBUG com.dwx.mapper.test.log4jTest(test.java:31)##################debug#################
[2022-01-11 21:01:04 下午]:ERROR com.dwx.mapper.test.log4jTest(test.java:32)##################error##################

log.log中的输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2022-01-11 21:01:04 下午 [Thread: main][ Class:com.dwx.mapper.test >> Method: com.dwx.mapper.test.log4jTest(test.java:30) ]
INFO:###################infor#################
2022-01-11 21:01:04 下午 [Thread: main][ Class:com.dwx.mapper.test >> Method: com.dwx.mapper.test.log4jTest(test.java:30) ]
INFO:###################infor#################
2022-01-11 21:01:04 下午 [Thread: main][ Class:com.dwx.mapper.test >> Method: com.dwx.mapper.test.log4jTest(test.java:31) ]
DEBUG:##################debug#################
2022-01-11 21:01:04 下午 [Thread: main][ Class:com.dwx.mapper.test >> Method: com.dwx.mapper.test.log4jTest(test.java:32) ]
ERROR:##################error##################
2022-01-11 21:01:04 下午 [Thread: main][ Class:com.dwx.mapper.test >> Method: com.dwx.mapper.test.log4jTest(test.java:32) ]
ERROR:##################error##################
2022-01-11 21:01:04 下午 [Thread: main][ Class:com.dwx.mapper.test >> Method: com.dwx.mapper.test.log4jTest(test.java:32) ]
ERROR:##################error##################
2022-01-11 21:01:04 下午 [Thread: main][ Class:com.dwx.mapper.test >> Method: com.dwx.mapper.test.log4jTest(test.java:32) ]
ERROR:##################error##################

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信