This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.web.filter.OncePerRequestFilter; | |
import org.springframework.web.util.ContentCachingRequestWrapper; | |
import org.springframework.web.util.ContentCachingResponseWrapper; | |
import javax.servlet.FilterChain; | |
import javax.servlet.ServletException; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2021-01-18 12:25:24 DEBUG RequestResponseBodyMethodProcessor:91 - Read "application/json;charset=ISO-8859-1" to [StudentData(firstName=John, lastName=Doe, grade=5)] | |
2021-01-18 12:25:24 DEBUG RequestResponseBodyMethodProcessor:265 - Using 'text/plain', given [*/*] and supported [text/plain, */*, application/json, application/*+json] | |
2021-01-18 12:25:24 DEBUG RequestResponseBodyMethodProcessor:91 - Writing ["You are great with your grade 5"] | |
2021-01-18 12:25:24 DEBUG DispatcherServlet:1131 - Completed 200 OK | |
2021-01-18 12:25:24 INFO LoggingFilter:104 - 0:0:0:0:0:0:0:1|> { | |
"firstName": "John", | |
"lastName": "Doe", | |
"grade": 5 | |
} | |
2021-01-18 12:25:24 INFO LoggingFilter:89 - 0:0:0:0:0:0:0:1|> 200 OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<filter> | |
<filter-name>Filter</filter-name> | |
<filter-class>ru.vrnsky.filter.LoggingFilter</filter-class> | |
</filter> | |
<filter-mapping> | |
<filter-name>Filter</filter-name> | |
<url-pattern>*</url-pattern> | |
</filter-mapping> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static ContentCachingRequestWrapper wrapRequest(HttpServletRequest httpServletRequest) { | |
if (httpServletRequest instanceof ContentCachingRequestWrapper) { | |
return (ContentCachingRequestWrapper) httpServletRequest; | |
} else { | |
return new ContentCachingRequestWrapper(httpServletRequest); | |
} | |
} | |
private static ContentCachingResponseWrapper wrapResponse(HttpServletResponse httpServletResponse) { | |
if (httpServletResponse instanceof ContentCachingResponseWrapper) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void logContent(byte[] content, String contentType, String contentEncoding, String prefix) { | |
MediaType mediaType = MediaType.valueOf(contentType); | |
boolean visible = VISIBLE_TYPES.stream().anyMatch(visibleType -> visibleType.includes(mediaType)); | |
if (visible) { | |
try { | |
String contentString = new String(content, contentEncoding); | |
Stream.of(contentString.split("\r\n|\r\n")).forEach(line -> logger.info("{} {}", prefix, line)); | |
} catch (UnsupportedEncodingException e) { | |
logger.info("{}, [{} bytes content]", prefix, content.length); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void logResponse(ContentCachingResponseWrapper response, String prefix) { | |
int status = response.getStatus(); | |
logger.info("{} {} {}", prefix, status, HttpStatus.valueOf(status).getReasonPhrase()); | |
response.getHeaderNames().forEach(header -> response.getHeaders(header).forEach(headerValue -> logger.info("{} {} {}", prefix, header, headerValue))); | |
logger.info("{}", prefix); | |
byte[] content = response.getContentAsByteArray(); | |
if (content.length > 0) { | |
logContent(content, response.getContentType(), response.getCharacterEncoding(), prefix); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void logRequestBody(ContentCachingRequestWrapper request, String prefix) { | |
byte[] content = request.getContentAsByteArray(); | |
if (content.length > 0) { | |
logContent(content, request.getContentType(), request.getCharacterEncoding(), prefix); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void logRequestHeader(ContentCachingRequestWrapper request, String prefix) { | |
String queryString = request.getQueryString(); | |
if (queryString == null) { | |
logger.info("{} {} {}", prefix, request.getMethod(), request.getRequestURI()); | |
} else { | |
logger.info("{} {} {}?{}", prefix, request.getMethod(), request.getRequestURI(), queryString); | |
} | |
Collections.list(request.getHeaderNames()).forEach(headerName -> | |
Collections.list(request.getHeaders(headerName)).forEach(headerValue -> logger.info("{} {} {}", prefix, headerName, headerValue))); | |
logger.info("{}", prefix); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void afterRequest(ContentCachingRequestWrapper request, ContentCachingResponseWrapper response) { | |
if (logger.isInfoEnabled()) { | |
logRequestBody(request, request.getRemoteAddr() + "|>"); | |
logResponse(response, request.getRemoteAddr() + "|>"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void beforeRequest(ContentCachingRequestWrapper request) { | |
if (logger.isInfoEnabled()) { | |
logRequestHeader(request, request.getRemoteAddr() + "|>"); | |
} | |
} |
NewerOlder