commit 4a34ffb129268d082a4a8be34f0c8c967d7a74c4 Author: BERNARD Adrien Date: Fri Jan 2 17:51:51 2026 +0100 Test custom exception logging diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/CustomizedThrowableTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/CustomizedThrowableTest.java new file mode 100644 index 0000000000..87342031f5 --- /dev/null +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/CustomizedThrowableTest.java @@ -0,0 +1,70 @@ +/* + * 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.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.test.appender.ListAppender; +import org.apache.logging.log4j.core.test.junit.LoggerContextSource; +import org.apache.logging.log4j.core.test.junit.Named; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@code throwable} pattern. + */ +@LoggerContextSource("log4j-throwable-default.xml") +public class CustomizedThrowableTest { + private ListAppender app; + private Logger logger; + + @BeforeEach + public void setUp(final LoggerContext context, @Named("List") final ListAppender app) { + this.logger = context.getLogger("LoggerTest"); + this.app = app.clear(); + } + + @Test + public void testException() { + final Throwable exception = new CustomException("This is an error message", "ERROR_CODE"); + logger.error("Exception", exception); + final List msgs = app.getMessages(); + assertNotNull(msgs); + assertEquals(1, msgs.size(), "Incorrect number of messages. Should be 1 is " + msgs.size()); + assertTrue(msgs.get(0).contains("Code: ERROR_CODE; This is an error message"), msgs.get(0)); + } + + private static class CustomException extends Exception { + + private String errorCode; + + public CustomException(String message, String errorCode) { + super(message); + this.errorCode = errorCode; + } + + @Override + public String toString() { + return getClass().getName() + ": Code: " + errorCode + "; " + getMessage(); + } + } +} diff --git a/log4j-core-test/src/test/resources/log4j-throwable-default.xml b/log4j-core-test/src/test/resources/log4j-throwable-default.xml new file mode 100644 index 0000000000..bf61e248ea --- /dev/null +++ b/log4j-core-test/src/test/resources/log4j-throwable-default.xml @@ -0,0 +1,39 @@ + + + + + org.junit,org.apache.maven,sun.reflect,java.lang.reflect + + + + + + + + + + + + + + + + + + +