You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "JongminChung (via GitHub)" <gi...@apache.org> on 2026/01/23 08:25:17 UTC

[PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

JongminChung opened a new pull request, #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033

   **Context**
   
   Issue #4019 reports that Pattern Layout no longer preserves custom `Throwable.toString()` output after upgrading from 2.24.3 to 2.25.x. 
   The official docs for the `exception` converter state that the default behavior (no options) should match `Throwable#printStackTrace()`, which relies on `Throwable.toString()` for the header line.
   
   **Suspected change: PR #2691**
   
   PR #2691 (commit `b55c4d3fa2` - "Consolidate stack trace rendering in Pattern Layout") introduced a new renderer pipeline:
   
   - Added `ThrowableStackTraceRenderer` (and extended/inverted variants) to manually render stack traces.
   - Updated `ThrowablePatternConverter` to delegate to the new renderer instead of calling `Throwable#printStackTrace()` directly.
   
   **What changed in rendering behavior**
   
   Before PR #2691 (e.g., tag `rel/2.24.3`):
   
   - `ThrowablePatternConverter` used `Throwable#printStackTrace()` directly (either into a `StringWriter` or a `StringBuilderWriter`).
   - The output header line was always produced by `Throwable.toString()`.
   - Any custom override of `toString()` or `printStackTrace()` would be reflected in Pattern Layout output.
   
   After PR #2691:
   
   - Rendering moved into `ThrowableStackTraceRenderer` which builds the header line manually.
   - In `ThrowableStackTraceRenderer.renderThrowableMessage(...)`, the header was constructed as:
     - `throwable.getClass().getName()` + `": " + throwable.getLocalizedMessage()`
   - This bypasses `Throwable.toString()` entirely.
   - The new renderer also manually iterates stack trace elements, suppressed exceptions, and causes using cached metadata.
   
   **Why that causes #4019**
   
   `Throwable#printStackTrace()` uses `Throwable.toString()` for its first line. 
   By replacing `printStackTrace()` with a manual renderer that uses `getClass().getName()` and `getLocalizedMessage()`
   **, any customized `toString()` logic is lost.** 
   
   This matches the reporter's symptoms: stack traces remain, but the custom header details disappear.
   
   In short:
   
   - **Regression introduced by PR #2691:** default `%ex` output no longer matches `printStackTrace()` for custom `toString()` implementations.
   - **Root cause:** `renderThrowableMessage` changed from `toString()` semantics to explicit class/message concatenation.
   
   **Related issue: #3623 (Pattern Layout vs. JSON Template Layout)**
   
   Issue #3623 reports a mismatch between Pattern Layout (`%ex`) and JSON Template Layout (`exception` resolver):
   
   - JSON Template Layout uses `Throwable.printStackTrace()` for `stackTrace.stringified`.
   - Pattern Layout uses `ThrowableStackTraceRenderer.renderThrowableMessage(...)`.
   
   The JSON Template Layout implementation lives in:
   
   - `log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/StackTraceStringResolver.java`
   
   Specifically, `StackTraceStringResolver.resolve(...)` calls `throwable.printStackTrace(...)` (line ~84), which means its header line always comes from `Throwable.toString()`. 
   
   This makes the mismatch visible when exceptions override `toString()`. Aligning `ThrowableStackTraceRenderer` to `throwable.toString()` closes both #4019 (regression) and #3623 (layout inconsistency).
   
   
   ## Checklist
   
   * Base your changes on `2.x` branch if you are targeting Log4j 2; use `main` otherwise
   * `./mvnw verify` succeeds ([the build instructions](https://logging.apache.org/log4j/2.x/development.html#building))
   * Non-trivial changes contain an entry file in the `src/changelog/.2.x.x` directory
   * Tests are provided
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#issuecomment-3924663998

   > Could you replace the entire ~AI slop~ description with a short meaningful summary, please?
   
   As this is my first PR, I wanted to provide more background context. I've shortened it now. 
   Thanks for the thorough review!
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2825111287


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java:
##########
@@ -545,9 +572,13 @@ private static List<Exception> createExceptionsOfDifferentDepths() {
     }
 
     static String convert(final String pattern) {
+        return convert(pattern, EXCEPTION);
+    }
+
+    static String convert(final String pattern, final Throwable throwable) {

Review Comment:
   This can't be private since it's accessed from other classes within the same package.
   
   ([Link](https://github.com/apache/logging-log4j2/blob/2.x/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/RootThrowablePatternConverterTest.java#L22))



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2811476881


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java:
##########
@@ -79,6 +79,21 @@ public String toString() {
         }
     }
 
+    private static final class CustomException extends Exception {

Review Comment:
   Could you implement the following changes, please?
   
   1. Rename this to `ToStringOverridingException`
   2. Use `super(EXCEPTION)` in ctor, and receive no arguments
   3. Create `ToStringOverridingException.INSTANCE` field and use it (similar to `TestFriendlyException.INSTANCE`)
   4. Return a constant in `toString()`, e.g., `return "foo"`



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java:
##########
@@ -545,9 +572,13 @@ private static List<Exception> createExceptionsOfDifferentDepths() {
     }
 
     static String convert(final String pattern) {
+        return convert(pattern, EXCEPTION);
+    }
+
+    static String convert(final String pattern, final Throwable throwable) {

Review Comment:
   This can be `private`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Take `Throwable#toString()` into account while rendering stack traces in Pattern Layout [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2871431475


##########
src/changelog/.2.x.x/4033_fix_custom_throwable_to_sting.xml:
##########
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+           https://logging.apache.org/xml/ns
+           https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+       type="changed">
+    <issue id="3623" link="https://github.com/apache/logging-log4j2/pull/3623"/>
+    <issue id="4033" link="https://github.com/apache/logging-log4j2/pull/4033"/>
+    <description format="asciidoc">
+        Take Throwable#toString() into account while rendering stack traces in Pattern Layout.

Review Comment:
   ```suggestion
       <issue id="3623" link="https://github.com/apache/logging-log4j2/issues/3623"/>
       <issue id="4033" link="https://github.com/apache/logging-log4j2/pull/4033"/>
       <description format="asciidoc">
           Take `Throwable#toString()` into account while rendering stack traces in Pattern Layout.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2867854470


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java:
##########
@@ -545,9 +572,13 @@ private static List<Exception> createExceptionsOfDifferentDepths() {
     }
 
     static String convert(final String pattern) {
+        return convert(pattern, EXCEPTION);
+    }
+
+    static String convert(final String pattern, final Throwable throwable) {

Review Comment:
   It should be, since the new method is only used by this class:
   
   ```
   $ cat p/A.java 
   package p;
   public class A {
       public static void main(String[] args) { System.out.println(f(1,2)); }
       static int f(int i) { return i; }
       private static int f(int i, int j) { return i + j; }
   }
   
   $ cat p/B.java 
   package p;
   import static p.A.f;
   public class B {
       public static void main(String[] args) { System.out.println(f(1)); }
   }
   
   $ javac p/*.java
   $ java p.A
   3
   $ java p.B
   1
   ```
   
   Am I missing something?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2720200907


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/ThrowableStackTraceRenderer.java:
##########
@@ -138,12 +138,7 @@ private void renderCause(
     }
 
     static void renderThrowableMessage(final StringBuilder buffer, final Throwable throwable) {
-        final String message = throwable.getLocalizedMessage();
-        buffer.append(throwable.getClass().getName());
-        if (message != null) {
-            buffer.append(": ");
-            buffer.append(message);
-        }
+        buffer.append(throwable);

Review Comment:
   Link [Throwable.java#L697](https://github.com/openjdk/jdk/blob/ca37dba4d40bf3f71c5489829c893346faec1c56/src/java.base/share/classes/java/lang/Throwable.java#L697)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#issuecomment-3881588246

   Hi, @vy 
   
   Would love to get your eyes on this when you get a moment.
   Let me know if any changes are needed. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2825647947


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java:
##########
@@ -79,6 +79,21 @@ public String toString() {
         }
     }
 
+    private static final class CustomException extends Exception {

Review Comment:
   Thanks for the review, @vy! I've applied all your suggestions:
   
   1. Renamed to ToStringOverridingException
   2. Changed constructor to use super(EXCEPTION) with no arguments
   3. Added ToStringOverridingException.INSTANCE static field (following TestFriendlyException.INSTANCE pattern)
   4. toString() now returns a constant ("foo")
   
   ---
   
   <img width="1660" height="222" alt="image" src="https://github.com/user-attachments/assets/a0bfdcb3-d874-49f8-b712-278e728730a1" />
   
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Take `Throwable#toString()` into account while rendering stack traces in Pattern Layout [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2868046731


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java:
##########
@@ -545,9 +572,13 @@ private static List<Exception> createExceptionsOfDifferentDepths() {
     }
 
     static String convert(final String pattern) {
+        return convert(pattern, EXCEPTION);
+    }
+
+    static String convert(final String pattern, final Throwable throwable) {

Review Comment:
   I am sorry. I made a mistake. I will correct it to private.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Take `Throwable#toString()` into account while rendering stack traces in Pattern Layout [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy merged PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#issuecomment-3907515047

   
   
   <table>
       <tr>
           <th>Job</th>
           <th>Requested goals</th>
           <th>Build Tool Version</th>
           <th>Build Outcome</th>
           <th>Build ScanĀ®</th>
       </tr>
       <tr>
           <td>build-macos-latest</td>
           <td>clean install</td>
           <td align='center'>3.9.8</td>
           <td align='center'>:x:</td>
           <td><a href="https://develocity.apache.org/s/ichmjutxd6uos" rel="nofollow"><img src="https://img.shields.io/badge/Build%20Scan%C2%AE-PUBLISHED-06A0CE?logo=Gradle" alt="Build Scan PUBLISHED" /></a></td>
       </tr>
       <tr>
           <td>build-ubuntu-latest</td>
           <td>clean install</td>
           <td align='center'>3.9.8</td>
           <td align='center'>:x:</td>
           <td><a href="https://develocity.apache.org/s/vpsqbssfxrwc6" rel="nofollow"><img src="https://img.shields.io/badge/Build%20Scan%C2%AE-PUBLISHED-06A0CE?logo=Gradle" alt="Build Scan PUBLISHED" /></a></td>
       </tr>
       <tr>
           <td>build-windows-latest</td>
           <td>clean install</td>
           <td align='center'>3.9.8</td>
           <td align='center'>:x:</td>
           <td><a href="https://develocity.apache.org/s/a2bz5nnqkhe76" rel="nofollow"><img src="https://img.shields.io/badge/Build%20Scan%C2%AE-PUBLISHED-06A0CE?logo=Gradle" alt="Build Scan PUBLISHED" /></a></td>
       </tr>
   </table>
       
   
   ###### Generated by gradle/develocity-actions
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on code in PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#discussion_r2825647947


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java:
##########
@@ -79,6 +79,21 @@ public String toString() {
         }
     }
 
+    private static final class CustomException extends Exception {

Review Comment:
   Thanks for the review, @vy! I've applied all your suggestions:
   
   1. Renamed to ToStringOverridingException
   2. Changed constructor to use super(EXCEPTION) with no arguments
   3. Added ToStringOverridingException.INSTANCE static field (following TestFriendlyException.INSTANCE pattern)
   4. toString() now returns a constant ("foo")
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Take `Throwable#toString()` into account while rendering stack traces in Pattern Layout [logging-log4j2]

Posted by "JongminChung (via GitHub)" <gi...@apache.org>.
JongminChung commented on PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#issuecomment-3978694135

   > @JongminChung, thanks for the update.
   > 
   > Please, don't ever force-push. It completely nukes all the conversation history.
   > 
   > Would you also mind adding a changelog entry file, please?
   
   Sorry, I deleted them thinking they were unnecessary commitments through force-push, but I overlooked that the context of the previous conversation disappeared.
   
   Regarding the changelog you requested, I will read the document and write it.
   
   Thank you for reviewing it in detail every time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: Pattern Layout throwable use Throwable.toString() [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on PR #4033:
URL: https://github.com/apache/logging-log4j2/pull/4033#issuecomment-3907412745

   Could you replace the entire ~AI slop~ description with a short meaningful summary, please?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org