Java 8 Servlet filter for logging requests and responses
Web page equivalent is here
Filter implements javax.servlet.Filter from Serlvet API 3.1.0 You can register the filter using web.xml descriptor.
<filter>
<filter-name>LoggingFilter</filter-name>
<filter-class>javax.servlet.filter.logging.LoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
or javax.servlet.ServletContext
public void onStartup(ServletContext servletContext) throws ServletException {
Dynamic registration = servletContext.addFilter("LoggingFilter", new LoggingFilter());
registration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
}
Name | Default | Description |
---|---|---|
loggerName | class name | Logger name for output |
maxContentSize | 1024 bytes | Maximal logged body size in bytes |
excludedPaths | empty | Comma sepparated list of URL prefixes e.g.: "/api,/admin" |
requestPrefix | REQUEST: | First word on request output line |
responsePrefix | RESPONSE: | First word on response output line |
There are few methods for rewrite if you want:
javax.servlet.filter.logging.LoggingFilter.doFilter
javax.servlet.filter.logging.LoggingFilter.getRequestDescription
javax.servlet.filter.logging.LoggingFilter.getResponseDescription
REQUEST:{"sender": "127.0.0.1", "method": "GET", "path": "http://localhost:8080/test", "params": {"param1": "1000"}, "headers": {"Accept": "application/json", "Content-Type":"text/plain"}, "body": "Test request body"}
RESPONSE: {"status":200,"headers":{"Content-Type":"text/plain"},"body":"Test response body"}