SSM环境下配置log4j输出sql和异常到控制台和本地日志文件中
1.引入日志依赖包
1 <!--解决Spring使用slf4j输出日志与log4j冲突的问题--> 2 <dependency> 3 <groupId>org.slf4j</groupId> 4 <artifactId>slf4j-log4j12</artifactId> 5 <version>1.6.6</version> 6 </dependency> 7 <!-- log4j的包 --> 8 <dependency> 9 <groupId>org.slf4j</groupId> 10 <artifactId>slf4j-api</artifactId> 11 <version>1.6.6</version> 12 </dependency> 13 <dependency> 14 <groupId>log4j</groupId> 15 <artifactId>log4j</artifactId> 16 <version>1.2.16</version> 17 </dependency>
2.web.xml中的配置
<!-- log4j 配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> <!-- <param-value>/WEB-INF/log4j.xml</param-value> --> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
3.log4j.properties
log4j.rootLogger=INFO,Console,File
log4j.appender.CONSOLE= org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target= System.out
log4j.appender.CONSOLE.layout= org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = \r\n\u7EA7\u522B\:%p\r\n\u4FE1\u606F\:%m\r\n\u4F4D\u7F6E\:%l\r\n\u65F6\u95F4\:%d\r\n\u7C7B\u76EE\:%c\r\n\u7EBF\u7A0B\:%t\r\n
#cn.bs.nl.mapper映射目录路径
log4j.logger.cn.bs.nl.mapper=DEBUG
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm|\:ss}][%c]%m%n
参考链接:
http://blog.csdn.net/github_32658299/article/details/54288923
https://www.cnblogs.com/zyw-205520/p/5416412.html
https://www.cnblogs.com/wangzhuxing/p/7753420.html