RequestContextExportingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> implements AppenderAttachable<ILoggingEvent> { public void addBuiltIn(BuiltInProperty property) { ensureNotStarted(); builder.builtIn(property); }  void addBuiltIn(BuiltInProperty property); void addAttribute(String alias, AttributeKey<?> attrKey); void addAttribute(String alias, AttributeKey<?> attrKey, Function<?, String> stringifier); void addRequestHeader(CharSequence name); void addResponseHeader(CharSequence name); void setExport(String mdcKey); void setExports(String mdcKeys); @Override void start(); @Override void stop(); @Override void addAppender(Appender<ILoggingEvent> newAppender); @Override Iterator<Appender<ILoggingEvent>> iteratorForAppenders(); @Override Appender<ILoggingEvent> getAppender(String name); @Override boolean isAttached(Appender<ILoggingEvent> appender); @Override void detachAndStopAllAppenders(); @Override boolean detachAppender(Appender<ILoggingEvent> appender); @Override boolean detachAppender(String name);  }
@Test void testMdcPropertyPreservation() throws Exception { final List<ILoggingEvent> events = prepare(a -> a.addBuiltIn(BuiltInProperty.REQ_DIRECTION)); MDC.put("some-prop", "some-value"); final ServiceRequestContext ctx = newServiceContext("/foo", null); try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("req.direction", "INBOUND") .containsEntry("some-prop", "some-value") .hasSize(2); } finally { MDC.remove("some-prop"); } }
@Test void testServiceContextWithoutLogs() throws Exception { final List<ILoggingEvent> events = prepare(a -> { for (BuiltInProperty p : BuiltInProperty.values()) { a.addBuiltIn(p); } }); final ServiceRequestContext ctx = newServiceContext("/foo", null); try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "server.com") .containsEntry("local.ip", "5.6.7.8") .containsEntry("local.port", "8080") .containsEntry("remote.host", "client.com") .containsEntry("remote.ip", "1.2.3.4") .containsEntry("remote.port", "5678") .containsEntry("client.ip", "9.10.11.12") .containsEntry("req.direction", "INBOUND") .containsEntry("req.authority", "server.com:8080") .containsEntry("req.method", "GET") .containsEntry("req.path", "/foo") .containsEntry("scheme", "unknown+h2") .containsEntry("tls.session_id", "0101020305080d15") .containsEntry("tls.proto", "TLSv1.2") .containsEntry("tls.cipher", "some-cipher") .containsKey("req.id") .hasSize(16); } }
@Test void testServiceContextWithMinimalLogs() throws Exception { final List<ILoggingEvent> events = prepare(a -> { for (BuiltInProperty p : BuiltInProperty.values()) { a.addBuiltIn(p); } }); final ServiceRequestContext ctx = newServiceContext("/foo", "name=alice"); try (SafeCloseable ignored = ctx.push()) { final RequestLogBuilder log = ctx.logBuilder(); log.endRequest(); log.endResponse(); final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "server.com") .containsEntry("local.ip", "5.6.7.8") .containsEntry("local.port", "8080") .containsEntry("remote.host", "client.com") .containsEntry("remote.ip", "1.2.3.4") .containsEntry("remote.port", "5678") .containsEntry("client.ip", "9.10.11.12") .containsEntry("req.direction", "INBOUND") .containsEntry("req.authority", "server.com:8080") .containsEntry("req.name", "GET") .containsEntry("req.service_name", ctx.config().service().getClass().getName()) .containsEntry("req.method", "GET") .containsEntry("req.path", "/foo") .containsEntry("req.query", "name=alice") .containsEntry("scheme", "none+h2") .containsEntry("req.content_length", "0") .containsEntry("res.status_code", "0") .containsEntry("res.content_length", "0") .containsEntry("tls.session_id", "0101020305080d15") .containsEntry("tls.proto", "TLSv1.2") .containsEntry("tls.cipher", "some-cipher") .containsKey("elapsed_nanos") .containsKey("req.id") .hasSize(23); } }
@Test void testClientContextWithMinimalLogs() throws Exception { final List<ILoggingEvent> events = prepare(a -> { for (BuiltInProperty p : BuiltInProperty.values()) { a.addBuiltIn(p); } }); final ClientRequestContext ctx = newClientContext("/foo", "type=bar"); try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("local.host", "client.com") .containsEntry("local.ip", "5.6.7.8") .containsEntry("local.port", "5678") .containsEntry("remote.host", "server.com") .containsEntry("remote.ip", "1.2.3.4") .containsEntry("remote.port", "8080") .containsEntry("req.direction", "OUTBOUND") .containsEntry("req.authority", "server.com:8080") .containsEntry("req.method", "GET") .containsEntry("req.path", "/foo") .containsEntry("req.query", "type=bar") .containsEntry("scheme", "unknown+h2") .containsEntry("tls.session_id", "0101020305080d15") .containsEntry("tls.proto", "TLSv1.2") .containsEntry("tls.cipher", "some-cipher") .containsKey("req.id") .hasSize(16); } }