Skip to content

Instantly share code, notes, and snippets.

@cmicali
Created February 26, 2014 13:54
Show Gist options
  • Save cmicali/9229807 to your computer and use it in GitHub Desktop.
Save cmicali/9229807 to your computer and use it in GitHub Desktop.
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