Skip to content
 
 

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WireMock Structured JSON Logging Extension

Java

Overview

This is a custom extension for WireMock that intercepts all served requests and logs them to the console (System.out) as a single-line, structured JSON object. It's designed to be a drop-in solution for feeding WireMock traffic into log aggregation systems like Logstash, Fluentd, Splunk, or an ELK/OpenSearch stack.

The extension provides rich, parsable information about each transaction while intelligently sanitizing large or binary data to keep logs clean and efficient.

Key Features

  • Structured JSON Logging: Every request/response event is logged as a single, well-formed JSON line.
  • Single-Line Output: Perfect for log shippers and aggregators that work best with one event per line.
  • Graceful Handling of Binary Payloads: Automatically detects binary request bodies (e.g., multipart/form-data, application/pdf) and replaces them with a placeholder, preventing log corruption.
  • Automatic Sanitization of Large Base64 Fields: Recursively scans JSON response bodies and replaces the value of any field that contains a long Base64 string with a placeholder. This is crucial for preventing multi-megabyte logs from file downloads.
  • Configurable Threshold: Avoids sanitizing short, valid Base64 strings (like tokens) by using a minimum length threshold.
  • Rich Metadata: Captures essential information, including timestamp, request details (method, URL, IP, headers, body), and response details (status, headers, body).

Why Use This Extension?

Default WireMock logging is verbose, multi-line, and not structured, making it difficult to parse automatically. This extension solves that problem by providing clean, machine-readable output out-of-the-box, without requiring complex multi-line parsing rules in your log aggregator.

Example Log Output

Here is an example of a log entry generated by the extension. Note how the Base64 content in the response body has been automatically sanitized.

{
  "@timestamp": "2025-11-06T10:30:00.123Z",
  "service": "wiremock",
  "wasMatched": true,
  "requestId": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
  "request": {
    "method": "POST",
    "url": "http://localhost:9119/auxi/downloadSign",
    "clientIp": "127.0.0.1",
    "headers": {
      "Content-Type": "application/json; charset=UTF-8"
    },
    "body": "{ \"idPF\": \"vbak1ds48m\"}"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json;charset=UTF-8"
    },
    "body": "{\"documentoFirmadoBase64\":\"<base64_data_omitted>\"}"
  }
}

Installation & Usage

  1. Build the Project Build the extension from the source to create the required uber-JAR.

    mvn clean package
  2. Copy the JAR Copy the generated JAR from the target directory (wiremock-log-extension-1.0.0-SNAPSHOT.jar) into your WireMock extensions directory.

  3. Start WireMock Start WireMock from the command line, telling it to load the extension.

    java -jar wiremock-standalone-3.13.1.jar --extensions dev.bernalvarela.wiremock.extensions.StructuredJsonLoggingListener

Now, all requests served by WireMock will be logged to the console in the structured JSON format.

Building from Source

Prerequisites

  • Java 11 or higher
  • Apache Maven 3.6.x or higher

Build Command

Navigate to the project root and run:

mvn clean package

This will compile the code, run the unit tests, and create a self-contained "uber-JAR" in the target/ directory.

Configuration

Configuration is handled via private static final constants at the top of the StructuredJsonLoggingListener.java class. You can modify the source and rebuild the JAR to change behavior.

  • BASE64_MIN_LENGTH_THRESHOLD: The minimum character length for a string to be considered for Base64 sanitization. Defaults to 100.
  • BINARY_BODY_PLACEHOLDER: The placeholder text for binary request bodies. Defaults to "<binary content not logged>".
  • BASE64_PLACEHOLDER: The placeholder text for sanitized Base64 fields. Defaults to "<base64_data_omitted>".

Development

To run the unit tests, execute:

mvn test

About

This is a custom extension for WireMock that intercepts all served requests and logs them to the console (System.out) as a single-line, structured JSON object. It's designed to be a drop-in solution for feeding WireMock traffic into log aggregation systems like Logstash, Fluentd, Splunk, or an ELK/OpenSearch stack.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages