LoggingFilter implements Filter { @Override @SuppressWarnings("unchecked") public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { long startTime = System.currentTimeMillis(); TeeHttpServletRequest req = new TeeHttpServletRequest( (HttpServletRequest) request); TeeHttpServletResponse resp = new TeeHttpServletResponse( (HttpServletResponse) response); try { MDC.put(LoggingUtil.MDC_REQUEST_TYPE_KEY, "req"); String requestUUID = UUID.randomUUID().toString(); MDC.put(LoggingUtil.MDC_REQUEST_UUID_KEY, requestUUID); String correlationId = null; Enumeration<String> headerNames = (Enumeration<String>) ((HttpServletRequest) request) .getHeaderNames(); while (headerNames.hasMoreElements()) { String name = (String) headerNames.nextElement(); if ("X-Correlation-ID".equalsIgnoreCase(name)) { correlationId = ((HttpServletRequest) request).getHeader(name); } } if (correlationId != null) { if (CSID_REGEX.matcher(correlationId).matches()) { if (correlationId.length() > CSID_MAX_LENGTH) { log.warn("Correlation Id {} has more characters than {}, hence truncating to {}", correlationId, CSID_MAX_LENGTH, CSID_MAX_LENGTH); correlationId = correlationId.substring(0, CSID_MAX_LENGTH); } MDC.put(LoggingUtil.MDC_CSID_KEY, correlationId); } else { log.warn("Correlation Id must consist of alphanumeric characters or hypens"); } } req.setAttribute("requestUuid", requestUUID); resp.setHeader(customHeaderName, requestUUID); if (log.isDebugEnabled()) { log.debug("{}", ServletLogger.logRequest(req)); } else { log.info("{}", ServletLogger.logBasicRequestInfo(req)); } chain.doFilter(req, resp); if (log.isDebugEnabled()) { log.debug("{}", ServletLogger.logResponse(resp, startTime)); } else { log.info("{}", ServletLogger.logBasicResponseInfo(resp, startTime)); } resp.finish(); } finally { MDC.clear(); } }  @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override @SuppressWarnings("unchecked") void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain);  }
@Test public void testDoFilterDebugOff() throws Exception { filterlogger.setLevel(Level.WARN); filter.doFilter(request, response, chain); verify(mockapp, never()).doAppend(null); }
@Test public void testWithHeaders() throws Exception { ArgumentCaptor<LoggingEvent> message = ArgumentCaptor.forClass(LoggingEvent.class); when(headernames.hasMoreElements()).thenReturn(Boolean.TRUE) .thenReturn(Boolean.FALSE); when(headernames.nextElement()).thenReturn("Accept"); when(request.getHeaderNames()).thenReturn(headernames); filter.doFilter(request, response, chain); verify(mockapp, atLeastOnce()).doAppend(message.capture()); }
@Test public void testWithQueryString() throws Exception { when(request.getQueryString()).thenReturn("foo=bar"); ArgumentCaptor<LoggingEvent> message = ArgumentCaptor.forClass(LoggingEvent.class); filter.doFilter(request, response, chain); verify(mockapp, atLeastOnce()).doAppend(message.capture()); }
@Test public void testDoFilter() throws Exception { filterlogger.setLevel(Level.DEBUG); final ByteArrayInputStream bais = new ByteArrayInputStream("this is my body".getBytes()); when(request.getInputStream()).thenReturn(new ServletInputStream() { @Override public int read() throws IOException { return bais.read(); } }); ArgumentCaptor<LoggingEvent> message = ArgumentCaptor.forClass(LoggingEvent.class); filter.doFilter(request, response, chain); verify(mockapp, atLeastOnce()).doAppend(message.capture()); }