AccessLogFilter implements Filter { @Override public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException { try { String accessLogKey = invoker.getUrl().getParameter(ACCESS_LOG_KEY); if (ConfigUtils.isNotEmpty(accessLogKey)) { AccessLogData logData = buildAccessLogData(invoker, inv); log(accessLogKey, logData); } } catch (Throwable t) { logger.warn("Exception in AccessLogFilter of service(" + invoker + " -> " + inv + ")", t); } return invoker.invoke(inv); } AccessLogFilter(); @Override Result invoke(Invoker<?> invoker, Invocation inv);  }
@Test public void testInvokeException() { Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(null); Invocation invocation = new MockInvocation(); LogUtil.start(); accessLogFilter.invoke(invoker, invocation); assertEquals(1, LogUtil.findMessage("Exception in AccessLogFilter of service")); LogUtil.stop(); }
@Test @SuppressWarnings("unchecked") public void testDefault() throws NoSuchFieldException, IllegalAccessException { URL url = URL.valueOf("test: Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(url); Invocation invocation = new MockInvocation(); Field field = AccessLogFilter.class.getDeclaredField("LOG_ENTRIES"); field.setAccessible(true); assertTrue(((Map) field.get(AccessLogFilter.class)).isEmpty()); accessLogFilter.invoke(invoker, invocation); Map<String, Set<AccessLogData>> logs = (Map<String, Set<AccessLogData>>) field.get(AccessLogFilter.class); assertFalse(logs.isEmpty()); assertFalse(logs.get("true").isEmpty()); AccessLogData log = logs.get("true").iterator().next(); assertEquals("org.apache.dubbo.rpc.support.DemoService", log.getServiceName()); }
@Test public void testCustom() { URL url = URL.valueOf("test: Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(url); Invocation invocation = new MockInvocation(); accessLogFilter.invoke(invoker, invocation); }