Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class CurrentUserLoggingFilter implements Filter {
public static final String MDC_CURRENT_ACCOUNT_ID_KEY = "CurrentAccountId";
@Context
HttpContext context;
@Override
public void init(FilterConfig filterConfig) throws ServletException { /* unused */ }
@Override
public void destroy() { /* unused */ }
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
try {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
// Get your auth ID however you need, from context or headers
Long userId = 1L;
if (userId != null) {
MDC.put(MDC_CURRENT_ACCOUNT_ID_KEY, userId);
}
filterChain.doFilter(request, response);
}
finally {
MDC.remove(MDC_CURRENT_ACCOUNT_ID_KEY);
}
}
}
logging:
level: INFO
loggers:
org.hibernate: WARN
org.hibernate.SQL: INFO
appenders:
- type: console
logFormat: "%-5p %date{HH:mm:ss} | %5.5mdc{OriginRequestId:-noreq} | %3.3mdc{CurrentAccountId:-na} | %48logger{48} | %m%n%xEx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment