Created
February 26, 2014 13:54
-
-
Save cmicali/9229807 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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