原文地址:http://ketayao.com/view/33
web开发,有个场景,希望将错误日志发送到指定邮箱,这样可以做到远程监控,实时处理。下面写一个利用apache commons email的例子,发送错误日志的例子。
003 |
*
@author <a href="mailto:ketayao@gmail.com">ketayao</a> |
004 |
*
@since 2013年9月7日 下午6:27:05 |
007 |
public class EmailExceptionHandler extends ExceptionHandler
{ |
009 |
private static final Logger
log = LoggerFactory.getLogger(EmailExceptionHandler.class); |
016 |
public static void reportError(HttpServletRequest
req, Throwable excp){ |
017 |
boolean is_localhost
= (req!=null)?"127.0.0.1".equals(RequestUtils.getRemoteAddr(req)):false; |
019 |
if(t
== null)
t = _getException(req); |
020 |
if(t
== null) return ; |
022 |
log.error("System
Exception",
t); |
026 |
String
email = SystemConfig.getConfig().get("blog.exception.email.to"); |
027 |
String
title = "错误" +
t.getClass().getSimpleName(); |
028 |
String
content = getErrorHtml(req, t); |
030 |
_sendHtmlMail(Arrays.asList(StringUtils.split(email,",")),
title, content); |
031 |
} catch (Exception
e) { |
032 |
log.error("Failed
to send error report.",
e); |
043 |
private static void _sendHtmlMail(List<String>
emailAddress, String title, |
044 |
String
content) throws Exception
{ |
045 |
for (String
address : emailAddress) { |
046 |
HtmlEmail
email = new HtmlEmail(); |
047 |
email.setStartTLSEnabled(true); |
048 |
email.setHostName(SystemConfig.getConfig().get("blog.exception.email.hotname")); |
049 |
email.setAuthentication(SystemConfig.getConfig().get("blog.exception.email.name"), |
050 |
SystemConfig.getConfig().get("blog.exception.email.password")); |
051 |
email.setFrom(SystemConfig.getConfig().get("blog.exception.email.name")); |
053 |
email.addTo(address); |
055 |
email.setSubject(title); |
057 |
email.setHtmlMsg(content); |
059 |
email.setTextMsg("Your
email client does not support HTML messages"); |
069 |
*
@param site 出错的个人空间 |
071 |
*
<h2>Request Headers</h2> |
073 |
@SuppressWarnings("rawtypes") |
074 |
public static String
getErrorHtml(HttpServletRequest req, Throwable t) { |
075 |
StringBuilder
html = new StringBuilder(); |
077 |
html.append("<h2>Request
Headers</h2><table>"); |
078 |
html.append("<tr><th>Request
URL</th><td>"); |
079 |
html.append(req.getRequestURL().toString()); |
080 |
if(req.getQueryString()!=null){ |
082 |
html.append(req.getQueryString()); |
084 |
html.append("</td></tr>"); |
085 |
html.append("<tr><th>Remote
Addr</th><td>"); |
086 |
html.append(RequestUtils.getRemoteAddr(req)); |
087 |
html.append("</td></tr>"); |
088 |
html.append("<tr><th>Request
Method</th><td>"); |
089 |
html.append(req.getMethod()); |
090 |
html.append("</td></tr>"); |
091 |
html.append("<tr><th>CharacterEncoding</th><td>"); |
092 |
html.append(req.getCharacterEncoding()); |
093 |
html.append("</td></tr>"); |
094 |
html.append("<tr><th>Request
Locale</th><td>"); |
095 |
html.append(req.getLocale()); |
096 |
html.append("</td></tr>"); |
097 |
html.append("<tr><th>Content
Type</th><td>"); |
098 |
html.append(req.getContentType()); |
099 |
html.append("</td></tr>"); |
100 |
Enumeration
headers = req.getHeaderNames(); |
101 |
while(headers.hasMoreElements()){ |
102 |
String
key = (String)headers.nextElement(); |
103 |
html.append("<tr><th>"); |
105 |
html.append("</th><td>"); |
106 |
html.append(req.getHeader(key)); |
107 |
html.append("</td></tr>"); |
109 |
html.append("</table><h2>Request
Parameters</h2><table>"); |
110 |
Enumeration
params = req.getParameterNames(); |
111 |
while(params.hasMoreElements()){ |
112 |
String
key = (String)params.nextElement(); |
113 |
html.append("<tr><th>"); |
115 |
html.append("</th><td>"); |
116 |
html.append(req.getParameter(key)); |
117 |
html.append("</td></tr>"); |
119 |
html.append("</table>"); |
122 |
html.append(t.getClass().getName()); |
124 |
html.append(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd
HH:mm:ss")); |
125 |
html.append(")</h2><pre>"); |
127 |
html.append(_exception(t)); |
128 |
} catch (IOException
ex) {} |
129 |
html.append("</pre>"); |
131 |
html.append("<h2>System
Properties</h2><table>"); |
132 |
Set
props = System.getProperties().keySet(); |
133 |
for(Object
prop : props){ |
134 |
html.append("<tr><th>"); |
136 |
html.append("</th><td>"); |
137 |
html.append(System.getProperty((String)prop)); |
138 |
html.append("</td></tr>"); |
140 |
html.append("</table>"); |
141 |
return html.toString(); |
147 |
*
@throws IOException |
149 |
private static Throwable
_getException(HttpServletRequest req) { |
150 |
if(req
== null) return null; |
151 |
Throwable
t = (Throwable)req.getAttribute("javax.servlet.jsp.jspException"); |
154 |
t
= (Throwable)req.getAttribute("javax.servlet.error.exception"); |
163 |
*
@throws IOException |
165 |
private static String
_exception(Throwable t) throws IOException{ |
168 |
ByteArrayOutputStream
baos = new ByteArrayOutputStream(); |
170 |
t.printStackTrace(new PrintStream(baos)); |
174 |
return baos.toString(); |
181 |
*
@see com.ketayao.fensy.handler.Handler#handle(com.ketayao.fensy.mvc.RequestContext, java.lang.Exception) |
184 |
public String
handle(final RequestContext
rc, final Exception
exception) { |
185 |
String
view = super.handle(rc,
exception); |
187 |
Thread
thread = new Thread(new Runnable()
{ |
191 |
reportError(rc.request(),
exception); |