日期:2014-05-18 浏览次数:20724 次
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>InitLog4jServlet</servlet-name>
<servlet-class>com.InitLog4jServlet</servlet-class>
<init-param>
<param-name>log4jLocation</param-name>
<param-value>WEB-INF\log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
/**
*@author
*@date 2014-4-13
*/
public class InitLog4jServlet extends HttpServlet {
public void init() throws ServletException {
String path=this.getServletContext().getRealPath("/");
String log4jPath=path+this.getInitParameter("log4jLocation");
System.out.println(log4jPath);
PropertyConfigurator.configure(log4jPath);
}
}
import org.apache.log4j.Logger;
/**
*@author
*@date 2014-4-13
*/
public class Test {
private static final Logger logger=Logger.getLogger(Test.class);
public static void main(String[] args) {
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.fatal("fatal");
}
}