Log4jInit.java
资源名称:shihua.rar [点击查看]
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:1k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
JavaScript
- package com.chinacannel.common;
- import javax.servlet.http.HttpServlet;
- import org.apache.log4j.*;
- import javax.servlet.*;
- import java.util.Properties;
- import java.io.FileInputStream;
- import java.io.IOException;
- public class Log4jInit extends HttpServlet {
- static Logger logger = Logger.getLogger(Log4jInit.class);
- public Log4jInit() {}
- public void init(ServletConfig config) throws ServletException {
- String prefix = config.getServletContext().getRealPath("/");
- String file = config.getInitParameter("log4j");
- String filePath = prefix + file;
- Properties props = new Properties();
- try {
- FileInputStream istream = new FileInputStream(filePath);
- props.load(istream);
- istream.close();
- //toPrint(props.getProperty("log4j.appender.file.File"));
- String logFile = prefix +
- props.getProperty("log4j.appender.file.File"); //设置路径
- props.setProperty("log4j.appender.file.File", logFile);
- PropertyConfigurator.configure(props); //装入log4j配置信息
- } catch (IOException e) {
- toPrint("Could not read configuration file [" + filePath + "].");
- toPrint("Ignoring configuration file [" + filePath + "].");
- return;
- }
- }
- public static void toPrint(String content) {
- System.out.println(content);
- }
- }