log4j

注:该文转载于其他地方。

log4j使用简介

       最近在整理公司产品的日志输出规范,涉及log4j的使用介绍,就简单整理了一下。

 

1 Log4j配置说明

1.1 配置文件

    Log4j可以通过java程序动态设置,该方式明显缺点是:如果需要修改日志输出级别等信息,则必须修改java文件,然后重新编译,很是麻烦;

    log4j也可以通过配置文件的方式进行设置,目前支持两种格式的配置文件:

  • xml文件
  • properties文件(推荐)

下面是一个log4j配置文件的完整内容:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->log4j.rootCategory=INFO, stdout  

<!--[if !supportLists]-->2.  <!--[endif]-->log4j.rootLogger=info, stdout  

<!--[if !supportLists]-->3.  <!--[endif]-->   

<!--[if !supportLists]-->4.  <!--[endif]-->### stdout ###  

<!--[if !supportLists]-->5.  <!--[endif]-->log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

<!--[if !supportLists]-->6.  <!--[endif]-->log4j.appender.stdout.Target=System.out  

<!--[if !supportLists]-->7.  <!--[endif]-->log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  

<!--[if !supportLists]-->8.  <!--[endif]-->log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n  

<!--[if !supportLists]-->9.  <!--[endif]-->   

<!--[if !supportLists]-->10.<!--[endif]-->### set package ###  

<!--[if !supportLists]-->11.<!--[endif]-->log4j.logger.org.springframework=info  

<!--[if !supportLists]-->12.<!--[endif]-->log4j.logger.org.apache.catalina=info  

<!--[if !supportLists]-->13.<!--[endif]-->log4j.logger.org.apache.commons.digester.Digester=info  

<!--[if !supportLists]-->14.<!--[endif]-->log4j.logger.org.apache.catalina.startup.TldConfig=info  

<!--[if !supportLists]-->15.<!--[endif]-->log4j.logger.chb.test=debug  

 

 1.2 配置根Logger

logger主要定义log4j支持的日志级别及输出目的地,其语法为:

log4j.rootLogger = [ level ] , appenderName, appenderName, …

其中,level 是日志记录的优先级,分为OFFFATALERRORWARNINFODEBUGALL或者自定义的级别。

建议只使用四个级别,优先级从高到低分别是ERRORWARNINFODEBUG

appenderName指定日志信息输出到哪个地方,可同时指定多个输出目的地。

1.3 配置输出目的地Appender

Appender主要定义日志信息输出在什么位置,主要语法为:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->log4j.appender.appenderName = classInfo  

<!--[if !supportLists]-->2.  <!--[endif]-->log4j.appender.appenderName.option1 = value1  

<!--[if !supportLists]-->3.  <!--[endif]-->  …  

<!--[if !supportLists]-->4.  <!--[endif]-->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例,如:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

<!--[if !supportLists]-->2.  <!--[endif]-->log4j.appender.stdout.Target=System.out  

 

1.4 配置日志信息的格式Layout

  Layout 负责格式化Appender的输出,其语法为:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->log4j.appender.appenderName.layout = classInfo  

<!--[if !supportLists]-->2.  <!--[endif]-->log4j.appender.appenderName.layout.option1 = value1  

<!--[if !supportLists]-->3.  <!--[endif]-->…  

<!--[if !supportLists]-->4.  <!--[endif]-->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为日志级别,例如:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->log4j.logger.org.springframework=info  

<!--[if !supportLists]-->2.  <!--[endif]-->log4j.logger.org.apache.catalina=info  

<!--[if !supportLists]-->3.  <!--[endif]-->log4j.logger.org.apache.commons.digester.Digester=info  

<!--[if !supportLists]-->4.  <!--[endif]-->log4j.logger.org.apache.catalina.startup.TldConfig=info  

<!--[if !supportLists]-->5.  <!--[endif]-->log4j.logger.chb.test=debug  

 

2 Log4jJ2ee结合

2.1 使用spring架构

Spring真是不错,替我们做了很多事情,如果系统使用了spring框架,则要集成log4j就很简单了,主要分为3个步骤,如下:

2.1.1 定义log4j配置文件

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->log4j.rootCategory=INFO, stdout  

<!--[if !supportLists]-->2.  <!--[endif]-->log4j.rootLogger=info, stdout  

<!--[if !supportLists]-->3.  <!--[endif]-->   

<!--[if !supportLists]-->4.  <!--[endif]-->### stdout ###  

<!--[if !supportLists]-->5.  <!--[endif]-->log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

<!--[if !supportLists]-->6.  <!--[endif]-->log4j.appender.stdout.Target=System.out  

<!--[if !supportLists]-->7.  <!--[endif]-->log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  

<!--[if !supportLists]-->8.  <!--[endif]-->log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n  

<!--[if !supportLists]-->9.  <!--[endif]-->   

<!--[if !supportLists]-->10.<!--[endif]-->### log to file ###  

<!--[if !supportLists]-->11.<!--[endif]-->log4j.logger.org.springframework=info  

<!--[if !supportLists]-->12.<!--[endif]-->log4j.logger.org.apache.catalina=info  

<!--[if !supportLists]-->13.<!--[endif]-->log4j.logger.org.apache.commons.digester.Digester=info  

<!--[if !supportLists]-->14.<!--[endif]-->log4j.logger.org.apache.catalina.startup.TldConfig=info  

<!--[if !supportLists]-->15.<!--[endif]-->log4j.logger.chb.test=debug  

 

 2.1.2  定义监听器

监听器需要定义在web.xml,主要包括:定义log4j配置文件目录、log4j监听器,如下:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]--><?xml version="1.0" encoding="UTF-8"?>  

<!--[if !supportLists]-->2.  <!--[endif]--><web-app version="2.4"  

<!--[if !supportLists]-->3.  <!--[endif]-->    xmlns="http://java.sun.com/xml/ns/j2ee"  

<!--[if !supportLists]-->4.  <!--[endif]-->    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

<!--[if !supportLists]-->5.  <!--[endif]-->    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  

<!--[if !supportLists]-->6.  <!--[endif]-->    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  

<!--[if !supportLists]-->7.  <!--[endif]-->     

<!--[if !supportLists]-->8.  <!--[endif]-->    <!--Spring载入的Log4j配置文件位置-->  

<!--[if !supportLists]-->9.  <!--[endif]-->    <context-param>  

<!--[if !supportLists]-->10.<!--[endif]-->       <param-name>log4jConfigLocation</param-name>  

<!--[if !supportLists]-->11.<!--[endif]-->       <param-value>/WEB-INF/log4j.properties</param-value>  

<!--[if !supportLists]-->12.<!--[endif]-->    </context-param>  

<!--[if !supportLists]-->13.<!--[endif]-->     

<!--[if !supportLists]-->14.<!--[endif]-->    <context-param>  

<!--[if !supportLists]-->15.<!--[endif]-->       <param-name>contextConfigLocation</param-name>  

<!--[if !supportLists]-->16.<!--[endif]-->       <param-value>  

<!--[if !supportLists]-->17.<!--[endif]-->           /WEB-INF/classes/applicationContext*.xml  

<!--[if !supportLists]-->18.<!--[endif]-->       </param-value>  

<!--[if !supportLists]-->19.<!--[endif]-->    </context-param>  

<!--[if !supportLists]-->20.<!--[endif]-->     

<!--[if !supportLists]-->21.<!--[endif]-->    <!--Spring log4j Config loader-->  

<!--[if !supportLists]-->22.<!--[endif]-->    <listener>  

<!--[if !supportLists]-->23.<!--[endif]-->       <listener-class>  

<!--[if !supportLists]-->24.<!--[endif]-->           org.springframework.web.util.Log4jConfigListener  

<!--[if !supportLists]-->25.<!--[endif]-->       </listener-class>  

<!--[if !supportLists]-->26.<!--[endif]-->    </listener>  

<!--[if !supportLists]-->27.<!--[endif]-->     

<!--[if !supportLists]-->28.<!--[endif]-->    <listener>  

<!--[if !supportLists]-->29.<!--[endif]-->       <listener-class>  

<!--[if !supportLists]-->30.<!--[endif]-->           org.springframework.web.context.ContextLoaderListener  

<!--[if !supportLists]-->31.<!--[endif]-->       </listener-class>  

<!--[if !supportLists]-->32.<!--[endif]-->    </listener>  

<!--[if !supportLists]-->33.<!--[endif]-->     

<!--[if !supportLists]-->34.<!--[endif]-->    <servlet>  

<!--[if !supportLists]-->35.<!--[endif]-->       <servlet-name>InitiaServlet</servlet-name>  

<!--[if !supportLists]-->36.<!--[endif]-->    <servlet-class>chb.test.web.InitiaServlet</servlet-class>  

<!--[if !supportLists]-->37.<!--[endif]-->       <load-on-startup>1</load-on-startup>  

<!--[if !supportLists]-->38.<!--[endif]-->    </servlet>  

<!--[if !supportLists]-->39.<!--[endif]-->     

<!--[if !supportLists]-->40.<!--[endif]-->  <welcome-file-list>  

<!--[if !supportLists]-->41.<!--[endif]-->    <welcome-file>index.jsp</welcome-file>  

<!--[if !supportLists]-->42.<!--[endif]-->  </welcome-file-list>  

<!--[if !supportLists]-->43.<!--[endif]--></web-app>  

 

2.1.3  测试类

[java] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->package com.dheaven.mip.web;  

<!--[if !supportLists]-->2.  <!--[endif]-->   

<!--[if !supportLists]-->3.  <!--[endif]-->   

<!--[if !supportLists]-->4.  <!--[endif]-->import javax.servlet.ServletException;  

<!--[if !supportLists]-->5.  <!--[endif]-->import javax.servlet.http.HttpServlet;  

<!--[if !supportLists]-->6.  <!--[endif]-->   

<!--[if !supportLists]-->7.  <!--[endif]-->import org.apache.log4j.Logger;  

<!--[if !supportLists]-->8.  <!--[endif]-->   

<!--[if !supportLists]-->9.  <!--[endif]-->public class InitiaServlet extends HttpServlet {  

<!--[if !supportLists]-->10.<!--[endif]-->     

<!--[if !supportLists]-->11.<!--[endif]-->    protected Logger log = Logger.getLogger(InitiaServlet.class);  

<!--[if !supportLists]-->12.<!--[endif]-->   

<!--[if !supportLists]-->13.<!--[endif]-->    private static final long serialVersionUID = 8550329576989690578L;  

<!--[if !supportLists]-->14.<!--[endif]-->   

<!--[if !supportLists]-->15.<!--[endif]-->    /** 

<!--[if !supportLists]-->16.<!--[endif]-->     * Constructor of the object. 

<!--[if !supportLists]-->17.<!--[endif]-->     */  

<!--[if !supportLists]-->18.<!--[endif]-->    public InitiaServlet() {  

<!--[if !supportLists]-->19.<!--[endif]-->       super();  

<!--[if !supportLists]-->20.<!--[endif]-->    }  

<!--[if !supportLists]-->21.<!--[endif]-->   

<!--[if !supportLists]-->22.<!--[endif]-->    /** 

<!--[if !supportLists]-->23.<!--[endif]-->     * Destruction of the servlet. <br> 

<!--[if !supportLists]-->24.<!--[endif]-->     */  

<!--[if !supportLists]-->25.<!--[endif]-->    public void destroy() {  

<!--[if !supportLists]-->26.<!--[endif]-->       super.destroy();  

<!--[if !supportLists]-->27.<!--[endif]-->    }  

<!--[if !supportLists]-->28.<!--[endif]-->   

<!--[if !supportLists]-->29.<!--[endif]-->    /** 

<!--[if !supportLists]-->30.<!--[endif]-->     * Initialization of the servlet. <br> 

<!--[if !supportLists]-->31.<!--[endif]-->     * 

<!--[if !supportLists]-->32.<!--[endif]-->     * @throws ServletException if an error occure 

<!--[if !supportLists]-->33.<!--[endif]-->     */  

<!--[if !supportLists]-->34.<!--[endif]-->    public void init() throws ServletException {  

<!--[if !supportLists]-->35.<!--[endif]-->       log.debug("服务器启动了,log4j开始工作了");  

<!--[if !supportLists]-->36.<!--[endif]-->    }  

<!--[if !supportLists]-->37.<!--[endif]-->}  

 

2.2不使用spring架构

如果系统没有使用spring,我们以servlet为例,很简单,大家按照步骤执行即可,只贴代码,不说废话。

2.2.1 定义log4j配置文件

放在web工程的WEB-INF目录下,内容如:

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->log4j.rootCategory=INFO, stdout  

<!--[if !supportLists]-->2.  <!--[endif]-->log4j.rootLogger=info, stdout  

<!--[if !supportLists]-->3.  <!--[endif]-->   

<!--[if !supportLists]-->4.  <!--[endif]-->### stdout ###  

<!--[if !supportLists]-->5.  <!--[endif]-->log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

<!--[if !supportLists]-->6.  <!--[endif]-->log4j.appender.stdout.Target=System.out  

<!--[if !supportLists]-->7.  <!--[endif]-->log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  

<!--[if !supportLists]-->8.  <!--[endif]-->log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n  

<!--[if !supportLists]-->9.  <!--[endif]-->   

<!--[if !supportLists]-->10.<!--[endif]-->### set package ###  

<!--[if !supportLists]-->11.<!--[endif]-->log4j.logger.org.apache.catalina=info  

<!--[if !supportLists]-->12.<!--[endif]-->log4j.logger.org.apache.commons.digester.Digester=info  

<!--[if !supportLists]-->13.<!--[endif]-->log4j.logger.org.apache.catalina.startup.TldConfig=info  

<!--[if !supportLists]-->14.<!--[endif]-->log4j.logger.com.dheaven=debug  

 

2.2.2  创建log4j初始化类

[java] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->package com.dheaven.mip.web;  

<!--[if !supportLists]-->2.  <!--[endif]-->   

<!--[if !supportLists]-->3.  <!--[endif]-->import javax.servlet.ServletException;  

<!--[if !supportLists]-->4.  <!--[endif]-->import javax.servlet.http.HttpServlet;  

<!--[if !supportLists]-->5.  <!--[endif]-->   

<!--[if !supportLists]-->6.  <!--[endif]-->import org.apache.log4j.PropertyConfigurator;  

<!--[if !supportLists]-->7.  <!--[endif]-->   

<!--[if !supportLists]-->8.  <!--[endif]-->public class InitLog4j extends HttpServlet {  

<!--[if !supportLists]-->9.  <!--[endif]-->   

<!--[if !supportLists]-->10.<!--[endif]-->    private static final long serialVersionUID = 1L;  

<!--[if !supportLists]-->11.<!--[endif]-->   

<!--[if !supportLists]-->12.<!--[endif]-->    public void init() throws ServletException {  

<!--[if !supportLists]-->13.<!--[endif]-->       String prefix = getServletContext().getRealPath("/");  

<!--[if !supportLists]-->14.<!--[endif]-->       prefix = prefix.replace("//""/");  

<!--[if !supportLists]-->15.<!--[endif]-->       String file = getInitParameter("log4j-init-file");  

<!--[if !supportLists]-->16.<!--[endif]-->       // if the log4j-init-file is not set, then no point in trying  

<!--[if !supportLists]-->17.<!--[endif]-->       if (file != null) {  

<!--[if !supportLists]-->18.<!--[endif]-->           PropertyConfigurator.configure(prefix + file);  

<!--[if !supportLists]-->19.<!--[endif]-->       }  

<!--[if !supportLists]-->20.<!--[endif]-->    }  

<!--[if !supportLists]-->21.<!--[endif]-->}  

 

 2.2.3  Web.xml中定义初始化类

[xhtml] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]--><servlet>  

<!--[if !supportLists]-->2.  <!--[endif]-->   <servlet-name>log4j-init</servlet-name>  

<!--[if !supportLists]-->3.  <!--[endif]-->   <servlet-class>chb.test.web.InitLog4j</servlet-class>  

<!--[if !supportLists]-->4.  <!--[endif]-->  

<!--[if !supportLists]-->5.  <!--[endif]-->   <init-param>  

<!--[if !supportLists]-->6.  <!--[endif]-->       <param-name>log4j-init-file</param-name>  

<!--[if !supportLists]-->7.  <!--[endif]-->       <param-value>WEB-INF/log4j.properties</param-value>  

<!--[if !supportLists]-->8.  <!--[endif]-->   </init-param>  

<!--[if !supportLists]-->9.  <!--[endif]-->    

<!--[if !supportLists]-->10.<!--[endif]-->   <load-on-startup>1</load-on-startup>  

<!--[if !supportLists]-->11.<!--[endif]--></servlet>  

 

2.2.4 测试类

[java] view plaincopy

<!--[if !supportLists]-->1.  <!--[endif]-->package chb.test.web;  

<!--[if !supportLists]-->2.  <!--[endif]-->   

<!--[if !supportLists]-->3.  <!--[endif]-->   

<!--[if !supportLists]-->4.  <!--[endif]-->import javax.servlet.ServletException;  

<!--[if !supportLists]-->5.  <!--[endif]-->import javax.servlet.http.HttpServlet;  

<!--[if !supportLists]-->6.  <!--[endif]-->   

<!--[if !supportLists]-->7.  <!--[endif]-->import org.apache.log4j.Logger;  

<!--[if !supportLists]-->8.  <!--[endif]-->   

<!--[if !supportLists]-->9.  <!--[endif]-->public class InitiaServlet extends HttpServlet {  

<!--[if !supportLists]-->10.<!--[endif]-->     

<!--[if !supportLists]-->11.<!--[endif]-->    protected Logger log = Logger.getLogger(InitiaServlet.class);  

<!--[if !supportLists]-->12.<!--[endif]-->   

<!--[if !supportLists]-->13.<!--[endif]-->    private static final long serialVersionUID = 8550329576989690578L;  

<!--[if !supportLists]-->14.<!--[endif]-->   

<!--[if !supportLists]-->15.<!--[endif]-->    /** 

<!--[if !supportLists]-->16.<!--[endif]-->     * Constructor of the object. 

<!--[if !supportLists]-->17.<!--[endif]-->     */  

<!--[if !supportLists]-->18.<!--[endif]-->    public InitiaServlet() {  

<!--[if !supportLists]-->19.<!--[endif]-->       super();  

<!--[if !supportLists]-->20.<!--[endif]-->    }  

<!--[if !supportLists]-->21.<!--[endif]-->   

<!--[if !supportLists]-->22.<!--[endif]-->    /** 

<!--[if !supportLists]-->23.<!--[endif]-->     * Destruction of the servlet. <br> 

<!--[if !supportLists]-->24.<!--[endif]-->     */  

<!--[if !supportLists]-->25.<!--[endif]-->    public void destroy() {  

<!--[if !supportLists]-->26.<!--[endif]-->       super.destroy();  

<!--[if !supportLists]-->27.<!--[endif]-->    }  

<!--[if !supportLists]-->28.<!--[endif]-->   

<!--[if !supportLists]-->29.<!--[endif]-->    /** 

<!--[if !supportLists]-->30.<!--[endif]-->     * Initialization of the servlet. <br> 

<!--[if !supportLists]-->31.<!--[endif]-->     * 

<!--[if !supportLists]-->32.<!--[endif]-->     * @throws ServletException if an error occure 

<!--[if !supportLists]-->33.<!--[endif]-->     */  

<!--[if !supportLists]-->34.<!--[endif]-->    public void init() throws ServletException {  

<!--[if !supportLists]-->35.<!--[endif]-->       log.debug("服务器启动了,log4j开始工作了");  

<!--[if !supportLists]-->36.<!--[endif]-->    }  

<!--[if !supportLists]-->37.<!--[endif]-->}  

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值