lbom

小江西

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  18 随笔 :: 21 文章 :: 69 评论 :: 0 Trackbacks

1、编写log4j配置文件(如log.config):
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, FILE
log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
#WORKDIR setted by system.setProperty!
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=${WORKDIR}/log/adapter.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=INFO
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

2、使用LogServlet初始化log4j的配置参数:
package com.cait.adapter.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

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

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class LogServlet extends HttpServlet {

 /**
  *
  */
 private static final long serialVersionUID = -1837292475877367091L;

 static Logger logger = Logger.getLogger(LogServlet.class);

 public LogServlet() {
 }

 public void init(ServletConfig sc) throws ServletException {

  String prefix = "";
  if (sc != null) {
   prefix = sc.getServletContext().getRealPath("/");
  } else {
   prefix = "D:/Project_java/Any3Adapter/web";
  }
  String file = sc.getInitParameter("logConfigFile");
  
  if (System.getProperty("WORKDIR") == null) {
   System.setProperty("WORKDIR", prefix);
  }
  
  Properties props = new Properties();
  try {
   FileInputStream istream = new FileInputStream(prefix + file);
   props.load(istream);
   istream.close();
   String logFile = prefix + props.getProperty("log4j.appender.file.File");// 设置路径
   props.setProperty("log4j.appender.file.File", logFile);
   PropertyConfigurator.configure(props);
  }
  catch (IOException e) {
   System.out.println("Could not read configuration file [" + file + "].");
   System.out.println("Ignoring configuration file [" + file + "].");
   return;
  }
 }

}

3、设置web.xml的servlet选项,保证配置被加载
 <servlet>
  <servlet-name>LogServlet</servlet-name>
  <servlet-class>com.cait.adapter.servlet.LogServlet</servlet-class>
  <init-param>
   <param-name>logConfigFile</param-name>
   <param-value>WEB-INF/config/log.config</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

4、测试用例:
....
   Logger log=Logger.getLogger(this.getClass());
   log.info("LogServlet Test:    testOK");
...
posted on 2007-05-24 14:09 lbom 阅读(1148) 评论(0)  编辑  收藏 所属分类: 个人总结

只有注册用户登录后才能发表评论。


网站导航: