Class AccessLogFormat

java.lang.Object
org.microproxy.extras.AccessLogFormat

public final class AccessLogFormat extends Object
An access-log line format, compatible with Envoy's access-log command operators: a format string such as [%START_TIME%] "%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%" %RESPONSE_CODE% %RESPONSE_FLAGS%, or a JSON dictionary whose values are format strings. The format is parsed once, into an array of pieces that each render one literal or one operator, so rendering a FlowRecord needs no parsing and no regular expressions.

An operator whose value is not available (a missing header, no upstream, a tunnel's response code details before it has any) renders -, as does an operator this class does not know. %OPERATOR(...):N% truncates the value to N characters. In text formats, control characters in values are escaped (\n, \t, ), so a value cannot break a line; %% is a literal %. In the JSON form, a value that is a single operator keeps the operator's type: numbers for codes, sizes and durations, true or false for flags, null when unavailable; other values are strings.

See the access-log format reference (docs/reference/access-log-format.md) for the operators.

  • Field Details

  • Method Details

    • envoy

      public static AccessLogFormat envoy(String format)
      Parses an Envoy format string. A trailing newline (Envoy's formats end with one) is dropped: the logger writes each line on its own.
      Parameters:
      format - the format string
      Returns:
      the format
    • envoyJson

      public static AccessLogFormat envoyJson(Map<String,?> dictionary)
      A JSON format: one JSON object per line, whose keys are those of dictionary (in its iteration order) and whose values are rendered from its values: a format string, a nested map, or a number, boolean or null written as it is.
      Parameters:
      dictionary - the keys and their format strings
      Returns:
      the format
    • envoyJson

      public static AccessLogFormat envoyJson(String dictionary)
      envoyJson(Map) from the JSON text of the dictionary, as the command line passes it: {"status":"%RESPONSE_CODE%","path":"%REQ(:PATH)%"}.
      Parameters:
      dictionary - a JSON object
      Returns:
      the format
      Throws:
      IllegalArgumentException - if the text is not a JSON object
    • isJson

      public boolean isJson()
      Returns whether this is a JSON format.
      Returns:
      whether this is a JSON format
    • format

      public String format(FlowRecord record)
      Renders one line for record.
      Parameters:
      record - the finished exchange or flow
      Returns:
      the line, without a line terminator
    • formatTo

      public void formatTo(FlowRecord record, StringBuilder out)
      Appends one line for record to out.
      Parameters:
      record - the finished exchange or flow
      out - where the line goes
    • toString

      public String toString()
      Overrides:
      toString in class Object