A Spring MVC request and response body capturing, error logging servlet filter.
Helps diagnose why that 500 internal server error happened.
compile 'com.ctzen:servlet-error-logger:1.0'
which in turn requires ctzen-common, and ctzen-test. The latter is required to unit-test this project.
Install a DelegatingFilterProxy which points to a ErrorLoggingFilter
.
@Bean
public ErrorLoggingFilter errorLoggingFilter(
ServletToStringUtil servletToStringUtil,
ErrorLogger errorLogger) {
Set<Integer> responseStatusCodesToLog = new HashSet<Integer>();
responseStatusCodesToLog.add(HttpStatus.INTERNAL_SERVER_ERROR.value());
ExceptionGetter exceptionGetter =
new RequestAttributeExceptionGetter(DispatcherServlet.EXCEPTION_ATTRIBUTE);
return new ErrorLoggingFilter(
responseStatusCodesToLog,
exceptionGetter,
servletToStringUtil,
errorLogger);
}
@Bean
public ServletToStringUtil servletToStringUtil() {
Set<String> requestAttributeFilter = new HashSet<String>();
Set<String> sessionAttributeFilter = new HashSet<String>();
return new ServletToStringUtil(requestAttributeFilter, sessionAttributeFilter);
}
@Bean
FileErrorLogger errorLogger() {
new FileErrorLogger(new File('/var/log/webapp-errors'))
}
The ErrorLoggingFilter
wraps the request and response with a version capbable of capturing the bodies, chain, and if the response status code is one of responseStatusCodesToLog
(typically 500), builds and log the request, response, session, and any exception thrown. Any uncaught exception during chaining also triggers the logging action.
See the unit test sources for usage patterns.