Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Incident reviews show that root causes are often missing from logs because logs only print e.getMessage(), or a custom log formatter drops causes and suppressed exceptions. How do you fix this across services?

Logging only e.getMessage() throws away the stack trace, the cause chain and the suppressed exceptions, which is exactly where root causes live. The fix is to pass the exception object as the last argument to the logger, to make sure shared formatters and JSON encoders print causes and suppressed entries with sensible limits, to add a build check that rejects message-only logging of caught exceptions, and to ship all of this in the shared logging starter so every service gets it.

Why this question is asked

An incident review that ends with "we could not tell why it failed" is expensive: the bug stays, and the next incident costs the same again. Very often the information was there at the moment of failure and was thrown away by one logging line or by a formatter someone wrote years ago. Interviewers ask this scenario because it tests whether we understand what an exception carries, how logging frameworks print it, and how to make a fix stick across many teams rather than in one service. In this answer, we will recap what an exception object holds, see exactly what message-only logging loses, fix the call sites, check the formatters and JSON encoders, add an automatic check, and roll it out through the shared starter.

1What an Exception Object Really Carries

An exception is more than its message. It holds the message, the stack trace (the list of method calls that were active when it was created), a cause (the lower-level exception that led to it, set when someone wraps one exception in another), and a list of suppressed exceptions (failures during cleanup, added by try-with-resources). The cause has its own cause, so together they form a chain from the high-level symptom down to the root problem.

In a typical Spring service, the top of that chain is something like DataAccessResourceFailureException with a message such as "Unable to acquire JDBC Connection". The useful part, for example a SocketTimeoutException from the driver saying which host timed out, is two or three causes further down. A log line that shows only the top message tells us almost nothing.

2What Message-Only Logging Loses

Here are the common ways teams log a caught exception, and what each one writes.

Java
try {
customerClient.load(id);
} catch (RuntimeException e) {
log.error("Load failed: " + e.getMessage()); // Load failed: Unable to acquire JDBC Connection
log.error("Load failed: {}", e.getMessage()); // same: one line, no trace, no causes
log.error("Load failed: {}", e.toString()); // adds the class name, still no trace
log.error("Load failed for customer {}", id, e); // full trace, every Caused by:, every Suppressed:
}

Only the last line keeps the root cause. With SLF4J, which Logback and Log4j 2 both support, a Throwable passed as the last argument is treated as the exception to print, even when it is not matched by a {} placeholder. The framework then prints the stack trace, each Caused by: section and each Suppressed: section. Notice that this costs nothing extra to write; it is simply the habit of passing e instead of e.getMessage().

There is one more trap: some code calls e.getMessage() on an exception whose message is null, such as many NullPointerExceptions in older code, and the log line reads "Load failed: null". That line is worse than useless, because it suggests the failure was understood.

3Checking the Shared Formatters and JSON Encoders

Fixing call sites is not enough if the output pipeline drops the information afterwards. Many organizations log JSON so a log platform can index the fields, and the encoder decides how an exception becomes a field. A hand-written encoder that writes only throwable.getMessage() into a field called error, or a pattern layout that uses a converter printing only the first line, silently undoes all the good call sites.

So we review every shared layout. For Logback with logstash-logback-encoder, the stack_trace field is produced by a throwable converter, and the ShortenedThrowableConverter lets us set limits such as maximum depth per throwable and total length, while still printing causes. Spring Boot's built-in structured logging formats (ECS, Logstash and GELF, added in Spring Boot 3.4) include the stack trace as well; check your version for the exact options. Whatever we use, we confirm three things with a test: causes appear, suppressed exceptions appear, and long traces are shortened rather than cut off before the root cause.

Limits are worth having. A trace with 300 frames from a deep framework stack can turn one error into 40 KB of log, and a retry storm then fills the log budget. A sensible setting keeps perhaps 30 to 50 frames per throwable, always keeps the Caused by: headers, and can omit common framework frames, because the root cause usually sits at the top of the deepest cause.

Step throughWhere a root cause gets lost
  1. Code catches the exceptionlogs e.getMessage(): trace and causes gone
  2. Logger receives the eventexception object attached only if passed last
  3. Layout or JSON encodermay write only the message, or cut the trace too early
  4. Log shipper and platformmay truncate very long fields
  5. Engineer reads the logsees a symptom, not the cause

5 steps in order. Play them, or step through them one at a time.

Each stage can drop information; the fix has to cover all of them.

4Making the Rule Automatic

Habits fade, so we add a check to the build. Three tools fit well. Sonar has rules that flag exception handlers which do not keep the original exception and logging calls that pass only the message (check the rule names in your version). A Semgrep or PMD rule can match a call to log.error, log.warn or log.info inside a catch block whose arguments include e.getMessage() but not e itself. Error Prone, a compiler plugin, can host a small custom check of the same shape.

We add a unit test for the logging configuration too. It throws an exception with a cause and a suppressed exception, logs it through the real configuration into an in-memory appender, and asserts that the output contains the root cause's class name and the word Suppressed. If someone changes the encoder later, this test fails instead of the next incident review.

Java
@Test
void logOutputKeepsCausesAndSuppressed() {
var root = new java.net.SocketTimeoutException("db-7 read timed out");
var top = new IllegalStateException("load failed", root);
top.addSuppressed(new java.io.IOException("close failed"));
log.error("Load failed for customer {}", 42, top);
String out = memoryAppender.lastEventAsString();
assertThat(out).contains("SocketTimeoutException"); // passes only if causes are printed
assertThat(out).contains("Suppressed"); // passes only if suppressed entries are printed
}

5Rolling It Out Through the Shared Logging Starter

In a company with many services, the logging configuration should come from one place: a shared Spring Boot starter, a library that every service includes and that brings the Logback or Log4j 2 configuration, the JSON encoder settings, the correlation ID filter and the configuration test above. A service that adopts the starter gets correct exception output without anyone touching its logback.xml.

The rollout then has three parts. First, fix the starter and release it, with the depth limits agreed with the team that owns the log platform. Second, run the static analysis rule in report mode across all repositories to measure how many message-only log calls exist, perhaps a few hundred, and fix them in bulk, which is mostly mechanical. Third, switch the rule to fail the build for new code. After that, we can check the outcome in the next incident reviews: the question "was the root cause in the logs?" should be answered yes.

6Follow-up questions

Should we log an exception at every layer it passes through?

No. Logging the same exception at each layer produces the same trace three or four times and makes logs harder to read. The usual rule is to either handle it and log it once, or wrap it with context and rethrow, and let one place, often the top-level handler such as a @RestControllerAdvice or the message listener's error handler, log it with the full trace.

Is it safe to log full stack traces given that they may contain data?

Stack traces themselves contain class and method names, but exception messages can contain data such as an SQL fragment, an email address or a token in a URL. So the real rule is about messages: we never put secrets or personal data into exception messages, and the log pipeline can add masking for known patterns as a second line of defence.

7Conclusion

In this answer, we saw that an exception carries a stack trace, a cause chain and suppressed exceptions, and that logging only getMessage() throws all three away. The fix is to pass the exception as the last logger argument, to verify that shared layouts and JSON encoders print causes and suppressed entries with sensible depth limits, to add static analysis and a configuration test so the rule is checked automatically, and to deliver it in the shared logging starter so every service inherits it.

What to say in the interviewAbout 80 seconds aloud

Open with

The root cause is usually in the cause chain, two or three exceptions below the one we caught, and logging e.getMessage() keeps only the top message. So first I fix the call sites: with SLF4J I pass the exception object as the last argument, like log.error with the customer id and then e, and the framework prints the full stack trace with every Caused by and Suppressed section.

Then, if they want more

Second, I check the shared layouts and JSON encoders, because a custom encoder that writes only the message undoes all of that. I make sure the stack trace field includes causes and suppressed exceptions, with limits on depth and length so a deep framework trace does not flood the logs, but never cutting off the root cause.

Third, I make it automatic: a Sonar, Semgrep or PMD rule that flags log calls in catch blocks passing only getMessage, and a test of the logging configuration that logs an exception with a cause and asserts the cause appears in the output.

Finally, I put the configuration and the test into the shared logging starter, run the rule in report mode across repositories, fix existing calls in bulk, then make the rule fail the build for new code.

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 documentation below. 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 "Incident reviews show that root causes are often missing from logs because logs only...": https://scrivio-interview-library.netlify.app/java/exceptions/6a82eaacc1/