Spring log4j.xml

Ibatissql语句、统计url与controller之间的映射关系统计日志

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

	<!-- Appenders -->
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p: %c - %m%n" />
		</layout>
	</appender>

	<appender name="IBatis" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
		</layout>
		<!--限制输出级别--> 
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="LevelMin" value="DEBUG" />
			<param name="LevelMax" value="DEBUG" />
		</filter>
	</appender>
	
	<appender name="UrlControllerMapping" class="org.apache.log4j.FileAppender">
		<param name="file" value="d:/TestUrlControllerMapping.log" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p: %c - %m%n" />
		</layout>
	</appender>
	
	<logger name="UrlControllerMappingStatistics" additivity="true">
		<level value="INFO" />
		<appender-ref ref="UrlControllerMapping" />
	</logger>

	<logger name="cn.org.fds" additivity="true">
		<level value="DEBUG" />
	</logger>

	<logger name="com.ibatis" additivity="true">
		<level value="DEBUG" />
	</logger>

	<logger name="java.sql.Connection" additivity="true">
		<level value="DEBUG" />
	</logger>

	<logger name="java.sql.Statement" additivity="true">
		<level value="DEBUG" />
	</logger>

	<logger name="java.sql.PreparedStatement" additivity="true">
		<level value="DEBUG" />
		<appender-ref ref="IBatis" />
	</logger>

	<logger name="java.sql.ResultSet" additivity="true">
		<level value="DEBUG" />
		<appender-ref ref="IBatis" />
	</logger>
	<!-- 3rdparty Loggers -->
	<logger name="org.springframework.core">
		<level value="info" />
	</logger>

	<logger name="org.springframework.beans">
		<level value="info" />
	</logger>

	<logger name="org.springframework.context">
		<level value="info" />
	</logger>

	<logger name="org.springframework.web">
		<level value="info" />
	</logger>

	<!-- Root Logger -->
	<root>
		<priority value="warn" />
		<appender-ref ref="console" />
	</root>

</log4j:configuration>

在java中直接用logger

package edu.gd.commons;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;

/**
 * url与controller之间的映射关系统计
 * 
 * 
 * 
 */
@Component
public class UrlControllerMappingStatistics implements BeanFactoryAware,
		InitializingBean {

	private static final Logger logger = Logger
			.getLogger("UrlControllerMappingStatistics");

	private BeanFactory beanFactory;


	public void afterPropertiesSet() throws Exception {
		DefaultAnnotationHandlerMapping handlerMapping = beanFactory
				.getBean(DefaultAnnotationHandlerMapping.class);
		Map<String, Object> handlerMap = handlerMapping.getHandlerMap();
		Collection<Object> controllers = handlerMap.values();
		Set<Object> controllerSet = new HashSet<Object>(controllers);
		for (Object obj : controllerSet) {
			Class<?> controllerClass = obj.getClass();
			Method[] methods = controllerClass.getMethods();
			for (Method m : methods) {
				RequestMapping rm = m.getAnnotation(RequestMapping.class);
				if (rm != null) {
					logger.info("URL = " + Arrays.toString(rm.value())
							+ ", ControllerClass = " + obj.getClass()
							+ ", Method = " + m.getName());
				}
			}
		}

	}

	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

	
}

相关推荐