Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mulesoft-json-logger

Reusable JSON logging library for Spring Boot and MuleSoft. Built on Logback with a chain-of-responsibility enricher pattern.


Quick Start

1. Build & install to local Maven repo

mvn clean install

2. Add dependency to your Spring Boot project

<dependency>
  <groupId>com.viz.logger</groupId>
  <artifactId>mulesoft-json-logger</artifactId>
  <version>1.0.0</version>
</dependency>

3. Copy logback-spring.xml to your app's src/main/resources/

The file is included in this project under src/main/resources/logback-spring.xml.


What's inside

Class Responsibility
JsonLogEncoder Logback encoder — converts event → JSON bytes
FieldExtractor Base fields: @timestamp, level, thread, logger, message
MdcEnricher MDC map → top-level JSON fields
ExceptionEnricher Throwable → { error: { type, message, stacktrace } }
StaticFieldsEnricher appName, env, hostname, pid
PiiMaskingEnricher Masks email, phone, credit card in all string fields
MdcRequestFilter Spring HTTP filter — populates MDC per request
MdcTaskDecorator Spring TaskDecorator — propagates MDC to @Async tasks
LogContextBuilder Fluent API for building MDC context

logback-spring.xml configuration

<encoder class="com.viz.logger.encoder.JsonLogEncoder">
  <appName>order-service</appName>
  <environment>prod</environment>
  <maskPii>true</maskPii>
  <includeAllMdc>true</includeAllMdc>
</encoder>

LogContextBuilder — fluent MDC API

try (LogContext ctx = LogContextBuilder.forRequest(httpRequest)
        .userId("42")
        .orderId("777")
        .service("order-service")
        .build()) {

    log.info("This line has full MDC context in JSON");
}
// MDC.clear() is called automatically

Sample JSON output

{
  "@timestamp": "2025-04-22T10:15:33.421Z",
  "level":      "INFO",
  "levelValue": 20000,
  "thread":     "http-nio-8080-exec-3",
  "logger":     "com.example.OrderService",
  "message":    "Order created",
  "requestId":  "abc-123",
  "traceId":    "550e8400-e29b-41d4-a716-446655440000",
  "userId":     "42",
  "method":     "POST",
  "uri":        "/api/v1/orders",
  "appName":    "order-service",
  "env":        "prod",
  "hostname":   "prod-node-42",
  "pid":        12345
}

About

Custom JSON logging library with MDC support for Spring Boot and MuleSoft

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages