@Test public void testThreadContext() { final Logger logger = LogManager.getLogger("test"); ThreadContext.push("Message only"); ThreadContext.push("int", 1); ThreadContext.push("int-long-string", 1, 2l, "3"); ThreadContext.put("key", "value"); logger.info("Hello World"); ThreadContext.clearAll(); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { boolean clear = false; if (!ThreadContext.containsKey("id")) { clear = true; ThreadContext.put("id", UUID.randomUUID().toString()); HttpSession session = ((HttpServletRequest) request).getSession(false); if (session != null) { ThreadContext.put("username", (String) session.getAttribute("username")); } } try { chain.doFilter(request, response); } finally { if (clear) { ThreadContext.clearAll(); } } }