This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new 138404a4fd Only set default configuration name if none has been
previously set (#3454)
138404a4fd is described below
commit 138404a4fd61df72cb53c40b85a16d007279229d
Author: JWT <[email protected]>
AuthorDate: Sun May 25 16:29:22 2025 +0200
Only set default configuration name if none has been previously set (#3454)
`AbstractConfiguration#setToDefault()` - only sets default configuration
name if none has been previously set,
- added test for this change
- added changelog entry
---
.../log4j/core/config/Configurator1Test.java | 2 +-
.../logging/log4j/core/config/Log4j_3431_Test.java | 89 ++++++++++++++++++++++
.../src/test/resources/bad/log4j-loggers.xml | 2 +-
.../log4j/core/config/AbstractConfiguration.java | 8 +-
src/changelog/.2.x.x/3431_default_config_name.xml | 10 +++
5 files changed, 107 insertions(+), 4 deletions(-)
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Configurator1Test.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Configurator1Test.java
index e46d4cb3ae..0a619bae9c 100644
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Configurator1Test.java
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Configurator1Test.java
@@ -332,7 +332,7 @@ class Configurator1Test {
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
assertNotNull(config, "No configuration");
- final String name = DefaultConfiguration.DEFAULT_NAME + "@" +
Integer.toHexString(config.hashCode());
+ final String name = "Configurator1Test.testNoLoggers";
assertEquals(name, config.getName(), "Unexpected Configuration.");
}
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Log4j_3431_Test.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Log4j_3431_Test.java
new file mode 100644
index 0000000000..a78f1e0f4a
--- /dev/null
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Log4j_3431_Test.java
@@ -0,0 +1,89 @@
+/*
+ * 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.config;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.logging.log4j.core.LoggerContext;
+import
org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests the change for Log4j issue #3431.
+ * <p>
+ * The configuration name should not be set to a default if a name already
exists.
+ * </p>
+ *
+ * @see <a href="https://github.com/apache/logging-log4j2/issues/3431"/>
+ */
+@SuppressWarnings("NewClassNamingConvention")
+class Log4j_3431_Test {
+
+ /**
+ * Tests that the name of a configurations with no defined loggers is
<strong>not</strong> reset when
+ * the configuration is started.
+ */
+ @Test
+ void testConfigurationDefaults_WithName() {
+
+ try (final LoggerContext ctx = new LoggerContext("Log4j_3431_Test")) {
+
+ final String name = "Log4j_3431_Configuration";
+
+ Configuration config =
ConfigurationBuilderFactory.newConfigurationBuilder()
+ .setConfigurationName(name)
+ .setConfigurationSource(ConfigurationSource.NULL_SOURCE)
+ .build(false);
+
+ // a configuration with no defined loggers should trigger
AbstractConfiguration 'setToDefault()'
+ // from 'doConfigure()'
+
+ ctx.start(config);
+
+ assertEquals(name, config.getName(), "The name of the
configuration should be '" + name + "'");
+ }
+ }
+
+ /**
+ * Tests that the name of a configurations with no defined loggers is set
to a default when
+ * the configuration is started.
+ */
+ @Test
+ void testConfigurationDefaults_WithNoName() {
+
+ try (final LoggerContext ctx = new LoggerContext("Log4j_3431_Test")) {
+
+ final String name = "Log4j_3431_Configuration";
+
+ Configuration config =
ConfigurationBuilderFactory.newConfigurationBuilder()
+ .setConfigurationSource(ConfigurationSource.NULL_SOURCE)
+ .build(false);
+
+ // a configuration with no defined loggers should trigger
AbstractConfiguration 'setToDefault()'
+ // from 'doConfigure()'
+
+ ctx.start(config);
+
+ final String expectedPrefix = DefaultConfiguration.DEFAULT_NAME +
"@";
+ Assertions.assertThatCharSequence(config.getName())
+ .withFailMessage("The name of the configuration should
start with '" + expectedPrefix + "'.")
+ .isNotBlank()
+ .startsWith(expectedPrefix);
+ }
+ }
+}
diff --git a/log4j-core-test/src/test/resources/bad/log4j-loggers.xml
b/log4j-core-test/src/test/resources/bad/log4j-loggers.xml
index 2e574046d5..acab8f7a3b 100644
--- a/log4j-core-test/src/test/resources/bad/log4j-loggers.xml
+++ b/log4j-core-test/src/test/resources/bad/log4j-loggers.xml
@@ -15,7 +15,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
-<Configuration status="OFF" name="XMLConfigTest">
+<Configuration status="OFF" name="Configurator1Test.testNoLoggers">
<Properties>
<Property name="filename">target/test.log</Property>
</Properties>
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
index 5b60a7727b..497e1e0e86 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
@@ -78,6 +78,7 @@ import org.apache.logging.log4j.core.util.WatcherFactory;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.LoaderUtil;
import org.apache.logging.log4j.util.PropertiesUtil;
+import org.apache.logging.log4j.util.Strings;
/**
* The base Configuration. Many configuration implementations will extend this
class.
@@ -774,8 +775,11 @@ public abstract class AbstractConfiguration extends
AbstractFilterable implement
}
protected void setToDefault() {
- // LOG4J2-1176 facilitate memory leak investigation
- setName(DefaultConfiguration.DEFAULT_NAME + "@" +
Integer.toHexString(hashCode()));
+ // LOG4J2-3431 don't set a default name if one has already been set
+ if (Strings.isBlank(getName())) {
+ // LOG4J2-1176 facilitate memory leak investigation
+ setName(DefaultConfiguration.DEFAULT_NAME + "@" +
Integer.toHexString(hashCode()));
+ }
final Appender appender =
ConsoleAppender.createDefaultAppenderForLayout(DefaultLayout.INSTANCE);
appender.start();
addAppender(appender);
diff --git a/src/changelog/.2.x.x/3431_default_config_name.xml
b/src/changelog/.2.x.x/3431_default_config_name.xml
new file mode 100644
index 0000000000..a76c218473
--- /dev/null
+++ b/src/changelog/.2.x.x/3431_default_config_name.xml
@@ -0,0 +1,10 @@
+<?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="changed">
+ <issue id="3431"
link="https://github.com/apache/logging-log4j2/issues/3431"/>
+ <description format="asciidoc">
+ Don't overwrite configured configuration name if the configuration has no
loggers / no root logger.
+ </description>
+</entry>