LoggingClient extends AbstractLoggingClient<HttpRequest, HttpResponse> implements HttpClient { public static LoggingClientBuilder builder() { return new LoggingClientBuilder(); } LoggingClient(
            HttpClient delegate,
            @Nullable Logger logger,
            Function<? super RequestOnlyLog, LogLevel> requestLogLevelMapper,
            Function<? super RequestLog, LogLevel> responseLogLevelMapper,
            BiFunction<? super RequestContext, ? super HttpHeaders, ?> requestHeadersSanitizer,
            BiFunction<? super RequestContext, Object, ?> requestContentSanitizer,
            BiFunction<? super RequestContext, ? super HttpHeaders, ?> requestTrailersSanitizer,
            BiFunction<? super RequestContext, ? super HttpHeaders, ?> responseHeadersSanitizer,
            BiFunction<? super RequestContext, Object, ?> responseContentSanitizer,
            BiFunction<? super RequestContext, ? super HttpHeaders, ?> responseTrailersSanitizer,
            BiFunction<? super RequestContext, ? super Throwable, ?> responseCauseSanitizer,
            Sampler<? super ClientRequestContext> sampler); static Function<? super HttpClient, LoggingClient> newDecorator(); static LoggingClientBuilder builder();  }
@Test void logger() throws Exception { final HttpRequest req = HttpRequest.of(HttpMethod.GET, "/"); final ClientRequestContext ctx = ClientRequestContext.of(req); final Logger logger = LoggingTestUtil.newMockLogger(ctx, capturedCause); when(logger.isInfoEnabled()).thenReturn(true); final LoggingClient customLoggerClient = LoggingClient.builder() .logger(logger) .requestLogLevel(LogLevel.INFO) .successfulResponseLogLevel(LogLevel.INFO) .build(delegate); customLoggerClient.execute(ctx, req); verify(logger, times(2)).isInfoEnabled(); verify(logger).info(eq("{} Request: {}"), eq(ctx), argThat((String actLog) -> actLog.endsWith("headers=[:method=GET, :path=/]}"))); verify(logger).info(eq("{} Response: {}"), eq(ctx), argThat((String actLog) -> actLog.endsWith("headers=[:status=0]}"))); verifyNoMoreInteractions(logger); clearInvocations(logger); final LoggingClient defaultLoggerClient = LoggingClient.builder() .requestLogLevel(LogLevel.INFO) .successfulResponseLogLevel(LogLevel.INFO) .build(delegate); defaultLoggerClient.execute(ctx, req); verifyNoInteractions(logger); }
@Test void sanitizeRequestHeaders() throws Exception { final HttpRequest req = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/hello/trustin", HttpHeaderNames.SCHEME, "http", HttpHeaderNames.AUTHORITY, "test.com")); final ClientRequestContext ctx = ClientRequestContext.of(req); final LoggingClient defaultLoggerClient = LoggingClient.builder() .requestLogLevel(LogLevel.INFO) .successfulResponseLogLevel(LogLevel.INFO) .requestHeadersSanitizer(RegexBasedSanitizer.of( Pattern.compile("trustin"), Pattern.compile("com"))) .build(delegate); assertThat(ctx.logBuilder().toString()).contains("trustin"); assertThat(ctx.logBuilder().toString()).contains("test.com"); defaultLoggerClient.execute(ctx, req); assertThat(ctx.logBuilder().toString()).doesNotContain("trustin"); assertThat(ctx.logBuilder().toString()).doesNotContain("com"); }
@Test void sanitizeRequestContent() throws Exception { final HttpRequest req = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/hello/trustin", HttpHeaderNames.SCHEME, "http", HttpHeaderNames.AUTHORITY, "test.com")); final ClientRequestContext ctx = ClientRequestContext.of(req); ctx.logBuilder().requestContent("Virginia 333-490-4499", "Virginia 333-490-4499"); final LoggingClient defaultLoggerClient = LoggingClient.builder() .requestLogLevel(LogLevel.INFO) .successfulResponseLogLevel(LogLevel.INFO) .requestContentSanitizer(RegexBasedSanitizer.of( Pattern.compile("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}"))) .build(delegate); assertThat(ctx.logBuilder().toString()).contains("333-490-4499"); defaultLoggerClient.execute(ctx, req); assertThat(ctx.logBuilder().toString()).doesNotContain("333-490-4499"); }
@Test void exceptionWhileLogging() throws Exception { final HttpRequest req = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/hello/trustin", HttpHeaderNames.SCHEME, "http", HttpHeaderNames.AUTHORITY, "test.com")); final ClientRequestContext ctx = ClientRequestContext.of(req); final Logger logger = LoggingTestUtil.newMockLogger(ctx, capturedCause); final LoggingClient loggingClient = LoggingClient.builder() .logger(logger) .requestLogLevelMapper(log -> { throw new AnticipatedException(); }) .responseLogLevelMapper(log -> { throw new AnticipatedException(); }) .build(delegate); loggingClient.execute(ctx, req); verify(logger).warn(eq("{} Unexpected exception while logging request: "), eq(ctx), any(CompletionException.class)); verify(logger).warn(eq("{} Unexpected exception while logging response: "), eq(ctx), any(CompletionException.class)); verifyNoMoreInteractions(logger); clearInvocations(logger); }