Log4j2 including library name in stacktrace
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If by "library name" you mean the JAR or package metadata associated with each stack-trace frame, the default Log4j2 exception converter is not enough. The feature you want is the extended throwable pattern, %xEx, which appends packaging information to exception frames and is useful when debugging classpath conflicts or duplicate dependencies.
Use %xEx instead of %ex
The default %ex or %throwable converter prints a normal Java stack trace. It does not include packaging details. To see the container name or version information attached to each frame, switch the pattern to %xEx.
That is the main answer. If the goal is to know which dependency supplied a class in the trace, %xEx is the pattern to remember.
You can also limit how much trace is printed:
Or filter noisy packages:
That keeps error logs readable while still preserving useful packaging details.
Packaging data is not the same as caller location
This is a common source of confusion. %xEx enriches exception frames. It does not tell Log4j2 to print the source location of the logging call itself.
If you want caller information for the log event, use location fields such as:
- '
%Cor%class' - '
%Mor%method' - '
%For%file' - '
%Lor%line'
For example:
That pattern combines caller location with extended exception output. It is useful, but more expensive than a plain logger-and-message pattern because Log4j2 has to inspect the call stack.
The Java code does not change
The application code is ordinary logging code. The difference is entirely in the layout configuration.
With %ex, you get a normal exception. With %xEx, the same exception output carries packaging information for the frames, which can help pinpoint classpath issues faster.
Be deliberate about performance
Extended exception output is usually reasonable for error-level logging, because exceptions should be relatively rare. Caller location fields are more expensive and are a bad default for very high-volume logs.
If you use asynchronous logging, location information may also need explicit enabling in the relevant configuration. That is another reason to treat location-heavy patterns as targeted diagnostics rather than a blanket default.
Common Pitfalls
- Expecting
%exto print JAR or packaging information when it only prints a standard stack trace. - Confusing packaging data from
%xExwith caller location data such as%classor%line. - Assuming shaded or repackaged JARs will always produce clean dependency identification.
- Enabling expensive location patterns on high-volume log paths without measuring the overhead.
- Changing Java logging code when the real fix belongs in the Log4j2 layout pattern.
Summary
- Use
%xExwhen you want Log4j2 stack traces enriched with packaging or library information. - '
%exprints a normal stack trace and does not include that extra metadata.' - Caller location fields solve a different problem from packaging data.
- Extended stack traces are most useful for error diagnostics and classpath issues.
- Keep expensive location-aware patterns targeted rather than global.

