l配置实现
先写一个Servlet的listener,用来监听服务启动,并自动解析l文件
代码如下:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import org.l.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 = ServletContext().getInitParameter(CONFIG_LOCATION_PARAM);
if (location != null) {
if (!location.startsWith("/")) {
location = "/" + location;
}
location = ServletContext().getRealPath(location);
//如果是xml结尾就用DOM解析,否则就用properties解析
if (LowerCase().endsWith(XML_FILE_EXTENSION)) {
figure(location);
}
else {
figure(location);
}
}
}
}
再写个util类,用来生成所要的不同类型的日志
代码如下:
package com.zhaopin.util;
import org.apache.log4j.Logger;
public class Log4jUtil {
public static Logger getSQLLogger(){
Logger("sql");
}
public static Logger getBusinessLogger(){
Logger("business");
}
public static Logger getSimpleErrorLogger(){
param name Logger("simpleError");
}
public static Logger getNormalErrorLogger(){
Logger("normalError");
}
}
下面开始写配置文件,先要在l下添加信息:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/l</param-value>
</context-param>
<listener>
<listener-class>Log4jConfigListener</listener-class>
</listener>
最重要的l的配置信息如下:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="/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>
<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/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/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">
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论