Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Your log platform requires JSON logs. Compare the options for a Spring Boot 3.4+ service: Spring Boot structured logging (logging.structured.format.console=ecs or logstash), the logstash-logback-encoder library, and Log4j2 JsonTemplateLayout. What fields would you add and how do you keep stack traces usable?

For a Spring Boot 3.4+ service, the built-in structured logging (logging.structured.format.console=ecs, logstash or gelf) is the simplest choice when its fields are enough; logstash-logback-encoder gives the most control on Logback, and Log4j2's JsonTemplateLayout is the equivalent on Log4j2. Whichever we pick, we add service, version, environment, trace and span ids and selected MDC keys, keep business values as structured fields rather than text, and put the stack trace in one size-limited field, root cause first.

Why this question is asked

Once a log platform requires JSON, the question stops being 'how do I print a line' and becomes 'what contract do my events follow'. A good choice here means dashboards and alerts work the same across every service; a poor one means each team invents its own field names and every query needs special cases. Interviewers ask this to see whether we know the three common tools, what each is good at, and how we keep stack traces useful rather than letting them explode into hundreds of events. In this answer, we will recap what structured logging means, compare the three options with configuration for each, decide which fields to add, look at structured arguments for business values, and finish with stack traces and a shared schema.

1A Quick Recap of Structured Logging

Structured logging means each log event is written as a set of named fields, usually one JSON object per line, instead of a sentence that a parser later has to cut apart. A log platform can then filter on level, service.name or order.id directly, without a regular expression. The message text is still there, but it is one field among many.

The schema is the agreed list of field names and their meaning. Elastic Common Schema (ECS) and the OpenTelemetry semantic conventions are two widely used schemas. Agreeing on one matters more than which one we pick, because a field called traceId in one service and trace_id in another breaks every cross-service query.

2Option 1: Spring Boot's Built-in Structured Logging

Spring Boot 3.4 added structured logging as a first-class feature. We set one property per output, and Boot writes JSON in the chosen format without any extra library. The supported formats in 3.4 are ecs, gelf and logstash, and later versions added more customisation; check your version for the exact list and properties.

Java
# application.properties (Spring Boot 3.4 and later)
logging.structured.format.console=ecs
logging.structured.ecs.service.name=order-service
logging.structured.ecs.service.version=2.7.1
logging.structured.ecs.service.environment=prod
// log.info("Order {} placed", 1042) now prints one line such as:
// {"@timestamp":"2026-11-05T08:33:27.481Z","log.level":"INFO","process.thread.name":"http-nio-8080-exec-3",
// "service.name":"order-service","log.logger":"com.shop.order.OrderService","message":"Order 1042 placed",
// "traceId":"4bf92f3577b34da6","ecs.version":"8.11"}

Notice that MDC entries, such as the traceId that Micrometer Tracing puts there, appear as fields automatically. The strength of this option is that there is nothing to maintain: it works with both Logback and Log4j2 backends, and it follows a known schema. Its limit is control. If we need custom field providers, fine-grained stack trace shaping or masking, we either use the customisation hooks Boot offers in our version or move to one of the libraries below.

3Option 2: logstash-logback-encoder

logstash-logback-encoder is a long-established library for Logback. Its LogstashEncoder gives a sensible default JSON, and its composite encoder lets us pick each field through providers. It also offers StructuredArguments for business fields, MDC include and exclude lists, a masking decorator and a ShortenedThrowableConverter for stack traces.

XML
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"service":"order-service","env":"prod"}</customFields>
<includeMdcKeyName>traceId</includeMdcKeyName>
<includeMdcKeyName>spanId</includeMdcKeyName>
<includeMdcKeyName>tenantId</includeMdcKeyName>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>30</maxDepthPerThrowable> <!-- frames kept per exception -->
<maxLength>8192</maxLength> <!-- hard cap in characters -->
<rootCauseFirst>true</rootCauseFirst> <!-- the real cause is printed at the top -->
<exclude>^sun\.reflect\..*</exclude>
</throwableConverter>
</encoder>

This is the option I reach for when the platform team needs exact control and the services are on Logback, which is Spring Boot's default.

4Option 3: Log4j2 JsonTemplateLayout

On Log4j2 the modern choice is JsonTemplateLayout. The JSON shape is described by a template file, and Log4j2 ships templates for ECS, GELF and others. We can add fields, write custom resolvers for values the templates do not cover, and control stack trace output with the exception resolvers.

XML
<Console name="JSON" target="SYSTEM_OUT">
<JsonTemplateLayout eventTemplateUri="classpath:EcsLayout.json">
<EventTemplateAdditionalField key="service.version" value="2.7.1"/>
<EventTemplateAdditionalField key="deployment.environment" value="prod"/>
</JsonTemplateLayout>
</Console>
Comparing the three options
OptionBest whenWatch out for
Boot structured loggingBoot 3.4+, a standard schema is enoughless control; options vary by Boot version
logstash-logback-encoderLogback, custom fields, masking, shapingone more dependency to keep current
Log4j2 JsonTemplateLayoutLog4j2 backend, custom resolverstemplate files to maintain

Log4j2's layout is also designed to allocate little memory, which matters for services that log heavily.

5Which Fields to Add

Every event should say who wrote it and where: service name, service version, environment, and ideally the host or pod name. It should carry trace and span ids so we can jump from a log line to the distributed trace and back. It should include a small, agreed set of MDC keys such as tenantId or requestId, and not the whole MDC, because random keys added by libraries make the schema drift.

Business values belong in fields too, not only in the message text. The message should stay a readable sentence, and the order id should be a separate field we can filter on. With logstash-logback-encoder we use StructuredArguments; with SLF4J 2 we can use the fluent API's addKeyValue, which Boot's structured formats and the encoders can turn into fields (check that your encoder version supports key-value pairs).

Java
import static net.logstash.logback.argument.StructuredArguments.kv;
log.info("Order placed {}", kv("orderId", 1042));
// message: "Order placed orderId=1042", plus a field "orderId": 1042
log.atInfo().setMessage("Order placed").addKeyValue("orderId", 1042).log();
// SLF4J 2 fluent API: message "Order placed", key-value orderId=1042 for the encoder

6Keeping Stack Traces Usable

In JSON, the stack trace must stay inside the event, in one field, so the error is one searchable document rather than sixty separate lines. Then we keep that field useful. A size limit, such as 8 KB, stops a deeply nested exception from producing a 200 KB event that the platform truncates or rejects. Root cause first puts the exception that really failed, for example a SocketTimeoutException, at the top instead of after three layers of wrapping exceptions. Excluding framework frames such as reflection and proxy internals removes noise.

It also helps to put the exception class and message in their own fields, as ECS does with error.type and error.message, so we can count errors by type without parsing the trace at all.

7Follow-up questions

How do you log a business field as a structured key without putting it in the message?

Use a key-value mechanism rather than string concatenation. With logstash-logback-encoder, StructuredArguments.keyValue or kv adds the field, and the value() variant or Markers.append can add a field without changing the message text at all. With SLF4J 2, the fluent API's addKeyValue attaches the pair to the event, and encoders that support key-value pairs write it as a field. Putting the value in the MDC also works for values that apply to a whole request, but it is the wrong tool for a value that belongs to a single line.

Should every MDC key be included in the JSON?

Usually not. Libraries and frameworks put their own keys into the MDC, and including them all makes the schema grow without anyone deciding. An allow list of agreed keys, such as traceId, spanId, tenantId and requestId, keeps fields predictable and avoids accidentally logging personal data that someone put in the MDC.

Why agree on ECS or OpenTelemetry conventions instead of our own names?

Because dashboards, alerts and tools expect known names. If every service uses service.name and trace.id, one dashboard works for all of them and tools that understand the schema can link logs to traces automatically. Inventing our own names means writing mapping rules forever.

8Conclusion

In this answer, we compared Spring Boot's built-in structured logging, which is the simplest choice in 3.4 and later, logstash-logback-encoder, which gives the finest control on Logback, and Log4j2's JsonTemplateLayout, which plays the same role on Log4j2. We added service, version, environment, trace and span ids and a small set of MDC keys, kept business values as structured key-value fields, kept the stack trace as one size-limited, root-cause-first field, and agreed on a shared schema so every service can be queried the same way.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start with Spring Boot's built-in structured logging, available since 3.4: setting logging.structured.format.console to ecs or logstash gives JSON on stdout with no extra library, and MDC values like the trace id come along automatically.

Then, if they want more

It is the least to maintain, but it gives less control, and the exact options depend on the Boot version. If the platform needs custom fields, MDC allow lists, masking or careful stack trace shaping on Logback, I would use logstash-logback-encoder, which has providers, custom fields, StructuredArguments and a ShortenedThrowableConverter.

On a Log4j2 backend, JsonTemplateLayout with the ECS template is the equivalent, and it supports custom resolvers. For fields, every event gets service name, version, environment, pod, trace id and span id, plus an agreed list of MDC keys such as tenant id, not the whole MDC.

The message stays a readable sentence, and business values like an order id go into their own fields through structured arguments or the SLF4J 2 key-value API.

For stack traces, I keep the whole trace in one field so the error is one event, cap its size at something like 8 KB, print the root cause first and trim framework frames, and add the exception type as its own field.

Most importantly, all services agree on one schema, ECS or OpenTelemetry conventions, so dashboards and alerts work everywhere.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your log platform requires JSON logs. Compare the options for a Spring Boot 3.4+...": https://scrivio-interview-library.netlify.app/java/java-logging/7e86ca8973/