Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
add RequestId in MDC logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jackjii79 committed Jun 29, 2023
1 parent c0433f7 commit 92b12ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Optional;
import java.util.UUID;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -33,7 +34,9 @@ protected void doFilterInternal(
MDC.MDCCloseable modelId = MDC.putCloseable(
ScorerJsonLayout.EXPERIMENT_ID, getFromEnvironment(MODEL_ID));
MDC.MDCCloseable scorerType =
MDC.putCloseable(ScorerJsonLayout.SCORER_TYPE, getScorerType()); ) {
MDC.putCloseable(ScorerJsonLayout.SCORER_TYPE, getScorerType());
MDC.MDCCloseable requestId =
MDC.putCloseable(ScorerJsonLayout.REQUEST_ID, UUID.randomUUID().toString());) {
filterChain.doFilter(request, response);
if (HttpServletResponse.SC_OK == response.getStatus()) {
try (MDC.MDCCloseable responseCode = MDC.putCloseable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ScorerJsonLayout extends JsonLayout {
public static final String SCORER_TYPE = "ScorerType";
public static final String TIMESTAMP = "Timestamp";
public static final String LOG_LEVEL = "LogLevel";
public static final String REQUEST_ID = "RequestId";

public ScorerJsonLayout() {
super();
Expand All @@ -36,6 +37,7 @@ protected Map toJsonMap(ILoggingEvent event) {
add(EXPERIMENT_ID, containsMdc(event, EXPERIMENT_ID), getFromMdc(event, EXPERIMENT_ID), map);
add(NUM_ROWS, containsMdc(event, NUM_ROWS), getFromMdc(event, NUM_ROWS), map);
add(RESPONSE_CODE, containsMdc(event, RESPONSE_CODE), getFromMdc(event, RESPONSE_CODE), map);
add(REQUEST_ID, containsMdc(event, REQUEST_ID), getFromMdc(event, REQUEST_ID), map);
addCustomDataToJsonMap(map, event);
return map;
}
Expand Down

0 comments on commit 92b12ef

Please sign in to comment.