Skip to content

Add support for old-style exception rendering in Pattern Layout #3873

@rschmitt

Description

@rschmitt

Due to #3045, all exception messages are prefixed with a newline now:

[SIGTSTP] [1] ~/src/logging-log4j2 # rg -txml pattern
src/main/resources/log4j2.xml
6:                <pattern>%date{EEE MMM dd HH:mm:ss yyyy zzz}{UTC} [%p] (%t) %c: %m %ex%n</pattern>
[0] [1] ~/src/logging-log4j2 # ./run.sh
Darwin 842f57b044d8 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 arm64
Log4j 2.24.3:
Thu Aug 07 17:22:01 2025 UTC [ERROR] (main) org.example.App: Going to call a method now
Thu Aug 07 17:22:01 2025 UTC [ERROR] (main) org.example.App: Got exception while calling throwException, how could this have happened java.lang.RuntimeException: This is the message passed to the RuntimeException ctor
	at org.example.App.throwException(App.java:20)
	at org.example.App.main(App.java:12)

Thu Aug 07 17:22:01 2025 UTC [ERROR] (main) org.example.App: Done calling method

Log4j 2.25.0:
Thu Aug 07 17:22:02 2025 UTC [ERROR] (main) org.example.App: Going to call a method now
Thu Aug 07 17:22:02 2025 UTC [ERROR] (main) org.example.App: Got exception while calling throwException, how could this have happened
java.lang.RuntimeException: This is the message passed to the RuntimeException ctor
	at org.example.App.throwException(App.java:20)
	at org.example.App.main(App.java:12)

Thu Aug 07 17:22:02 2025 UTC [ERROR] (main) org.example.App: Done calling method

I don't see a way to reproduce the 2.24.3 style of log messages with the pattern layout.

  1. Using %ex{0} to get the first line doesn't work, because a line separator is prepended.
  2. I can't find anything in ThrowableFormatOptions that provides the type of the exception thrown. A pattern like %m %ex{short.message}%n doesn't fully reproduce the top line of the stack trace.
  3. Using %n%ex to print the rest of the stack trace repeats the first line. In other words, there's no way to just print the frames of the stack trace.

Since this layout change affects log scans, I'd like it if there were a way to specify a pattern that reproduces the pre-2.25 layout for anyone who depends on it.

Metadata

Metadata

Assignees

Labels

layoutsAffects one or more Layout plugins

Type

No type

Projects

Status

To triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions