1 Log4j配置说明
1.1 配置文件
Log4j可以通过java程序动态设置,该方式明显缺点是:如果需要修改日志输出级别等信息,则必须修改java文件,然后重新编译,很是麻烦;
log4j也可以通过配置文件的方式进行设置,目前支持两种格式的配置文件:
•xml文件
•properties文件(推荐)
下面是一个log4j配置文件的完整内容:
log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout
### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
### set package ###
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug
1.2 配置根Logger
根logger主要定义log4j支持的日志级别及输出目的地,其语法为:
log4j.rootLogger = [ level ] , appenderName, appenderName, …
其中,level 是日志记录的优先级,分为OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者自定义的级别。建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG。
appenderName指定日志信息输出到哪个地方,可同时指定多个输出目的地。
1.3 配置输出目的地Appender
Appender主要定义日志信息输出在什么位置,主要语法为:
log4j.appender.appenderName = classInfo
log4j.appender.appenderName.option1 = value1
…
log4j.appender.appenderName.optionN = valueN
Log4j提供的appender有以下几种: •org.apache.log4j.ConsoleAppender(控制台),
•org.apache.log4j.FileAppender(文件),
•org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件),
•org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
•org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
以ConsoleAppender为例,如:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
1.4 配置日志信息的格式Layout
Layout 负责格式化Appender的输出,其语法为:
log4j.appender.appenderName.layout = classInfo
log4j.appender.appenderName.layout.option1 = value1
…
log4j.appender.appenderName.layout.optionN = valueN
其中,Log4j提供的layout有以下几种: •org.apache.log4j.HTMLLayout(以HTML表格形式布局),
•org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
•org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串)
•org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
1.5 设置package输出级别
可以设置不同package的日志输出级别,语法为:
log4j.logger.packageName=level
其中,packageName为实际的包名,level为日志级别,例如:
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug
2 Log4j与J2ee结合2.1 使用spring架构
Spring真是不错,替我们做了很多事情,如果系统使用了spring框架,则要集成log4j就很简单了,主要分为3个步骤,如下:
2.1.1 定义log4j配置文件
log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout
### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
### log to file ###
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug
2.1.2 定义监听器
监听器需要定义在web.xml,主要包括:定义log4j配置文件目录、log4j监听器,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--由Spring载入的Log4j配置文件位置-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext*.xml
</param-value>
</context-param>
<!--Spring log4j Config loader-->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>InitiaServlet</servlet-name>
<servlet-class>chb.test.web.InitiaServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.1.3 测试类
package com.dheaven.mip.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
public class InitiaServlet extends HttpServlet {
protected Logger log = Logger.getLogger(InitiaServlet.class);
private static final long serialVersionUID = 8550329576989690578L;
/**
* Constructor of the object.
*/
public InitiaServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
log.debug("服务器启动了,log4j开始工作了");
}
}
2.2不使用spring架构
如果系统没有使用spring,我们以servlet为例,很简单,大家按照步骤执行即可,只贴代码,不说废话。2.2.1 定义log4j配置文件
放在web工程的WEB-INF目录下,内容如:
log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout
### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
### set package ###
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.com.dheaven=debug
2.2.2 创建log4j初始化类
package com.dheaven.mip.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
public class InitLog4j extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
prefix = prefix.replace("//", "/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file is not set, then no point in trying
if (file != null) {
PropertyConfigurator.configure(prefix + file);
}
}
}
2.2.3 在Web.xml中定义初始化类
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>chb.test.web.InitLog4j</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
2.2.4 测试类
package chb.test.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
public class InitiaServlet extends HttpServlet {
protected Logger log = Logger.getLogger(InitiaServlet.class);
private static final long serialVersionUID = 8550329576989690578L;
/**
* Constructor of the object.
*/
public InitiaServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
log.debug("服务器启动了,log4j开始工作了");
}
}