Skip to content

Commit

Permalink
GEODE-7640: allow tests to turn on management request/response logging (
Browse files Browse the repository at this point in the history
  • Loading branch information
jinmeiliao authored and mhansonp committed Mar 12, 2020
1 parent 3afb970 commit 10295bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Expand Up @@ -58,6 +58,11 @@
public class LocatorStarterRule extends MemberStarterRule<LocatorStarterRule> implements Locator {
private transient InternalLocator locator;

public LocatorStarterRule() {
// in test environment, enable management request/response logging
withSystemProperty("geode.management.request.logging", "true");
}

@Override
public void before() {
super.before();
Expand Down
Expand Up @@ -31,13 +31,16 @@

public class ManagementLoggingFilter extends OncePerRequestFilter {

private static final Boolean ENABLE_REQUEST_LOGGING =
Boolean.parseBoolean(System.getProperty("geode.management.request.logging", "false"));

private static int MAX_PAYLOAD_LENGTH = 10000;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {

if (!logger.isDebugEnabled()) {
if (!logger.isDebugEnabled() && !ENABLE_REQUEST_LOGGING) {
filterChain.doFilter(request, response);
return;
}
Expand Down Expand Up @@ -70,8 +73,17 @@ private void logRequest(HttpServletRequest request, ContentCachingRequestWrapper
}
String payload = getContentAsString(wrappedRequest.getContentAsByteArray(),
wrappedRequest.getCharacterEncoding());
logger.debug(String.format(requestPattern, request.getMethod(), requestUrl,
request.getRemoteUser(), payload));
String message = String.format(requestPattern, request.getMethod(), requestUrl,
request.getRemoteUser(), payload);
logMessage(message);
}

private void logMessage(String message) {
if (ENABLE_REQUEST_LOGGING) {
logger.info(message);
} else {
logger.debug(message);
}
}

private void logResponse(HttpServletResponse response,
Expand All @@ -80,8 +92,9 @@ private void logResponse(HttpServletResponse response,
String responsePattern = "Management Response: Status=%s; response=%s";
String payload = getContentAsString(wrappedResponse.getContentAsByteArray(),
wrappedResponse.getCharacterEncoding());
logger.debug(String.format(responsePattern, response.getStatus(),
ManagementControllerAdvice.removeClassFromJsonText(payload)));
String message = String.format(responsePattern, response.getStatus(),
ManagementControllerAdvice.removeClassFromJsonText(payload));
logMessage(message);
}

private String getContentAsString(byte[] buf, String encoding) {
Expand Down

0 comments on commit 10295bb

Please sign in to comment.