Java Web Project自定义错误页面,log4j记录日志。

创建记录日志的文件LoggerHelper.java:

 1 package com.wyp.helper;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 public class LoggerHelper {
 6     private static Logger _logger = Logger.getLogger(LoggerHelper.class);
 7     private static String newLine = System.getProperty("line.separator");
 8 
 9     public static void info(Logger logger, String message) {
10         logger.info(newLine);
11         logger.info(message);
12     }
13 
14     public static void info(Logger logger, String message, Throwable exception) {
15         logger.info(newLine);
16         logger.info(message, exception);
17     }
18 
19     public static void debug(Logger logger, String message) {
20         logger.debug(newLine);
21         logger.debug(message);
22     }
23 
24     public static void debug(Logger logger, String message, Throwable exception) {
25         logger.debug(newLine);
26         logger.debug(message, exception);
27     }
28 
29     public static void error(Logger logger, String message) {
30         logger.error(newLine);
31         logger.error(message);
32     }
33 
34     public static void error(Logger logger, String message, Throwable exception) {
35         logger.error(newLine);
36         logger.error(message, exception);
37     }
38 
39     public static void info(String message) {
40         _logger.info(newLine);
41         _logger.info(message);
42     }
43 
44     public static void info(String message, Throwable exception) {
45         _logger.info(newLine);
46         _logger.info(message, exception);
47     }
48 
49     public static void debug(String message) {
50         _logger.debug(newLine);
51         _logger.debug(message);
52     }
53 
54     public static void debug(String message, Throwable exception) {
55         _logger.debug(newLine);
56         _logger.debug(message, exception);
57     }
58 
59     public static void error(String message) {
60         _logger.error(newLine);
61         _logger.error(message);
62     }
63 
64     public static void error(String message, Throwable exception) {
65         _logger.error(newLine);
66         _logger.error(message, exception);
67     }
68 
69 }
LoggerHelper

创建Global.java文件:

 1 package helper;
 2 
 3 import java.io.File;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.ServletContextEvent;
 7 import javax.servlet.ServletContextListener;
 8 
 9 import org.apache.log4j.Logger;
10 import org.apache.log4j.PropertyConfigurator;
11 
12 public class Global implements ServletContextListener {
13 
14     private Logger _logger = Logger.getLogger(Global.class);
15     
16     @Override
17     public void contextDestroyed(ServletContextEvent arg0) {
18         // TODO Auto-generated method stub
19         LoggerHelper.info(_logger, "网站停止");
20     }
21 
22     @Override
23     public void contextInitialized(ServletContextEvent arg0) {
24         // TODO Auto-generated method stub
25         ServletContext sc = arg0.getServletContext();
26 
27         String prefix = sc.getRealPath("/WEB-INF");
28         String file = "log4j.properties";
29         String configPath = prefix + File.separator + file;
30 
31         String logFilePath = sc.getRealPath("/");
32         System.setProperty("tomcat", logFilePath);
33 
34         PropertyConfigurator.configure(configPath);
35 
36         LoggerHelper.info(_logger, "网站启动");
37     }
38 }
Global

web.xml的配置很简单啦,网上搜一下就一大堆了。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     <display-name>JspLog4j</display-name>
 7     <listener>
 8         <listener-class>helper.Global</listener-class>
 9     </listener>
10     <welcome-file-list>
11         <welcome-file>index.jsp</welcome-file>
12     </welcome-file-list>
13     <error-page>
14         <error-code>500</error-code>
15         <location>/error.jsp</location>
16     </error-page>
17 </web-app>
web.xml

自定义错误页面error.jsp。

 1 <%@page import="com.wyp.helper.LoggerHelper"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>
 3 <%
 4     response.setStatus(200);
 5     String newLine = System.getProperty("line.separator");
 6     StringBuilder sb = new StringBuilder();
 7     sb.append(newLine);
 8     sb.append("访问的路径:");
 9     sb.append(newLine);
10     sb.append(request.getRequestURI());
11     sb.append(newLine);
12     sb.append("异常信息:");
13     sb.append(newLine);
14     sb.append(exception.getClass() + " : " + exception.getMessage());
15     
16     Map<String,String[]> map = request.getParameterMap();
17     if (map.size()>0) {
18         sb.append(newLine);
19         sb.append("请求参数:");
20         sb.append(newLine);
21         for(Map.Entry<String,String[]> entry : map.entrySet()){
22             String key = entry.getKey();
23             String[] values = entry.getValue();
24             sb.append(key+"=");
25             for(String value : values){
26                 sb.append(value+",");
27             }
28         }
29         sb.append(newLine);
30     }
31     LoggerHelper.error(sb.toString(),exception);
32 
33 %>
34 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
35 <html>
36   <head>
37     <title>自定义错误页面</title>
38   </head>
39   <body>
40     <h1>自定义错误页面</h1>
41   </body>
42 </html>
error.jsp

在WEB-INF文件夹里创建log4j.properties文件

 1 log4j.rootLogger=debug,stdout,logfile
 2 
 3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 4 log4j.appender.stdout.Target=System.err
 5 log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
 6 
 7 log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
 8 log4j.appender.logfile.File=${tomcat}/logs/website.txt
 9 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
10 log4j.appender.logfile.DatePattern='.'yyyy-MM-dd
11 log4j.appender.logfile.layout.ConversionPattern=%n%d{yyyy-MM-dd HH\:mm\:ss} %F %m
log4j.properties

默认日志文件存放在:apache-tomcat-6.0.36\bin\下面,可以使用占位符变量表示你日志信息存放的目录,例如:${tomcat}。

使用System.setProperty("tomcat",sc.getRealPath("/"));设置最终的目录。

最后如果部属目录没有log4j.properties和log4j.jar文件,需要手动将这个log4j.properties文件复制到Tomcat\webapps\项目名\WEB-INF文件夹里,将log4j.jar文件复制到对应的Tomcat\webapps\项目名\WEB-INF\lib目录下面。

posted @ 2013-09-01 16:50  —八戒—  阅读(516)  评论(0编辑  收藏  举报