log4j.xml web.xml配置

log4j.xml配置实现配置实现配置实现配置实现  

先写一个Servlet的listener,用来监听服务启动,并自动解析log4j.xml文件 代码如下:

 

 

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator; 
public class Log4jConfigListener implements ServletContextListener{  
 
	public static final String CONFIG_LOCATION_PARAM = "log4jConfigLocation";  
	public static final String XML_FILE_EXTENSION = ".xml";

	public void contextDestroyed(ServletContextEvent event) {  
		// TODO Auto-generated method stub  
		LogManager.shutdown();  
	}

	public void contextInitialized(ServletContextEvent event) {  
		// TODO Auto-generated method stub  
		String location = event.getServletContext().getInitParameter(CONFIG_LOCATION_PARAM);  		if (location != null) {    
			if (!location.startsWith("/")) {    
				location = "/" + location;    
			}  
			location = event.getServletContext().getRealPath(location);

			//如果是xml结尾就用DOM解析,否则就用properties解析    
			if (location.toLowerCase().endsWith(XML_FILE_EXTENSION)) {  
				DOMConfigurator.configure(location);    
			}else {    
				PropertyConfigurator.configure(location);    
			}      
		}  
	}
}
 

 

 

再写个util类,用来生成所要的不同类型的日志 

代码如下:

 

 

package com.zhaopin.util;
import org.apache.log4j.Logger;

public class Log4jUtil {    

	public static Logger getSQLLogger(){  
		return Logger.getLogger("sql");  
	}    

	public static Logger getBusinessLogger(){  
		return Logger.getLogger("business");  
	}    

	public static Logger getSimpleErrorLogger(){  
		return Logger.getLogger("simpleError");  
	}    

	public static Logger getNormalErrorLogger(){  
		return Logger.getLogger("normalError");  
	}
}  
 

 

 

下面开始写配置文件,先要在web.xml下添加信息

 

下面开始写配置文件,先要在web.xml下添加信息

 

<context-param>  
	<param-name>log4jConfigLocation</param-name>  
	<param-value>/WEB-INF/Log4j.xml</param-value>  
</context-param>

<!-- 加载log4j配置文件 -->  
<listener>  
	<listener-class>Log4jConfigListener</listener-class>  
</listener>  
 

 

最重要的log4j.xml的配置信息如下:

 

 

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">  
	<param name="target" value="System.out"/> 
	<layout class="org.apache.log4j.PatternLayout">    
		<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] (%C:%L) - %m%n"/>  
	</layout>  
</appender>

<!-- SQL类日志 -->  
<appender name="sql" class="org.apache.log4j.DailyRollingFileAppender">  
	<param name="File" value="/usr/local/tomcat/logs/testlog4j.sql.log"/>  
	<param name="Append" value="true"/>
	<!-- name="DatePattern" 配置这条信息是让日志每天自动生成,名字尾部追加日期格式, 第一天生成的日志需第二天才能看到log 可以通过更改系统时间来看日志的输出 -->  
	<param   name="DatePattern"   value="'.'yyyy-MM-dd"/>  
	<layout class="org.apache.log4j.PatternLayout">    
		<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] (%C:%L) - %m%n"/>  
	</layout>  
</appender>    

<!-- 业务类日志 -->  
<appender name="business" class="org.apache.log4j.DailyRollingFileAppender">  
	<param name="File" value="/usr/local/tomcat/logs/testlog4j.business.log"/>  
	<param name="Append" value="true"/>   <param   name="DatePattern"   value="'.'yyyy-MM-dd"/>  
	<layout class="org.apache.log4j.PatternLayout">    
		<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] (%C:%L) - %m%n"/>  
	</layout>  
</appender>    

<!-- 不带执行轨迹错误日志 -->  
<appender name="simpleError" class="org.apache.log4j.DailyRollingFileAppender">  
	<param name="File" value="/usr/local/tomcat/logs/testlog4j.error.simple.log"/>  
	<param name="Append" value="true"/>  
	<param   name="DatePattern"   value="'.'yyyy-MM-dd"/>  
	<layout class="org.apache.log4j.PatternLayout">    
		<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] (%C:%L) - %m%n"/>  
	</layout> 
</appender>    

<!-- 带执行轨迹错误日志 -->  
<appender name="normalError" class="org.apache.log4j.DailyRollingFileAppender">  
	<param name="File" value="/usr/local/tomcat/logs/testlog4j.error.normal.log"/>  
	<param name="Append" value="true"/>  
	<param   name="DatePattern"   value="'.'yyyy-MM-dd"/>  
	<layout class="org.apache.log4j.PatternLayout">    
		<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] (%C:%L) - %m%n"/>  
	</layout>  
</appender>          

<logger name="sql" additivity="true">  
	<priority value="info"/>  
	<appender-ref ref="sql"/>  
</logger>  

<logger name="business" additivity="true">  
	<priority value="info"/>  
	<appender-ref ref="simpleError"/>  
</logger>  

<logger name="simpleError" additivity="true">  
	<priority value="info"/>  
	<appender-ref ref="simpleError"/>  
</logger>  

<logger name="normalError" additivity="true">  
	<priority value="info"/>  
	<appender-ref ref="normalError"/>  
</logger>  

<root>            
	<appender-ref ref="STDOUT"/>            
	<priority value="info"/>      
</root>  

</log4j:configuration>
 

 

在程序中应用时需要编写如下代码: Log4jUtil.getBusinessLogger().info("message!");  

你可能感兴趣的:(log4j xml web)