13.1 introduction
In the process of program development, we often need a lot of program debugging and troubleshooting. In the beginning stage, we often use South and debug to debug the program. The output information of the console is not detailed enough most of the time, so the log factory was born. We can more easily view the background log and understand the details of program operation.
13.2 opening log
mybatis provides the settings tag in the core configuration file. The syntax is as follows:
<settings> <setting name="" value=""/> </settings>
Tip: name is the setting name, and value is the candidate value. The log factory setting name is logImpl, and the candidate values are as follows:;
Candidate value | remarks |
---|---|
SLF4J | understand |
LOG4J | a key |
LOG4J2 | understand |
JDK_LOGGING | understand |
COMMONS_LOGGING | understand |
STDOUT_LOGGING | master |
NO_LOGGING | understand |
13.3 STDOUT_ Use of logging
Core file configuration:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"/> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> <!--Open standard log here--> <setting name="cacheEnabled" value="true"/> </settings> <typeAliases> <package name="com.yun.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${pwd}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/yun/dao/UserMapper.xml"/> <mapper class="com.yun.test.TestMapper"/> <!-- <package name="com.yun.dao"/>--> <mapper class="com.yun.dao.TeacherMapper"/> </mappers> </configuration>
Log output:
Opening JDBC Connection Created connection 2012846597. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@77f99a05] ==> Preparing: select * from user where id=? ==> Parameters: 2(Integer) <== Columns: id, name, gender, tid <== Row: 2, Li SiGe, 1, 1 <== Total: 1 User(id=2, name=Li SiGe, gender=1, tid=1) Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@77f99a05] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@77f99a05] Returned connection 2012846597 to pool. Cache Hit Ratio [com.yun.dao.UserMapper]: 0.5 User(id=2, name=Li SiGe, gender=1, tid=1) false Process finished with exit code 0
Tip: it's easy to open, but to use it is to view the details of program operation more clearly.
13.4 use of log4j
13.4. 1 Introduction
Log4j is a of Apache Open source In the project, by using Log4j, we can control the destination of log information delivery to console, files, GUI components, and even Socket interface Server, NT event recorder, UNIX Syslog daemon, etc; We can also control the output format of each log; By defining the level of each log information, we can control the log generation process in more detail. The most interesting thing is that these can be flexibly configured through a configuration file without modifying the application code. To put it bluntly, it is a highly free log.
13.4. 2 configuration
Maven dependent package:
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Configuration file log4j properties:
#The log information with the level of debug is output to console and file log4j.rootLogger=DEBUG,console,file #Console related output settings log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out 1og4j.appender.console.Threshold = DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = [%c]-%m%n #File output related settings org apache. log4j log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File = ./log/yun.log log4i.appender.file.MaxFileSize = 10mb log41.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 #Log output level log4j.logger.org.mybatis = DEBUG log4j.logger.java.sql = DEBUG 1og4j.logger.java.sql.Statement = DEBUG 1og4j.logger.java.sql.ResultSet = DEBUG 1og4j.logger.java.sql.PreparedStatement = DEBUG
Priority: the behavior of the Logger is hierarchical, as shown in the following table:
It is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or the level you define. Log4j recommends using only four levels. The priority from high to low is ERROR, WARN, INFO and DEBUG. Through the levels defined here, you can control the switch to the corresponding level of log information in the application. For example, if the INFO level is defined here, the log information of ALL DEBUG levels in the application will not be printed, that is, the logs of levels greater than or equal to will be output.
code:
import org.apache.log4j.Logger; public class UserMapperTest { static Logger logger= Logger.getLogger(UserMapperTest.class); @Test public void loggerTest(){ logger.info("Hello"); logger.debug("Hello"); } }
log4j is not used