That not only depends on what API's and/or frameworks you're already using (e.g. log4j), but it also depends on how you handle the global exceptions. If you just have definied an error-page
in web.xml
, then for example the aforementioned log4j surely won't see/handle it. You need to catch and log it yourself.
You can use a Filter
listening on an url-pattern
of /*
for this:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
try {
chain.doFilter(request, response);
} catch (Exception e) {
logger.error(e); // Let "error" level associate with SMTPAppender.
throw new ServletException(e); // Will be handled by error-page.
}
}
Or in a controller servlet if you have one:
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
View view = new View(request, response);
Action action = ActionFactory.getAction(request);
try {
action.execute(view);
} catch (Exception e) {
logger.error(e); // Let "error" level associate with SMTPAppender.
throw new ServletException(e); // Will be handled by error-page.
}
view.navigate();
}
As a short but semantically nasty alternative you can also make use of scriptlets in JSP. The exception
is available as implicit variable in JSP. Log it in the error page and let the logger handle the mailing stuff. E.g.
<% logger.error(exception); %>