TraceLoggingInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } if (logger.isTraceEnabled()) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method m = handlerMethod.getMethod(); logger.trace("[START CONTROLLER] {}.{}({})", new Object[] { m .getDeclaringClass().getSimpleName(), m.getName(), buildMethodParams(handlerMethod) }); } long startTime = System.nanoTime(); request.setAttribute(START_ATTR, startTime); return true; }  @Override boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler); @Override void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView); void setWarnHandlingNanos(long warnHandlingNanos);  }
@Test public void testPreHandle_LogOutput() throws Exception { HandlerMethod paramHandler = new HandlerMethod(controller, TraceLoggingInterceptorController.class .getMethod("createForm")); try { interceptor.preHandle(request, response, paramHandler); } catch (Exception e) { fail("illegal case"); } String logMessage = jdbcTemplate.queryForObject( "SELECT FORMATTED_MESSAGE FROM LOGGING_EVENT WHERE EVENT_ID=:id", Collections.singletonMap("id", 1), String.class); Long startTime = (Long) request.getAttribute( TraceLoggingInterceptor.class.getName() + ".startTime"); assertThat(logMessage, is( "[START CONTROLLER] TraceLoggingInterceptorController.createForm()")); assertThat(startTime, notNullValue()); }
@Test public void testPreHandle_ParamNotHandler() { Object paramHandler = new Object(); try { interceptor.preHandle(request, response, paramHandler); } catch (Exception e) { fail("illegal case"); } Long startTime = (Long) request.getAttribute( TraceLoggingInterceptor.class.getName() + ".startTime"); long count = jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM LOGGING_EVENT", Collections.singletonMap( "", ""), Long.class); assertThat(startTime, nullValue()); assertThat(count, is(0L)); }
@Test public void testPreHandleIsTraceEnabledFalse() throws Exception { LogLevelChangeUtil.setLogLevel(LogLevelChangeUtil.LogLevel.INFO); Object paramHandler = new Object(); try { interceptor.preHandle(request, response, paramHandler); } catch (Exception e) { fail("illegal case"); } Long startTime = (Long) request.getAttribute( TraceLoggingInterceptor.class.getName() + ".startTime"); long count = jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM LOGGING_EVENT", Collections.singletonMap( "", ""), Long.class); assertThat(startTime, nullValue()); assertThat(count, is(0L)); assertThat(logger.isDebugEnabled(), is(false)); LogLevelChangeUtil.resetLogLevel(); }