Class LoggingHttpClient

java.lang.Object
dev.langchain4j.http.client.log.LoggingHttpClient
All Implemented Interfaces:
HttpClient

public class LoggingHttpClient extends Object implements HttpClient
An HttpClient decorator that logs requests and responses.

Streaming response logging is blocking IO on the delivery thread. For the streaming paths (execute(HttpRequest, ServerSentEventListener), execute(HttpRequest, ServerSentEventParser, ServerSentEventListener) and especially stream(HttpRequest, ServerSentEventParser)), each server-sent event is logged synchronously on the thread that delivers it — for the publisher path this is the underlying HTTP client's non-blocking worker thread. Whether that log call actually blocks is decided by the logging backend's appender, which this client does not control: a synchronous appender (e.g. a plain file/console appender) performs a blocking write on the worker thread, which under load can stall the worker and collapse streaming throughput. Therefore, when logResponses is enabled for streaming, configure an asynchronous appender (e.g. Logback AsyncAppender, Log4j2 async loggers, tinylog writingthread=true) so log writes happen off the delivery thread.

  • Constructor Details

    • LoggingHttpClient

      public LoggingHttpClient(HttpClient delegateHttpClient, Boolean logRequests, Boolean logResponses)
    • LoggingHttpClient

      public LoggingHttpClient(HttpClient delegateHttpClient, Boolean logRequests, Boolean logResponses, org.slf4j.Logger logger)
  • Method Details

    • execute

      public SuccessfulHttpResponse execute(HttpRequest request) throws HttpException
      Description copied from interface: HttpClient
      Executes a given HTTP request synchronously and returns the response. This method blocks until the entire response is received.
      Specified by:
      execute in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      Returns:
      a SuccessfulHttpResponse containing the response data for successful HTTP requests (2XX status codes)
      Throws:
      HttpException - if the server returns a client (4XX) or server (5XX) error response
    • executeAsync

      public CompletableFuture<SuccessfulHttpResponse> executeAsync(HttpRequest request)
      Description copied from interface: HttpClient
      Non-blocking counterpart of HttpClient.execute(HttpRequest). Returns immediately with a CompletableFuture that completes with the SuccessfulHttpResponse once the full response has been received, without blocking the calling thread. The future completes exceptionally with an HttpException for non-2XX responses, or with the underlying error (e.g. a timeout or network failure) otherwise.
      Specified by:
      executeAsync in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      Returns:
      a CompletableFuture of the SuccessfulHttpResponse.
    • execute

      public void execute(HttpRequest request, ServerSentEventListener delegateListener)
      Description copied from interface: HttpClient
      Executes a given HTTP request asynchronously with server-sent events (SSE) handling. This method returns immediately while processing continues on a separate thread. Events are processed through the provided ServerSentEventListener.

      The execution flow is as follows:

      1. The request is initiated asynchronously
      2. Received SSE data is parsed using the DefaultServerSentEventParser
      3. Parsed events are delivered to the listener's appropriate methods
      4. If an error occurs, ServerSentEventListener.onError(Throwable) is called

      If any exception is thrown from the listener's methods, the stream processing will be terminated and no further events will be processed.

      Specified by:
      execute in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      delegateListener - the listener to receive parsed events and error notifications.
    • execute

      public void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener delegateListener)
      Description copied from interface: HttpClient
      Executes a given HTTP request asynchronously with server-sent events (SSE) handling. This method returns immediately while processing continues on a separate thread. Events are processed through the provided ServerSentEventListener.

      The execution flow is as follows:

      1. The request is initiated asynchronously
      2. Received SSE data is parsed using the provided parser
      3. Parsed events are delivered to the listener's appropriate methods
      4. If an error occurs, ServerSentEventListener.onError(Throwable) is called

      If any exception is thrown from the listener's methods, the stream processing will be terminated and no further events will be processed.

      Specified by:
      execute in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      parser - the parser to process incoming server-sent events.
      delegateListener - the listener to receive parsed events and error notifications.
    • stream

      Like HttpClient.stream(HttpRequest), but with a caller-supplied ServerSentEventParser.

      When logResponses is enabled, each event is logged in onNext, i.e. synchronously on the upstream's delivery (worker) thread. See the class-level note: use an asynchronous appender so this logging does not perform blocking IO on that non-blocking thread.

      Specified by:
      stream in interface HttpClient