Class HttpLogger

java.lang.Object
org.microproxy.extras.HttpLogger
All Implemented Interfaces:
HttpFiltersSource

public final class HttpLogger extends Object implements HttpFiltersSource
Logs whole requests and responses for debugging and audits, like OkHttp's HttpLoggingInterceptor or a mitmproxy flow dump. It is a filters source; add it with bootstrap.plusFiltersSource(logger), in an HttpFiltersChain, or inside built filters with HttpFiltersBuilder.log(org.microproxy.extras.HttpLogger):

 HttpLogger logger = HttpLogger.builder()
         .level(HttpLogger.Level.HEADERS)
         .redactQueryParams("token", "api_key")
         .build();
 MicroProxy.bootstrap().plusFiltersSource(logger).start();
 

For each exchange it logs the request as the client sent it, with the changes made before it was forwarded (by the proxy and by filters) when there are any, then the response as the server sent it, with the changes made before it was delivered, the ResponseSource, the server's status (FlowContext.upstreamStatus()) and the timings (FlowContext.timings()). Each message is one string handed to the sink, every line starting with [conn <id> #<n>]: the client connection, as in the proxy's own log lines, and the exchange's number on it. Responses are logged once they have been delivered in full.

HttpLogger.Level.BASIC and HttpLogger.Level.HEADERS look only at heads, so bodies keep the proxy's fast path. HttpLogger.Level.BODY reads bodies piece by piece and keeps at most HttpLogger.Builder.maxBodyBytes of each; it never changes what is forwarded. Bodies are decoded ( gzip, deflate, br, zstd, see HttpBodies) when the whole body was kept, and shown as text in their charset, or summarised when they are binary. With HttpLogger.Builder.contentViews(org.microproxy.contentviews.ContentViews) they are rendered through ContentViews instead where a view applies: protobuf and gRPC (message by message) as YAML with field numbers, JSON indented, form data, GraphQL, msgpack. Trailers are shown after the body, and gRPC's status fields explained.

The values of Authorization, Cookie, Set-Cookie and Proxy-Authorization are replaced with "\u2588\u2588" unless HttpLogger.Builder.redactNothing() says otherwise. Bodies are not redacted: HttpLogger.Level.BODY logs payloads, with whatever secrets and personal data they carry.

Lines go to the System.Logger named "org.microproxy.http" at INFO unless a HttpLogger.Builder.sink is given; when that logger is off, the filters are not even created. A sink that throws is reported once and never disturbs the proxy.

  • Field Details

  • Method Details

    • builder

      public static HttpLogger.Builder builder()
      Starts a logger at HttpLogger.Level.HEADERS, writing HttpLogger.Format.TEXT with the default redaction.
      Returns:
      a new builder with default settings
    • level

      public HttpLogger.Level level()
      Returns the configured logging detail.
      Returns:
      the configured logging detail
    • format

      public HttpLogger.Format format()
      Returns the configured log output format.
      Returns:
      the configured log output format
    • redactedHeaders

      public Set<String> redactedHeaders()
      Returns the redacted header names, in lower case.
      Returns:
      the redacted header names, in lower case
    • redactedQueryParams

      public Set<String> redactedQueryParams()
      Returns the redacted query parameter names, in lower case.
      Returns:
      the redacted query parameter names, in lower case
    • maxBodyBytes

      public int maxBodyBytes()
      Returns the maximum body bytes retained per log entry.
      Returns:
      the maximum body bytes retained per log entry
    • contentViews

      public ContentViews contentViews()
      Returns the content views bodies are rendered with, or null when they are not.
      Returns:
      the content views bodies are rendered with, or null when they are not
    • contentView

      public String contentView()
      Returns the content view bodies are rendered with ("auto" for the best one).
      Returns:
      the content view bodies are rendered with ("auto" for the best one)
    • logsWebSocketFrames

      public boolean logsWebSocketFrames()
      Returns whether WebSocket frames are logged (at HttpLogger.Level.BODY).
      Returns:
      whether WebSocket frames are logged (at HttpLogger.Level.BODY)
    • filterRequest

      public SelectiveFilters filterRequest(HttpRequest originalRequest, FlowContext flowContext)
      Filters that log this exchange, or null when it is not logged: the HttpLogger.Builder.only predicate declined it, or the default System.Logger is off at INFO.
      Specified by:
      filterRequest in interface HttpFiltersSource
      Parameters:
      originalRequest - a copy of the request as received from the client
      flowContext - the client connection the request arrived on
      Returns:
      the per-request filters, or null to leave the request unfiltered
    • toString

      public String toString()
      Overrides:
      toString in class Object