This article mainly introduces a detailed explanation of the logs combined with log4j and slf4j under the SSM framework, and shares them with you. The details are as follows:
First add log4j and slf4j jar packages
<!-- Log processing<!-- slf4j log package--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <!-- Introducing log package--> <dependency> <groupId>log4j</groupId> --> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency>
Mybatis' built-in log factory provides logging functions. The specific log implementation has the following tools:
1. Which log implementation tool to choose is determined by MyBatis' built-in log factory. It will use the first one found (find in the order listed above). If none of them are found, the logging function will be disabled.
Many application servers already contain Commons Logging, such as Tomcat and WebShpere, so MyBatis will implement it as a specific log. It is very important to remember this. This will mean that in an environment such as WebSphere - WebSphere provides a private implementation of Commons Logging, and your Log4J configuration will be ignored. This approach is inevitably sad. How can MyBatis ignore your configuration? In fact, because Commons Logging already exists, Log4J is naturally ignored in order of priority! However, if your application is deployed in an environment containing Commons Logging and you want to use other logging frameworks, you can select a different log implementation by adding a setting (config) to the MyBatis configuration file mybatis-config.xml.
<?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> <!-- Used to output log4j:LOG4J SLF4J --> <settings> <setting name="logImpl" value="SLF4J"/> </settings> <!-- This file can be an empty file, but it must have this file --> <!-- The location of plugins in the configuration file must meet the requirements, otherwise an error will be reported in the following order: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <!-- com.github.pagehelper is the package name where the PageHelper class is located --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- This parameter can be set without setting in versions 4.0.0------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ When set to true, the first parameter offset of RowBounds will be used as pageNum page number --> <!-- The effect is the same as the pageNum in startPage --> <property name="offsetAsPageNum" value="true" /> <!-- This parameter defaults to false --> <!-- When set to true, count query will be performed using RowBounds pagination --> <property name="rowBoundsWithCount" value="true" /> <!-- When set to true, if pageSize=0 or RowBounds.limit = 0, all results will be queryed --> <!-- (equivalent to the fact that the page query is not executed, but the result is still Page type) --> <property name="pageSizeZero" value="true" /> <!-- Version 3.3.0 is available - Paging parameter rationalization, false is disabled by default --> <!-- When rationalization is enabled, if pageNum<1 will query the first page, if pageNum>pages will query the last page --> <!-- When rationalization is disabled, if pageNum<1 or pageNum>pages will return empty data --><!-- <property name="reasonable" value="false" /> --> <!-- Version 3.5.0 is available - In order to support the startPage(Object params) method --> <!-- Added a `params` parameter to configure parameter mapping to get values from Map or ServletRequest --> <!-- You can configure pageNum, pageSize, count, pageSizeZero, reasonable, orderBy, and use the default value for mappings--> <!-- If you do not understand this meaning, do not copy the configuration casually--><!-- <property name="params" value="pageNum=pageHelperStart;pageSize=pageHelperRows;" /> --> <!-- Support passing paging parameters through Mapper interface parameters--> <property name="supportMethodsArguments" value="false" /> <!-- Always always return PageInfo type, check check whether the return type is PageInfo, none returns Page --> <property name="returnPageInfo" value="none" /> </plugin> </plugins> </configuration>
The optional values of logImpl are: SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, NO_LOGGING or the fully qualified class name of the class that implements the interface org.apache.ibatis.logging.Log, and the constructor of this class needs to be a string (String type) as the parameter. (You can refer to the implementation of org.apache.ibatis.logging.slf4j.Slf4jImpl.java)
You call a method as needed:
org.apache.ibatis.logging.LogFactory.useSlf4jLogging(); org.apache.ibatis.logging.LogFactory.useLog4JLogging(); org.apache.ibatis.logging.LogFactory.useJdkLogging(); org.apache.ibatis.logging.LogFactory.useJdkLogging(); org.apache.ibatis.logging.LogFactory.useCommonsLogging(); org.apache.ibatis.logging.LogFactory.useStdOutLogging();
If you do need to call one of the above methods, call it before all other MyBatis methods. In addition, it is only meaningful to call the corresponding method under the premise that it exists in the corresponding log implementation, otherwise MyBatis will be ignored. If Log4J does not exist in your environment, but you call the corresponding method, MyBatis will ignore this call and instead search the log implementation in the default search order.
2. Configure web.xml
<!-- Load log4j --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
3. Just create a file named log4j.properties in the application classpath, the specific content of the file is as follows:
log4j.rootLogger=INFO,console,dailyFile# TODO Please add it when publishing to Alibaba Cloud. In addition, the console does not output (only output warning or error information)#INFO,console,dailyFilelog4j.logger.org.mybatis =DEBUGlog4j.logger.com.itzixi.mapper=DEBUGlog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.encoding=UTF-8log4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SS} [%t] [%l] - [%p] %m%n#mybatis displays SQL statement log configuration#log4j.logger.org.mybatis=DEBUG#log4j.logger.com.itzixi.mapper=DEBUG # Regularly scroll the log files and generate logs every day log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.dailyFile.encoding=UTF-8log4j.appender.dailyFile.Threshold=INFO# TODO Local log address, for the official environment, please be sure to switch to Alibaba Cloud address #log4j.appender.dailyFile.File=C:/logs/itzixi-web/log.log4jlog4j.appender.dailyFile.File=/itzixi-disk1/logs/itzixi-web/log.log4jlog4j.appender.dailyFile.DatePattern='.'yyyy-MM-ddlog4j.appender.dailyFile.layout=org.apache.log4j.PatternLayoutlog4j.appender.dailyFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SS} [%t] [%l] - [%p] %m%n##Show SQL statement part#log4j.logger.com.ibatis=DEBUG#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG#log4j.logger.com .ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG#log4j.logger.java.sql.Connection=DEBUG#log4j.logger.java.sql.Statement=DEBUG#log4j.logger.java.sql.PreparedStatement=DEBUGAs shown in the figure, when we enter debug, the SQL statement is printed.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.