Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Latest commit

 

History

History
61 lines (54 loc) · 2.55 KB

README.md

File metadata and controls

61 lines (54 loc) · 2.55 KB

Servlet logging filter

Java 11 Servlet filter for logging requests and responses

Web page equivalent is here

Usage

Filter implements javax.servlet.Filter from Servlet API 4.0.1 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, "/*");
}

Init params

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
requestMarker RESPONSE Slf4J marker for request
responseMarker RESPONSE Slf4J marker for response
disablePrefix false No prefixes are logged if true
disableMarkers false No Slf4J markers are logged if true

Customization

There are few methods for rewrite if you want:

Main filter processing

javax.servlet.filter.logging.LoggingFilter.doFilter

Creating description of request. Default is create JSON object.

javax.servlet.filter.logging.LoggingFilter.getRequestDescription

Creating description of response. Default is create JSON object.

javax.servlet.filter.logging.LoggingFilter.getResponseDescription

Output

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"}