β

log4j指定的接口输出到指定的目标

xiaofancn 67 阅读

spring-mvc.xml

<!-- extends HandlerInterceptorAdapter -->
	<mvc:interceptors>
	<bean class="com.snailteam.game.controller.TrackHandler"></bean>
	</mvc:interceptors>
package com.snailteam.game.controller;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class TrackHandler extends HandlerInterceptorAdapter {
	protected final Log logger = LogFactory.getLog(getClass());
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		Map<String, String> header = new HashMap<String, String>();
		Enumeration<String> hs = request.getHeaderNames();
		while (hs.hasMoreElements()) {
			String h = hs.nextElement();
			header.put(h, request.getHeader(h));
		}
		logger.info(header);
		return super.preHandle(request, response, handler);
	}
}

log4j

# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL

# Global logging configuration
log4j.rootLogger=DEBUG,INFO,ERROR,stdout,file

## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} - [ %p ] %l - %m%n
log4j.appender.stdout.Encoding=UTF-8

#     append to file Info
log4j.appender.infoFile.Threshold=INFO
log4j.appender.infoFile=org.apache.log4j.RollingFileAppender
log4j.appender.infoFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.infoFile.Encoding=UTF-8
log4j.appender.infoFile.File=log/game.log
log4j.appender.infoFile.DatePattern='_'yyyyMMdd'.log'
log4j.appender.infoFile.layout=org.apache.log4j.PatternLayout
log4j.appender.infoFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} - [ %p ] %l - %m%n


#   append to file error
log4j.appender.errorFile.Threshold=ERROR
log4j.appender.errorFile=org.apache.log4j.RollingFileAppender
log4j.appender.errorFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorFile=org.apache.log4j.FileAppender
log4j.appender.errorFile.File=log/game-error.log
log4j.appender.errorFile.DatePattern='_'yyyyMMdd'.log'
log4j.appender.errorFile.Append=true
log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} - [ %p ] %l - %m%n 

#  com.snailteam.game.controller.TrackHandler >>> accessFile
log4j.appender.accessFile=org.apache.log4j.RollingFileAppender
log4j.appender.accessFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.accessFile.Encoding=UTF-8
log4j.appender.accessFile.File=log/accessFile.log
log4j.appender.accessFile.DatePattern='_'yyyyMMdd'.log'
log4j.appender.accessFile.layout=org.apache.log4j.PatternLayout
log4j.appender.accessFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} - [ %p ] %l - %m%n
log4j.additivity.com.snailteam.game.controller.TrackHandler=false
log4j.logger.com.snailteam.game.controller.TrackHandler=debug,accessFile


log4j.logger.org.springframework=debug
log4j.logger.com.snailteam.game=info, infoFile,errorFile

#log4j.logger.freemarker=debug


已有 0 人发表留言,猛击->> 这里 <<-参与讨论


ITeye推荐



作者:xiaofancn
喜欢挑战,对技术有些偏爱。
原文地址:log4j指定的接口输出到指定的目标, 感谢原作者分享。

发表评论