javaweb程序配置log4j日志记录

第一步:在web工程下,导入log4j的jar包,即在lib文件夹下导入log4j的jar包

第二步:在src目录下,创建log4j.properties.我写入了以下代码

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
这里配置的输出log文件,如果是当天的是log_,如果不是当天的则是yyyy-MM-dd.log比如2015-07-28.log,日志的位置在tomcat或其他中间件的logs文件夹下。

第三步,手写一个Log4jInit.java,其实是一个servlet,  web服务器启动时就加载这个servlet加载log4j.properties配置文件。如下

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 Log4jInit() {
	  super();
	 }

	 /**
	  * Initialization of the servlet. 
* * @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); } } public void destroy() { super.destroy(); } }
第四步,在web.xml文件中配置这个servlet.如下:


  Log4jInit
  com.bmw.example.Log4jInit
  
   log4j
   classpath:log4j.properties
  
 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下面是我写的一个servlet进行测试的,并在web.xml中进行了配置

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;

	 
	 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    //如果直接 className.class 日志输出到全局的 即rootLogger 指定的文件中
	    Logger logger = Logger.getLogger(Test.class.getName());
	   //如果指定logger名字,则是把日志,输出到pay-log 指定的日志文件中去
	  System.out.println("================97987==============");
	  logger.info("日志信息开始!");
	  logger.info("日志信息结束!");
	  try {
	   Integer.parseInt("a");
	  } catch (NumberFormatException e) {
	   logger.error("解析数字出现异常",e);
	   e.printStackTrace();
	  }
	 }

	 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
	  this.doGet(request, response);
	 }
}

 
    Test
    com.bmw.example.Test
  

  
    Test
    /servlet/Test
  

启动服务器之后,在浏览器输入http://localhost:8080/weblog4j/servlet/Test 进行测试,测试成功!



你可能感兴趣的:(java,基础)