-
This will generate JSon-formatted logs using logstash-logback-encoder, which plugs into Logback (Spring Boot’s default logging framework). This enables logs to be machine-parsable JSON — perfect for tools like Loki, Elasticsearch, Prometheus (via exporters), or FluentBit. Example:
{ "@timestamp": "2025-10-02T13:45:00.123Z", "level": "INFO", "logger": "com.sztab.service.IssueService", "thread": "http-nio-8080-exec-3", "message": "Updated issue status to IN_PROGRESS", "context": "default", "mdc": { "userId": "rk@tigase.net", "requestId": "f3a9c8d0-4b5e-11ee-be56-0242ac120002", "projectId": "sztab-core" } }
-
Custom logback-spring.xml config:
<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <appender name="JSON_CONSOLE" class="net.logstash.logback.appender.LoggingEventCompositeJsonEncoder"> <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"> <providers> <timestamp> <fieldName>@timestamp</fieldName> </timestamp> <logLevel /> <loggerName /> <threadName /> <message /> <mdc /> </providers> </encoder> </appender> <root level="INFO"> <appender-ref ref="JSON_CONSOLE" /> </root> </configuration>
Type |
New Feature
|
Priority |
Normal
|
Assignee | |
Version |
none
|
Sprints |
n/a
|
Customer |
n/a
|
Issue Votes (0)
Enable Structured JSON Logging for Observability
Goal:
Configure structured, machine-readable logging in JSON format to support integration with observability tools (e.g., Prometheus, Grafana, Loki, ELK).
Estimated Time: 3–4 hours
Task Breakdown
1. Choose Logging Framework
logback
(default in Spring Boot) or switch tolog4j2
if neededlogstash-logback-encoder
)2. Configure JSON Log Output
net.logstash.logback:logstash-logback-encoder
logback-spring.xml
for JSON formattingtimestamp
,level
,logger
,message
,thread
, and MDC context (e.g., userId, requestId)3. Enable MDC Context Logging
HandlerInterceptor
orOncePerRequestFilter
to populate MDC with request metadata (e.g., userId, correlationId)4. Profile-Based Logging Levels
application-dev.yml
andapplication-prod.yml
overrides5. Output Verification
🕒 Estimated Time: 3–4 hours
Subtask Estimates
Output Artifacts
logback-spring.xml
with JSON encoderREADME.md