Log4jInit.java
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:1k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

  1. package com.chinacannel.common;
  2. import javax.servlet.http.HttpServlet;
  3. import org.apache.log4j.*;
  4. import javax.servlet.*;
  5. import java.util.Properties;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. public class Log4jInit extends HttpServlet {
  9.     static Logger logger = Logger.getLogger(Log4jInit.class);
  10.     public Log4jInit() {}
  11.     public void init(ServletConfig config) throws ServletException {
  12.         String prefix = config.getServletContext().getRealPath("/");
  13.         String file = config.getInitParameter("log4j");
  14.         String filePath = prefix + file;
  15.         Properties props = new Properties();
  16.         try {
  17.             FileInputStream istream = new FileInputStream(filePath);
  18.             props.load(istream);
  19.             istream.close();
  20.             //toPrint(props.getProperty("log4j.appender.file.File"));
  21.             String logFile = prefix +
  22.                              props.getProperty("log4j.appender.file.File"); //设置路径
  23.             props.setProperty("log4j.appender.file.File", logFile);
  24.             PropertyConfigurator.configure(props); //装入log4j配置信息
  25.         } catch (IOException e) {
  26.             toPrint("Could not read configuration file [" + filePath + "].");
  27.             toPrint("Ignoring configuration file [" + filePath + "].");
  28.             return;
  29.         }
  30.     }
  31.     public static void toPrint(String content) {
  32.         System.out.println(content);
  33.     }
  34. }