org.dellroad.stuff.servlet
Class ExceptionLoggingFilter

java.lang.Object
  extended by org.springframework.web.filter.GenericFilterBean
      extended by org.springframework.web.filter.OncePerRequestFilter
          extended by org.dellroad.stuff.servlet.ExceptionLoggingFilter
All Implemented Interfaces:
Filter, BeanNameAware, DisposableBean, InitializingBean, ServletContextAware

public class ExceptionLoggingFilter
extends OncePerRequestFilter

Servlet filter that ensures any exceptions which are not handled by the servlet get logged somewhere.


Field Summary
 
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
 
Fields inherited from class org.springframework.web.filter.GenericFilterBean
logger
 
Constructor Summary
ExceptionLoggingFilter()
           
 
Method Summary
 void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
          Process the request.
protected  Logger getLogger(Throwable t)
          Get the logging destination.
protected  void logException(Throwable t)
          Log an exception caught by this instance.
protected  boolean shouldLogException(Throwable t)
          Determine if an exception caught by this instance should be logged.
 
Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, getAlreadyFilteredAttributeName, shouldNotFilter
 
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setServletContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExceptionLoggingFilter

public ExceptionLoggingFilter()
Method Detail

doFilterInternal

public void doFilterInternal(HttpServletRequest request,
                             HttpServletResponse response,
                             FilterChain filterChain)
                      throws IOException,
                             ServletException
Process the request. If an exception is thrown, it will be (possibly) logged and re-thrown.

Specified by:
doFilterInternal in class OncePerRequestFilter
Throws:
IOException
ServletException
See Also:
shouldLogException(java.lang.Throwable)

shouldLogException

protected boolean shouldLogException(Throwable t)
Determine if an exception caught by this instance should be logged.

The implementation in ExceptionLoggingFilter returns true except for SocketException (typically caused by the client disconnecting) and ThreadDeath (typically caused by virtual machine shutdown). Subclasses should override if necessary.

Parameters:
t - exception caught by this instance

logException

protected void logException(Throwable t)
Log an exception caught by this instance.

The implementation in ExceptionLoggingFilter logs the exception as an error via the logger returned by getLogger(). Subclasses should override if necessary.

Parameters:
t - exception caught by this instance

getLogger

protected Logger getLogger(Throwable t)
Get the logging destination.

The implementation in ExceptionLoggingFilter uses the Logger returned by LoggerFactory.getLogger(java.lang.String) when passed this instance's class as the parameter. Subclasses should override if necessary.

Parameters:
t - the exception about to be logged