@Pollux
2016-07-23T03:21:54.000000Z
字数 2695
阅读 975
web
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency>
<context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:/log4j/log4j.properties</param-value></context-param><context-param><param-name>log4jRefreshInterval</param-name><param-value>6000</param-value></context-param><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener>
根据/log4j/log4j.properties目录建立文件
# log4j.properties
这里一定要注意,路径的斜杠要写成\而不是\,如果不这样写,似乎无法输出!
# debug<info<warn<errorlog4j.rootLogger=debug,CONSOLE,FILE#log4j.debug=true# CONSOLE is set to be a ConsoleAppender using a PatternLayout.log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Threshold=debuglog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%d %p [%c] - <%m>%nlog4j.appender.FILE=org.apache.log4j.RollingFileAppenderlog4j.appender.FILE.File=D:\\log\\error.loglog4j.appender.FILE.MaxFileSize=10000KBlog4j.appender.FILE.MaxBackupIndex=1000log4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%nlog4j.logger.org.apache.ibatis=ERRORlog4j.logger.org.apache.ibatis.common.jdbc.SimpleDataSource=ERRORlog4j.logger.org.apache.ibatis.common.jdbc.ScriptRunner=ERRORlog4j.logger.org.apache.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERRORlog4j.logger.java.sql.Connection=ERRORlog4j.logger.java.sql.Statement=ERRORlog4j.logger.java.sql.PreparedStatement=ERRORlog4j.logger.org.mybatis.spring = ERRORlog4j.logger.org.springframework=ERROR
package com.pollux.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.pollux.pojo.user;import com.pollux.service.userservice;@Controllerpublic class Testlog4j {Logger logger = LoggerFactory.getLogger(Testlog4j.class);@Autowiredprivate userservice userservice;@ResponseBody@RequestMapping(value = "/sayhello")public String sayhello() {logger.debug("log4j success!");return "hello Pollux";}}
<!--2016-07-18 14:39:38,699 DEBUG [com.pollux.controller.Testlog4j] - <log4j success!>-->