java-logging-patterns

Implements structured JSON logging with SLF4J and MDC request tracing in Java applications.

Updated Apr 20, 2020
One-click install
npx skills add https://github.com/UnterrainerInformatik/java-rdb-utils --skill java-logging-patterns-unterrainerinformatik
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: java-logging-patterns
Source: https://github.com/UnterrainerInformatik/java-rdb-utils/tree/main/.agents/skills/java-logging-patterns
Command: npx skills add https://github.com/UnterrainerInformatik/java-rdb-utils --skill java-logging-patterns-unterrainerinformatik

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires logstash-logback-encoder.

What problem does it solve? Java applications often produce unstructured text logs that are hard to filter, parse, and analyze, making debugging slow and preventing AI tools from efficiently reading application behavior. ## Core Features & Use Cases - Structured JSON Logging: Configure Spring Boot 3.4+ native structured logging or the Logstash Logback encoder for older versions, with profile-based switching between JSON and human-readable formats. - MDC Request Tracing: Add requestId and userId context to every log entry via servlet filters, including propagation to async threads. - AI-Friendly Log Analysis: Emit logs with fields like requestId, step, and duration_ms so Claude Code can filter them with jq queries to trace request flows and find slow operations. - Use Case: When a user reports a failing order, ask the AI to run jq 'select(.requestId == "abc123")' against the logs to reconstruct the exact flow steps and pinpoint where the error occurred. ## Quick Start Ask the AI to add structured JSON logging with MDC request tracing to your Spring Boot service using this skill.

Frequently Asked Questions about java-logging-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add structured JSON logging to a Spring Boot application?▼

In Spring Boot 3.4+, set logging.structured.format.console to logstash in application.yml with no extra dependencies. For older versions, add the logstash-logback-encoder dependency and configure a LogstashEncoder appender in logback-spring.xml.

How to trace requests across logs with MDC in Java?▼

Create a servlet filter that puts a requestId into MDC at the start of each request and clears it in a finally block. Every subsequent log entry automatically includes the requestId, letting you filter all logs for one request.

Does Spring Boot support structured logging natively?▼

Yes, Spring Boot 3.4 added native structured logging supporting logstash, ecs, and gelf formats through configuration only. Earlier versions require the logstash-logback-encoder library to produce JSON output.

Why does MDC context disappear in async threads?▼

MDC is thread-local, so it does not propagate to new threads automatically. Copy the context with MDC.getCopyOfContextMap() before submitting the async task, then call MDC.setContextMap() inside the new thread and clear it afterward.

What data should never be written to application logs?▼

Never log passwords, full card numbers, authentication tokens, or PII such as social security numbers. Log safe alternatives instead, like userId, the last four card digits, or token subject and expiry claims.