配置过程:
导入的jar包:
slf4j-api-1.7.6.jar
slf4j-log4j12-1.7.6.jar
log4j.properties
log4j.rootLogger= DEBUG,stdout,D1 log4j.rootLogger=INFO,stdout,E log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.Threshold=INFO log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern [email protected] lyx:-%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] -[%c] -%m%n log4j.appender.D1 = org.apache.log4j.DailyRollingFileAppender log4j.appender.D1.File = ${springLog.root}/WEB-INF/logs/error.log log4j.appender.D1.Append = true log4j.appender.D1.Threshold = ERROR log4j.appender.D1.layout = org.apache.log4j.PatternLayout log4j.appender.D1.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File = ${springLog.root}/WEB-INF/logs/error1.log log4j.appender.E.Append = true log4j.appender.E.Threshold = INFO log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
web.xml
<!-- log4j日志配置 --> <!-- 路径变量值(是服务器的路径) --> <context-param> <param-name>webAppRootKey</param-name> <param-value>springLog.root</param-value> </context-param> <!-- log4j配置文件 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath: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>
java代码:
package com.controller; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.ResponseBody; import com.service.UserService; /** * @author lyx * * 2015-8-19上午8:53:52 * *com.controller.UserController * TODO */ @Controller @RequestMapping("/user") public class UserController { //private static final Logger log =Logger.getLogger(UserController.class); private static final org.slf4j.Logger logger =org.slf4j.LoggerFactory.getLogger(UserController.class); @Autowired private UserService service; @RequestMapping("/queryAllInfo") @ResponseBody public void queryAllInfo(HttpServletRequest request,HttpServletResponse response) { //当前方法名 String currentMethod=Thread.currentThread().getStackTrace()[1].getMethodName(); logger.info("currentThread call currentMethod is:"+currentMethod); logger.debug("debug"); logger.error("error"); } }
内容解释
输出方式appender一般有5种:
org.apache.log4j.RollingFileAppender(滚动文件,自动记录最新日志)
org.apache.log4j.ConsoleAppender (控制台)
org.apache.log4j.FileAppender (文件)
org.apache.log4j.DailyRollingFileAppender (每天产生一个日志文件)
org.apache.log4j.WriterAppender (将日志信息以流格式发送到任意指定的地方)
(2). 日记记录的优先级priority,优先级由高到低分为
OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。
Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。
(3). 格式说明layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出):
%c 输出所属类的全名,可在修改为 %d{Num} ,Num类名输出的维(如:"org.apache.elathen.ClassName",%C{2}将输出elathen.ClassName)
%d 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}
%l 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数
%n 换行符
%m 输出代码指定信息,如info(“message”),输出message
%p 输出优先级,即 FATAL ,ERROR 等
%r 输出从启动到显示该log信息所耗费的毫秒数
%t 输出产生该日志事件的线程名
详细网站:
如何在log4j.properties文件中使用相对路径:http://blog.csdn.net/wang12/article/details/6395602
spring mvc中log4j的配置与使用:http://rockelixir.iteye.com/blog/1902352
配置Log4j(很详细): http://blog.csdn.net/azheng270/article/details/2173430
版权声明:本文为博主原创文章,未经博主允许不得转载。