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

时间:2022-06-09 20:29:34

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

 package com.wyp.helper;

 import org.apache.log4j.Logger;

 public class LoggerHelper {
private static Logger _logger = Logger.getLogger(LoggerHelper.class);
private static String newLine = System.getProperty("line.separator"); public static void info(Logger logger, String message) {
logger.info(newLine);
logger.info(message);
} public static void info(Logger logger, String message, Throwable exception) {
logger.info(newLine);
logger.info(message, exception);
} public static void debug(Logger logger, String message) {
logger.debug(newLine);
logger.debug(message);
} public static void debug(Logger logger, String message, Throwable exception) {
logger.debug(newLine);
logger.debug(message, exception);
} public static void error(Logger logger, String message) {
logger.error(newLine);
logger.error(message);
} public static void error(Logger logger, String message, Throwable exception) {
logger.error(newLine);
logger.error(message, exception);
} public static void info(String message) {
_logger.info(newLine);
_logger.info(message);
} public static void info(String message, Throwable exception) {
_logger.info(newLine);
_logger.info(message, exception);
} public static void debug(String message) {
_logger.debug(newLine);
_logger.debug(message);
} public static void debug(String message, Throwable exception) {
_logger.debug(newLine);
_logger.debug(message, exception);
} public static void error(String message) {
_logger.error(newLine);
_logger.error(message);
} public static void error(String message, Throwable exception) {
_logger.error(newLine);
_logger.error(message, exception);
} }

LoggerHelper

创建Global.java文件:

 package helper;

 import java.io.File;

 import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator; public class Global implements ServletContextListener { private Logger _logger = Logger.getLogger(Global.class); @Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
LoggerHelper.info(_logger, "网站停止");
} @Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
ServletContext sc = arg0.getServletContext(); String prefix = sc.getRealPath("/WEB-INF");
String file = "log4j.properties";
String configPath = prefix + File.separator + file; String logFilePath = sc.getRealPath("/");
System.setProperty("tomcat", logFilePath); PropertyConfigurator.configure(configPath); LoggerHelper.info(_logger, "网站启动");
}
}

Global

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

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>JspLog4j</display-name>
<listener>
<listener-class>helper.Global</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>

web.xml

自定义错误页面error.jsp。

 <%@page import="com.wyp.helper.LoggerHelper"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>
<%
response.setStatus(200);
String newLine = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append(newLine);
sb.append("访问的路径:");
sb.append(newLine);
sb.append(request.getRequestURI());
sb.append(newLine);
sb.append("异常信息:");
sb.append(newLine);
sb.append(exception.getClass() + " : " + exception.getMessage()); Map<String,String[]> map = request.getParameterMap();
if (map.size()>0) {
sb.append(newLine);
sb.append("请求参数:");
sb.append(newLine);
for(Map.Entry<String,String[]> entry : map.entrySet()){
String key = entry.getKey();
String[] values = entry.getValue();
sb.append(key+"=");
for(String value : values){
sb.append(value+",");
}
}
sb.append(newLine);
}
LoggerHelper.error(sb.toString(),exception); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>自定义错误页面</title>
</head>
<body>
<h1>自定义错误页面</h1>
</body>
</html>

error.jsp

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

 log4j.rootLogger=debug,stdout,logfile

 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=${tomcat}/logs/website.txt
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.DatePattern='.'yyyy-MM-dd
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目录下面。