Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cemo
Forked from cmicali/CurrentUserLoggingFilter.java
Created February 26, 2014 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemo/9229951 to your computer and use it in GitHub Desktop.
Save cemo/9229951 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