servlet:
1 package com.charmyin.test; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import org.apache.log4j.PropertyConfigurator; 13 14 public class Log4jInitServlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 private final String WEB_APP_ROOT_DEFAULT = "webapp.root"; 18 19 public void init() throws ServletException { 20 String prefix = getServletContext().getRealPath("/"); 21 // System.setProperty("webapp.root", prefix); 22 String webAppRootKey = getServletConfig().getInitParameter( 23 "webAppRootKey"); 24 System.out.println(webAppRootKey); 25 if (webAppRootKey == null || webAppRootKey.length() < 1) { 26 webAppRootKey = WEB_APP_ROOT_DEFAULT; 27 } 28 System.setProperty(webAppRootKey, prefix); 29 30 String propfile = getServletConfig().getInitParameter( 31 "log4j-configuration"); 32 System.out.println(propfile); 33 if (propfile != null) { 34 PropertyConfigurator.configure(prefix + propfile); 35 System.out.println("Log4J Configured."); 36 } 37 } 38 39 protected void doGet(HttpServletRequest request, 40 HttpServletResponse response) throws ServletException, IOException { 41 // TODO Method stub generated by Lomboz 42 } 43 44 protected void doPost(HttpServletRequest request, 45 HttpServletResponse response) throws ServletException, IOException { 46 // TODO Method stub generated by Lomboz 47 } 48 }
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>http</display-name> 4 <servlet> 5 <servlet-name>Log4jInit</servlet-name> 6 <servlet-class>com.charmyin.test.Log4jInitServlet</servlet-class> 7 <init-param> 8 <param-name>log4j-configuration</param-name> 9 <param-value>/WEB-INF/log4j.properties</param-value> 10 </init-param> 11 <init-param> 12 <param-name>webAppRootKey</param-name> 13 <param-value>webapp.root</param-value> 14 </init-param> 15 <load-on-startup>0</load-on-startup> 16 </servlet> 17 <welcome-file-list> 18 <welcome-file>index.html</welcome-file> 19 <welcome-file>index.htm</welcome-file> 20 <welcome-file>index.jsp</welcome-file> 21 <welcome-file>default.html</welcome-file> 22 <welcome-file>default.htm</welcome-file> 23 <welcome-file>default.jsp</welcome-file> 24 </welcome-file-list> 25 </web-app>
log4j.properties:
1 # initialise root logger with level DEBUG 2 log4j.rootLogger=DEBUG, CONSOLE, LOGFILE 3 log4j.logger.com.mchange=OFF 4 5 #log4j.appender.stdout = org.apache.log4j.ConsoleAppender 6 #log4j.appender.stdout.Target = System.out 7 #log4j.appender.stdout.Threshold=ALL 8 #log4j.appender.stdout.layout = org.apache.log4j.PatternLayout 9 #log4j.appender.stdout.layout.ConversionPattern =[%-6p] [%d{yyyy-MM-dd HH/:mm/:ss}]%-5p %c(line/:%L) %x-%m%n 10 11 #\u8F93\u51FA\u5230\u63A7\u5236\u53F0 12 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 13 log4j.appender.CONSOLE.Threshold=DEBUG 14 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 15 log4j.appender.CONSOLE.layout.ConversionPattern=[%-6p] %d{yyyy-MM-dd HH:mm:ss,SSS} %l%n %m%n 16 17 #\u8F93\u51FA\u5230\u6587\u4EF6 18 log4j.appender.LOGFILE=org.apache.log4j.DailyRollingFileAppender 19 log4j.appender.LOGFILE.DatePattern='.'yyyy-MM-dd'.log' 20 log4j.appender.LOGFILE.Append=true 21 log4j.appender.LOGFILE.Threshold=DEBUG 22 log4j.appender.LOGFILE.File=${webapp.root}/http.log 23 log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout 24 log4j.appender.LOGFILE.layout.ConversionPattern= [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method: %l%n %m%n