JavaWinner 2020-05-04
系统日志是记录系统中硬件、软件和系统问题的信息,同时还可以监视系统中发生的事件。
用户可以通过它来检查错误发生的原因,或者寻找受到攻击时攻击者留下的痕迹。
【我感觉是一样的】
我们过往所采哟的办法:
SOUT打印
DEBUG调试
现在,我们使用日志工厂
在上回阅读官方文档指北时,可以看到这样一个东西
我们所要知道和掌握主要是
LOG4J & STDOUT_LOGING
第二个Mybatis自带提供的
Mybatis自带的标准输出日志,只需要在核心配置中设置即可
1、在核心配置文件设置setting标签
我们可以不需要打印了
每次执行,都会打印日志记录
我们可以清晰的看到程序具体是怎么执行的
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:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo Reader entry: User.class Listing file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo Find JAR URL: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo/User.class Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo/User.class Reader entry: ???? 1 _ H I J K Checking to see if class cn.dai.pojo.User 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:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper Reader entry: UserMapper.class Reader entry: UserMapper.xml Listing file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper Find JAR URL: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.class Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.class Reader entry: ???? 1 getUserList ()Ljava/util/List; Signature &()Ljava/util/List<Lcn/dai/pojo/User;>; getUserById (I)Lcn/dai/pojo/User; addUser (Lcn/dai/pojo/User;)I updateUserById deleteUserById (I)I addUserByMap (Ljava/util/Map;)I 8(Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)I getUserLike $(Ljava/lang/String;)Ljava/util/List; 8(Ljava/lang/String;)Ljava/util/List<Lcn/dai/pojo/User;>; Find JAR URL: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.xml Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.xml Reader entry: <?xml version="1.0" encoding="UTF-8" ?> Checking to see if class cn.dai.mapper.UserMapper matches criteria [is assignable to Object] Opening JDBC Connection Created connection 689745064. ==> Preparing: /* 这里填写我们的SQL语句 */ SELECT * FROM user; ==> Parameters: <== Columns: user_id, user_name, user_password <== Row: 1, 杰哥, 123456 <== Row: 2, 阿伟, 123456 <== Row: 3, 空条承太郎, 123456 <== Row: 4, 乔鲁诺乔巴纳, 123456 <== Row: 5, 迪奥布兰多, 123456 <== Row: 6, 乔瑟夫乔斯达, 123456 <== Row: 7, 乔纳森乔斯达, 123456 <== Row: 8, 丽萨丽萨, 654321 <== Row: 9, 东方仗助, 654321 <== Row: 10, 东方定助, 654321 <== Row: 11, 花京院典明, 654321 <== Row: 12, 波鲁纳雷夫, 654321 <== Row: 13, 吉良吉影, 654321 <== Row: 14, 布加拉提, 654321 <== Row: 15, 葛德米斯达, 131313 <== Row: 18, 哈吉咩, 335577 <== Total: 16 User(user_id=1, user_name=杰哥, user_password=123456) User(user_id=2, user_name=阿伟, user_password=123456) User(user_id=3, user_name=空条承太郎, user_password=123456) User(user_id=4, user_name=乔鲁诺乔巴纳, user_password=123456) User(user_id=5, user_name=迪奥布兰多, user_password=123456) User(user_id=6, user_name=乔瑟夫乔斯达, user_password=123456) User(user_id=7, user_name=乔纳森乔斯达, user_password=123456) User(user_id=8, user_name=丽萨丽萨, user_password=654321) User(user_id=9, user_name=东方仗助, user_password=654321) User(user_id=10, user_name=东方定助, user_password=654321) User(user_id=11, user_name=花京院典明, user_password=654321) User(user_id=12, user_name=波鲁纳雷夫, user_password=654321) User(user_id=13, user_name=吉良吉影, user_password=654321) User(user_id=14, user_name=布加拉提, user_password=654321) User(user_id=15, user_name=葛德米斯达, user_password=131313) User(user_id=18, user_name=哈吉咩, user_password=335577) Closing JDBC Connection [] Returned connection 689745064 to pool. Process finished with exit code 0
1、先设置日志输出为LOG4J
2、依赖导入
Maven坐标
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
3、配置LOG4J的配置文件
src\main\resources\log4j.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/l4j.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
然后在需要使用日志的类中设置这个属性,
我们进行的一个简单的测试
import cn.dai.mapper.UserMapper; import cn.dai.pojo.User; import cn.dai.util.MybatisUtil; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author ArkD42 * @file Mybatis * @create 2020 - 05 - 04 - 8:33 */ public class SqlSessionTest { private static Logger logger = Logger.getLogger(SqlSessionTest.class); @Test public void log4jTest(){ logger.info("----------普通信息打印级别------------"); logger.debug("----------调试打印级别------------"); logger.error("----------错误查看打印级别------------"); } }
测试结果
在我们配置文件信息中的输出文件路径,现在也生成了
这里没有装log文件的插件支持,IDEA提示我们是否安装【看自己喜好】
在这里我们使用之前的测试
只需要添加一个即可
@Test public void sqlSessionTest(){ SqlSession sqlSession = MybatisUtil.getSqlSession(true); logger.info("开始输出"); System.out.println(sqlSession); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); for (User user: userList) { System.out.println(user); } sqlSession.close(); }
打印结果
[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:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo [org.apache.ibatis.io.DefaultVFS]-Reader entry: User.class [org.apache.ibatis.io.DefaultVFS]-Listing file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo [org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo/User.class [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/pojo/User.class [org.apache.ibatis.io.DefaultVFS]-Reader entry: ???? 1 _ H I J K [org.apache.ibatis.io.ResolverUtil]-Checking to see if class cn.dai.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.io.DefaultVFS]-Find JAR URL: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper [org.apache.ibatis.io.DefaultVFS]-Reader entry: UserMapper.class [org.apache.ibatis.io.DefaultVFS]-Reader entry: UserMapper.xml [org.apache.ibatis.io.DefaultVFS]-Listing file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper [org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.class [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.class [org.apache.ibatis.io.DefaultVFS]-Reader entry: ???? 1 getUserList ()Ljava/util/List; Signature &()Ljava/util/List<Lcn/dai/pojo/User;>; getUserById (I)Lcn/dai/pojo/User; addUser (Lcn/dai/pojo/User;)I updateUserById deleteUserById (I)I addUserByMap (Ljava/util/Map;)I 8(Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)I getUserLike $(Ljava/lang/String;)Ljava/util/List; 8(Ljava/lang/String;)Ljava/util/List<Lcn/dai/pojo/User;>; [org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.xml [org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/C:/Users/Administrator/IdeaProjects/Mybatis/Mybatis02%20-%20Configure/target/classes/cn/dai/mapper/UserMapper.xml [org.apache.ibatis.io.DefaultVFS]-Reader entry: <?xml version="1.0" encoding="UTF-8" ?> [org.apache.ibatis.io.ResolverUtil]-Checking to see if class cn.dai.mapper.UserMapper matches criteria [is assignable to Object] [SqlSessionTest]-开始输出 org.apache.ibatis.session.defa [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection [org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 532087022. [cn.dai.mapper.UserMapper.getUserList]-==> Preparing: /* 这里填写我们的SQL语句 */ SELECT * FROM user; [cn.dai.mapper.UserMapper.getUserList]-==> Parameters: [cn.dai.mapper.UserMapper.getUserList]-<== Total: 16 User(user_id=1, user_name=杰哥, user_password=123456) User(user_id=2, user_name=阿伟, user_password=123456) User(user_id=3, user_name=空条承太郎, user_password=123456) User(user_id=4, user_name=乔鲁诺乔巴纳, user_password=123456) User(user_id=5, user_name=迪奥布兰多, user_password=123456) User(user_id=6, user_name=乔瑟夫乔斯达, user_password=123456) User(user_id=7, user_name=乔纳森乔斯达, user_password=123456) User(user_id=8, user_name=丽萨丽萨, user_password=654321) User(user_id=9, user_name=东方仗助, user_password=654321) User(user_id=10, user_name=东方定助, user_password=654321) User(user_id=11, user_name=花京院典明, user_password=654321) User(user_id=12, user_name=波鲁纳雷夫, user_password=654321) User(user_id=13, user_name=吉良吉影, user_password=654321) User(user_id=14, user_name=布加拉提, user_password=654321) User(user_id=15, user_name=葛德米斯达, user_password=131313) User(user_id=18, user_name=哈吉咩, user_password=335577) [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [] [org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 532087022 to pool. Process finished with exit code 0