发布于 2016-07-09 04:26:44 | 193 次阅读 | 评论: 0 | 来源: 网友投递
Apache Log4j log记录软件
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
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
建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG。
appenderName指定日志信息输出到哪个地方,可同时指定多个输出目的地。
1.3 配置输出目的地Appender
Appender主要定义日志信息输出在什么位置,主要语法为:
log4j.appender.appenderName = classInfo
log4j.appender.appenderName.option1 = value1
…
log4j.appender.appenderName.optionN = valueN
•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
log4j.appender.appenderName.layout = classInfo
log4j.appender.appenderName.layout.option1 = value1
…
log4j.appender.appenderName.layout.optionN = valueN
•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.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
<?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>
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.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
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);
}
}
}
<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>
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开始工作了");
}
}