关闭

java Web应用配置log4j日志记录

1030人阅读 评论(0) 收藏 举报
分类:
第一步:首先建立一个WEB工程,去log4j官网下log4j的JAR包导入到工程的lib目录下

第二步:在src目录下建一个log4j.properties 文件,文件命名可以由自己,只是记加载时候和这里名字一致就行;

log4j.properties  里边的内容如下:

 ### set log levels ###
log4j.rootLogger = debug,stdout,E

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 %c{1}:%L - %m%n

log4j.appender.E= org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =${catalina.home}/logs/log_
log4j.appender.E.DatePattern=yyyy-MM-dd'.log'
log4j.appender.E.Threshold =DEBUG
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss}[%c] [%t\:%r] - [%p]  %m%n

解释下几个常用的,其它的在详细的,请自己去网上查找资料:
stdout是一个日志记录配置,它的意思是把日志输出到控制台上,也就是这个配置属性--->log4j.appender.stdout.Target = System.out ,到时候到了生产上可以注解掉就可以了。
E是一个日志记录配置,它是把日志文件记录到文件中去,log4j.appender.E.File =${catalina.home}/logs/log_  是放日志文件的路径,其中${catalina.home}是容器的路径
log4j.appender.E= org.apache.log4j.DailyRollingFileAppender  这个属性配置是一天产生一个新的日志文件(还有其它配置方式,这个是用的最多的)
log4j.appender.E.DatePattern=yyyy-MM-dd'.log'  这个属性是以时间为名字的文件日志,形式如下:
log_2011-06-26.log,log_2011-06-28.log,不过第一个生成的日志文件的名字是:log_
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss}[%c] [%t\:%r] - [%p]  %m%n  这个是输出日志内容格式,详细情况请自己查阅资料;
log4j.appender.E.Threshold =DEBUG  这个是输出日志级别(意思是只输出比DEBUG级别高的日志信息);

第三步:配置容器起动时候加载log4j配置文件:
建立一个servlet内容如下:

package com.tchzt.logTest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.PropertyConfigurator;

public class Log4jInit extends HttpServlet {
 private static final long serialVersionUID = 1L;
 public void destroy() {
  super.destroy();
 }

 public Log4jInit() {
  super();
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  String file =this.getInitParameter("log4j");//从web.xml配置读取,名字一定要和web.xml配置一致
  if(file != null){
     PropertyConfigurator.configure(file);
  }
 }

}

web.xml配置:
<servlet>
  <servlet-name>Log4jInit</servlet-name>
  <servlet-class>com.tchzt.logTest.Log4jInit</servlet-class>
  <init-param>
   <param-name>log4j</param-name>//这个是名字是下边路径配置的标识(好像KEY一样)
   <param-value>/WEB-INF/classes/log4j.properties</param-value>//这是容器初始化时候加载log4j配置文件的路径(这好像一个value);
  </init-param>
 </servlet>

好了基本上配置完了开始下测试了:
测试类:建立一个servlet内容如下:

package com.tchzt.logTest;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class Test extends HttpServlet {

 private static final long serialVersionUID = 1L;

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  Logger logger = Logger.getLogger(Test.class.getName());
  MyLog4j.getSomething();
  System.out.println("================97987==============");
  logger.info("日志信息开始!");
  logger.info("日志信息结束!");
  try {
   Integer.parseInt("a");
  } catch (NumberFormatException e) {
   logger.error("解析数字出现异常",e);
   e.printStackTrace();
  }
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  this.doGet(request, response);
 }

}


这个servlet 的web.xml配置如下:
<servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>com.tchzt.logTest.Test</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Test</servlet-name>
    <url-pattern>/servlet/Test</url-pattern>
  </servlet-mapping>


启动容器:输出URL:http://localhost:8080/Log4jWebTest/servlet/Test
第二次关闭容器,修改系统时间;在启动容器,最后生成日志文件如下:

log_;log_2011-06-26.log;log_2011-06-28.log;log_2011-06-30.log

内容这里选一个文件里的内容:
2011-06-30 21:59:28[test.jsp] [http-8080-2:0] - [ERROR]  asdfasdf
2011-06-30 21:59:28[test.jsp] [http-8080-2:16] - [INFO]  Test Logging in JSP
2011-06-30 21:59:28[test.jsp] [http-8080-2:16] - [FATAL]  Test fatal logging in jsp page.
2011-06-30 21:59:28[test.jsp] [http-8080-2:16] - [ERROR]  解析数字出现异常
java.lang.NumberFormatException: For input string: "a"
 at java.lang.NumberFormatException.forInputString(Unknown Source)
 at java.lang.Integer.parseInt(Unknown Source)
 at java.lang.Integer.parseInt(Unknown Source)
 at org.apache.jsp.index_jsp._jspService(index_jsp.java:90)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
 at java.lang.Thread.run(Unknown Source)
2011-06-30 21:59:37[com.tchzt.logTest.MyLog4j] [http-8080-2:8641] - [ERROR]  java.io.FileNotFoundException: F:\tt.txt (系统找不到指定的文件。)
2011-06-30 21:59:37[com.tchzt.logTest.Test] [http-8080-2:8656] - [INFO]  日志信息开始!
2011-06-30 21:59:37[com.tchzt.logTest.Test] [http-8080-2:8656] - [INFO]  日志信息结束!
2011-06-30 21:59:37[com.tchzt.logTest.Test] [http-8080-2:8656] - [ERROR]  解析数字出现异常
java.lang.NumberFormatException: For input string: "a"
 at java.lang.NumberFormatException.forInputString(Unknown Source)
 at java.lang.Integer.parseInt(Unknown Source)
 at java.lang.Integer.parseInt(Unknown Source)
 at com.tchzt.logTest.Test.doGet(Test.java:33)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
 at java.lang.Thread.run(Unknown Source)

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:164211次
    • 积分:2831
    • 等级:
    • 排名:第12637名
    • 原创:93篇
    • 转载:167篇
    • 译文:1篇
    • 评论:15条
    最新评论