I. Introduction of related packages and dependencies
I use the method of slf4j+log4j to introduce pom files. There is another method of Commons logging + log4j. I will not talk about it here. You can refer to the materials you need.
<!--slf4j+log4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
II. Configure log4j.properties file
About the configuration of log4j.properties configuration file, I only post my own configuration here:
#//Be careful not to change the name with INFO, DEBUG, etc log4j.rootLogger=infoA,errorA,stdout,DEBUGA #Filtering out the extra logs under the spring framework log4j.category.org.springframework = WARN #Output to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.Threshold = debug log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c{1}:%L - %m%n #Output error to specified file log4j.logger.errorA=error log4j.appender.errorA=org.apache.log4j.DailyRollingFileAppender log4j.appender.errorA.layout=org.apache.log4j.PatternLayout log4j.appender.errorA.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.errorA.datePattern=yyyy-MM-dd'.log' log4j.appender.errorA.Threshold = error log4j.appender.errorA.append=true log4j.appender.error.File=d:/log/error.log #Output DEBUG information to the specified file log4j.logger.DEBUGA=DEBUG log4j.appender.DEBUGA=org.apache.log4j.DailyRollingFileAppender log4j.appender.DEBUGA.layout=org.apache.log4j.PatternLayout log4j.appender.DEBUGA.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.DEBUGA.datePattern=yyyy-MM-dd'.log' log4j.appender.DEBUGA.Threshold = DEBUG log4j.appender.DEBUGA.append=true log4j.appender.DEBUGA.File=d:/log/debug_log.log #Print sql statement log4j.logger.com.ibatis=DEBUG log4j.logger.java.sql.ResultSet=INFO 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=DEBUG
3. Configure mybats-config.xml
The main function of configuring mybats-config.xml is to combine it with log4j and output sql logs in the console and files. For options on LogImpl, please refer to other materials.
<settings> <setting name="logImpl" value="LOG4J" /> </settings>
4. Write information to the log manually in the code
private static final Logger logger = org.apache.log4j.Logger.getLogger(User.class); @RequestMapping("/login") public String login() { logger.debug("start printing log ================); logger.info("info================"); logger.debug("info================"); System.out.println("syso================"); return "login"; }
Launch the project, you can see that the log files and the console have printed the logs, and also filtered many logs under the unnecessary spring framework