Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a new test file, would you mind extending ThrowablePatternConverterTest.StackTraceTest, please?

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.pattern;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;

import java.util.List;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.Issue;

/**
* Tests for {@link ThrowableExtendedStackTraceRenderer}.
*
* <p>Verifies that {@link NoClassDefFoundError} during class loading
* does not break the extended stack trace rendering ({@code %xEx}).</p>
*
* @see <a href="https://github.com/apache/logging-log4j2/issues/4028">#4028</a>
*/
class ThrowableExtendedStackTraceRendererTest {

private static final PatternParser PATTERN_PARSER = PatternLayout.createPatternParser(null);

/**
* Verifies that the extended stack trace renderer does not propagate {@link NoClassDefFoundError}
* when a class in the stack trace cannot be found by the class loader.
*/
@Test
@Issue("https://github.com/apache/logging-log4j2/issues/4028")
void loadClass_should_handle_NoClassDefFoundError() {

// Create an exception with a stack trace element referencing a non-existent class.
// When the extended renderer tries to resolve this class for JAR/version info,
// the class loader will throw NoClassDefFoundError.
final Exception exception = new Exception("test exception");
final StackTraceElement[] originalTrace = exception.getStackTrace();
final StackTraceElement[] modifiedTrace = new StackTraceElement[originalTrace.length + 1];
modifiedTrace[0] = new StackTraceElement(
"com.nonexistent.deliberately.missing.ClassName", "someMethod", "ClassName.java", 42);
System.arraycopy(originalTrace, 0, modifiedTrace, 1, originalTrace.length);
exception.setStackTrace(modifiedTrace);

// Render using the extended throwable pattern (%xEx)
final List<PatternFormatter> patternFormatters = PATTERN_PARSER.parse("%xEx", false, true, true);
assertThat(patternFormatters).hasSize(1);
final PatternFormatter patternFormatter = patternFormatters.get(0);

final LogEvent logEvent = Log4jLogEvent.newBuilder()
.setThrown(exception)
.setLevel(Level.ERROR)
.build();
final StringBuilder buffer = new StringBuilder();

// The rendering should not throw any exception
assertThatNoException().isThrownBy(() -> patternFormatter.format(logEvent, buffer));

// The output should contain our non-existent class name and the exception message
final String output = buffer.toString();
assertThat(output).contains("com.nonexistent.deliberately.missing.ClassName");
assertThat(output).contains("test exception");
}

/**
* Verifies that the extended stack trace renderer gracefully handles a class loader
* that throws {@link NoClassDefFoundError} during class resolution.
*
* <p>This simulates the real-world scenario from
* <a href="https://github.com/apache/logging-log4j2/issues/4028">#4028</a>,
* where custom class loaders (e.g., in application servers or frameworks like Frank!Framework)
* throw {@link NoClassDefFoundError} for classes that have been unloaded.</p>
*/
@Test
@Issue("https://github.com/apache/logging-log4j2/issues/4028")
void loadClass_should_handle_NoClassDefFoundError_from_custom_classloader() {

// Create a class loader that always throws NoClassDefFoundError
final ClassLoader errorThrowingLoader = new ClassLoader(null) {
@Override
public Class<?> loadClass(final String name) throws ClassNotFoundException {
throw new NoClassDefFoundError("Simulated missing class: " + name);
}
};

final Exception exception = new Exception("test with custom classloader");
exception.setStackTrace(new StackTraceElement[] {
new StackTraceElement("com.example.UnloadedClass", "doWork", "UnloadedClass.java", 10),
new StackTraceElement("com.example.CallerClass", "invoke", "CallerClass.java", 20)
});

final List<PatternFormatter> patternFormatters = PATTERN_PARSER.parse("%xEx", false, true, true);
final PatternFormatter patternFormatter = patternFormatters.get(0);

final LogEvent logEvent = Log4jLogEvent.newBuilder()
.setThrown(exception)
.setLevel(Level.ERROR)
.build();
final StringBuilder buffer = new StringBuilder();

// Set the error-throwing class loader as the context class loader
// so the renderer's loadClass method encounters it
final Thread currentThread = Thread.currentThread();
final ClassLoader originalLoader = currentThread.getContextClassLoader();
try {
currentThread.setContextClassLoader(errorThrowingLoader);

// The rendering should succeed without propagating NoClassDefFoundError
assertThatNoException().isThrownBy(() -> patternFormatter.format(logEvent, buffer));
} finally {
currentThread.setContextClassLoader(originalLoader);
}

// The output should still contain the exception and stack trace
final String output = buffer.toString();
assertThat(output).contains("test with custom classloader");
assertThat(output).contains("com.example.UnloadedClass");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private static Class<?> loadClass(final ClassLoader loader, final String classNa
if (clazz != null) {
return clazz;
}
} catch (final Exception ignored) {
} catch (final Exception | LinkageError ignored) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (final Exception | LinkageError ignored) {
} catch (final Throwable ignored) {

// Do nothing
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="4028" link="https://github.com/apache/logging-log4j2/issues/4028"/>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<issue id="4028" link="https://github.com/apache/logging-log4j2/issues/4028"/>
<issue id="4049" link="https://github.com/apache/logging-log4j2/pull/4049"/>

<description format="asciidoc">Catch `LinkageError` in `ThrowableExtendedStackTraceRenderer#loadClass` to prevent `NoClassDefFoundError` from breaking logging</description>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<description format="asciidoc">Catch `LinkageError` in `ThrowableExtendedStackTraceRenderer#loadClass` to prevent `NoClassDefFoundError` from breaking logging</description>
<description format="asciidoc">Ensure all `Throwable`s are handled while rendering stack traces</description>

</entry>