Add response status logging for each request.
@@ -5,6 +5,10 @@ import java.util.UUID;
import javax.ws.rs.container.CompletionCallback;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
+import javax.ws.rs.core.Response;
+import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
/**
@@ -12,7 +16,9 @@ import org.slf4j.MDC;
* logs throughout the request context. Request ID is randomly generated UUID, which can be used to
* group by to see the logs for a particular request.
*/
-public class LoggingMdcFilter implements ContainerRequestFilter, CompletionCallback {
+@Slf4j
+public class LoggingMdcFilter
+ implements ContainerRequestFilter, ContainerResponseFilter, CompletionCallback {
private static final String REQUEST_ID = "requestID";
private static final String METHOD = "method";
@@ -31,4 +37,17 @@ public class LoggingMdcFilter implements ContainerRequestFilter, CompletionCallb
MDC.put(METHOD, requestContext.getMethod());
MDC.put(PATH, requestContext.getUriInfo().getPath());
}
+
+ @Override
+ public void filter(
+ ContainerRequestContext requestContext, ContainerResponseContext responseContext)
+ throws IOException {
+ if (responseContext.getStatus() >= Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
+ log.error("status: {}", responseContext.getStatus());
+ } else if (responseContext.getStatus() >= Response.Status.BAD_REQUEST.getStatusCode()) {
+ log.warn("status: {}", responseContext.getStatus());
+ } else {
+ log.info("status: {}", responseContext.getStatus());
+ }
+ }
}
