关于监测网站出现500错误后邮件通知的代码

发布时间:2011-02-28 14:35:57   来源:文档文库   
字号:

[代码] 500.vm

view source

print?

01

02

03

04

05

    </span><span class='_1'>应用程序出错</span><span class='_1'>

06

    

07

    

08

09

10

#set($__nothing = $osc_tool.report_error($request))

11

  

12

    

13

    

您访问的页面发生错误!

14

    

我们已经将此错误信息记录下来,并将尽快处理,为此造成您的不便请多见谅
      

15

    

16

  

17

18

[代码] web.xml

view source

print?

1

2

    500

3

    /500.vm

4

[代码] [Java]代码

view source

print?

1

/**

2

 * 报告错误信息

3

 * vm上的用法 $osc_tool.report_error($request)

4

 * @param t

5

 */

6

public static void report_error(HttpServletRequest req){

7

    SmtpHelper.reportError(req, null);

8

}

[代码] SmtpHelper.java

view source

print?

001

/**

002

 * 报告错误信息

003

 * @param req

004

 * @param excp

005

 */

006

public static void reportError(HttpServletRequest req, Throwable excp){

007

    boolean is_localhost = (req!=null)?"127.0.0.1".equals(RequestUtils.getRemoteAddr(req)):false;

008

    Throwable t = excp;

009

    if(t == null) t = _GetException(req);

010

    if(t == null) return ;

011

 

012

    log.error("System Exception", t);

013

    if(!is_localhost)

014

    //发送电子邮件通知

015

    try {

016

        String email = smtp_props.getProperty("administrator");

017

        String title = ResourceUtils.getString("ui", "error_500", t.getClass().getSimpleName());

018

        String content = getErrorHtml(req, t);

019

        //发送邮件到指定邮箱

020

        _SendHtmlMail(s.asList(StringUtils.split(email,",")), title, content);

021

    } catch (Exception e) {

022

        log.error("Failed to send error report.", e);

023

    }

024

}

025

 

026

/**

027

 * 格式化错误信息

028

 * @param req

029

 * @param t 错误信息

030

 * @param site 出错的个人空间

031

 * @return

032

 *

Request Headers

033

 */

034

@SuppressWarnings("rawtypes")

035

public static String getErrorHtml(HttpServletRequest req, Throwable t) {

036

    StringBuilder html = new StringBuilder(error_css);

037

    if(req != null){

038

        html.append("

Request Headers

");  

039

        html.append("

Request URL");

040

        html.append(req.getRequestURL().toString());

041

        if(req.getQueryString()!=null){

042

            html.append('?');

043

            html.append(req.getQueryString());                     

044

        }

");

045

        html.append("

046

        html.append("

Remote Addr");

");

047

        html.append(RequestUtils.getRemoteAddr(req));

048

        html.append("

049

        html.append("

Request Method");

050

        html.append(req.getMethod());

");

051

        html.append("

052

        html.append("

CharacterEncoding");

");

053

        html.append(req.getCharacterEncoding());

054

        html.append("

055

        html.append("

Request Locale");

056

        html.append(req.getLocale());

");

057

        html.append("

058

        html.append("

Content Type");

");

059

        html.append(req.getContentType());

060

        html.append("

061

        Enumeration headers = req.getHeaderNames();

062

        while(headers.hasMoreElements()){

063

            String key = (String)headers.nextElement();

064

            html.append("

");

065

            html.append(key);

066

            html.append("

");

");

067

            html.append(req.getHeader(key));

068

            html.append("

069

        }      

070

        html.append("

Request Parameters

");     

071

        Enumeration params = req.getParameterNames();

072

        while(params.hasMoreElements()){

073

            String key = (String)params.nextElement();

074

            html.append("

");

075

            html.append(key);

076

            html.append("

");

");

077

            html.append(req.getParameter(key));

078

            html.append("

079

        }

080

        html.append("

");

081

    }

082

    html.append("

");

083

    html.append(t.getClass().getName());

084

    html.append('(');

085

    html.append(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));

086

    html.append(")

");

087

    try {

088

        html.append(_Exception(t));

089

    } catch (IOException ex) {}

090

    html.append("");

091

 

092

    html.append("

System Properties

");    

093

    Set props = System.getProperties().keySet();

094

    for(Object prop : props){

095

        html.append("

");

096

        html.append(prop);

097

        html.append("

");

098

        html.append(System.getProperty((String)prop));

");

099

        html.append("

100

    }

101

    html.append("

");

102

    return html.toString();

103

}

104

 

105

/**

106

 * 将当前上下文发生的异常转为字符串

107

 * @return

108

 * @throws IOException

109

 */

110

private static Throwable _GetException(HttpServletRequest req) {

111

    if(req == null) return null;

112

    Throwable t = (Throwable)req.getAttribute("javax.servlet.jsp.jspException");

113

    if(t==null){

114

        //Tomcat的错误处理方式

115

        t = (Throwable)req.getAttribute("javax.servlet.error.exception");

116

    }

117

    return t;

118

}

119

 

120

/**

121

 * 将异常信息转化成字符串

122

 * @param t

123

 * @return

124

 * @throws IOException

125

 */

126

private static String _Exception(Throwable t) throws IOException{

127

    if(t == null)

128

        return null;

129

    ByteOutputStream baos = new ByteOutputStream();

130

    try{

131

        t.printStackTrace(new PrintStream(baos));

132

    }finally{

133

        baos.close();

134

    }

135

    return baos.toString();

136

}

本文来源:https://www.2haoxitong.net/k/doc/4c610edba58da0116c17494a.html

《关于监测网站出现500错误后邮件通知的代码.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式