当前环境:SPRINGMVC+mybatis+SPRING.
在项目中pom.xml加入log4j包的依赖,更新MAEVN
1 <!-- log4j日志文件管理包版本 --> 2 <properties> 3 <slf4j.version>1.7.7</slf4j.version> 4 <log4j.version>1.2.17</log4j.version> 5 </properties> 6 7 <!-- 日志文件管理包 --> 8 <dependency> 9 <groupId>log4j</groupId> 10 <artifactId>log4j</artifactId> 11 <version>${log4j.version}</version> 12 </dependency> 13 <dependency> 14 <groupId>org.slf4j</groupId> 15 <artifactId>slf4j-api</artifactId> 16 <version>${slf4j.version}</version> 17 </dependency> 18 <dependency> 19 <groupId>org.slf4j</groupId> 20 <artifactId>slf4j-log4j12</artifactId> 21 <version>${slf4j.version}</version> 22 </dependency>
写log4j配置文件---logback.properties---
1 #定义输出级别 2 log4j.rootLogger=DEBUG,Console,File 3 #日志输出方式:控制台输出 4 5 log4j.appender.Console=org.apache.log4j.ConsoleAppender 6 log4j.appender.Console.Target=System.out 7 log4j.appender.Console.Encoding=GBK 8 9 #可以灵活地指定布局模式 10 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 11 #log4j.appender.Console.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} -%p (%F:%L)- %m%n 12 #打印格式栗子:2017-08-11 15:36 -DEBUG (HttpServletBean.java:174)- Servlet 'mvc' configured successfully 13 log4j.appender.Console.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm} -%p (%F:%L)- %m%n 14 15 ### 打印SQL ### 16 #log4j.logger.com.ibatis=DEBUG 17 #log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG 18 #log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG 19 #log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG 20 log4j.logger.java.sql.Connection=DEBUG 21 log4j.logger.java.sql.Statement=DEBUG 22 log4j.logger.java.sql.PreparedStatement=DEBUG 23 #log4j.logger.java.sql.ResultSet=DEBUG
最后一步,在项目/WEB-INF下的web.xml文件中加入监听配置,顺序需注意,listener必须放在后面
1 <!-- 指定监听器加载的log4j配置文件 --> 2 <context-param> 3 <param-name>log4jConfigLocation</param-name> 4 <param-value>classpath:logback.properties</param-value> 5 </context-param> 6 7 <listener> 8 <listener-class> 9 org.springframework.web.util.Log4jConfigListener 10 </listener-class> 11 </listener>