Skip to content

Commit 138404a

Browse files
authored
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
1 parent a3cc61d commit 138404a

File tree

5 files changed

+107
-4
lines changed

5 files changed

+107
-4
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/Configurator1Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ void testNoLoggers() {
332332
LogManager.getLogger("org.apache.test.TestConfigurator");
333333
final Configuration config = ctx.getConfiguration();
334334
assertNotNull(config, "No configuration");
335-
final String name = DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(config.hashCode());
335+
final String name = "Configurator1Test.testNoLoggers";
336336
assertEquals(name, config.getName(), "Unexpected Configuration.");
337337
}
338338

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.config;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import org.apache.logging.log4j.core.LoggerContext;
22+
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
23+
import org.assertj.core.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
/**
27+
* Tests the change for Log4j issue #3431.
28+
* <p>
29+
* The configuration name should not be set to a default if a name already exists.
30+
* </p>
31+
*
32+
* @see <a href="https://github.com/apache/logging-log4j2/issues/3431"/>
33+
*/
34+
@SuppressWarnings("NewClassNamingConvention")
35+
class Log4j_3431_Test {
36+
37+
/**
38+
* Tests that the name of a configurations with no defined loggers is <strong>not</strong> reset when
39+
* the configuration is started.
40+
*/
41+
@Test
42+
void testConfigurationDefaults_WithName() {
43+
44+
try (final LoggerContext ctx = new LoggerContext("Log4j_3431_Test")) {
45+
46+
final String name = "Log4j_3431_Configuration";
47+
48+
Configuration config = ConfigurationBuilderFactory.newConfigurationBuilder()
49+
.setConfigurationName(name)
50+
.setConfigurationSource(ConfigurationSource.NULL_SOURCE)
51+
.build(false);
52+
53+
// a configuration with no defined loggers should trigger AbstractConfiguration 'setToDefault()'
54+
// from 'doConfigure()'
55+
56+
ctx.start(config);
57+
58+
assertEquals(name, config.getName(), "The name of the configuration should be '" + name + "'");
59+
}
60+
}
61+
62+
/**
63+
* Tests that the name of a configurations with no defined loggers is set to a default when
64+
* the configuration is started.
65+
*/
66+
@Test
67+
void testConfigurationDefaults_WithNoName() {
68+
69+
try (final LoggerContext ctx = new LoggerContext("Log4j_3431_Test")) {
70+
71+
final String name = "Log4j_3431_Configuration";
72+
73+
Configuration config = ConfigurationBuilderFactory.newConfigurationBuilder()
74+
.setConfigurationSource(ConfigurationSource.NULL_SOURCE)
75+
.build(false);
76+
77+
// a configuration with no defined loggers should trigger AbstractConfiguration 'setToDefault()'
78+
// from 'doConfigure()'
79+
80+
ctx.start(config);
81+
82+
final String expectedPrefix = DefaultConfiguration.DEFAULT_NAME + "@";
83+
Assertions.assertThatCharSequence(config.getName())
84+
.withFailMessage("The name of the configuration should start with '" + expectedPrefix + "'.")
85+
.isNotBlank()
86+
.startsWith(expectedPrefix);
87+
}
88+
}
89+
}

log4j-core-test/src/test/resources/bad/log4j-loggers.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
~ See the License for the specific language governing permissions and
1616
~ limitations under the License.
1717
-->
18-
<Configuration status="OFF" name="XMLConfigTest">
18+
<Configuration status="OFF" name="Configurator1Test.testNoLoggers">
1919
<Properties>
2020
<Property name="filename">target/test.log</Property>
2121
</Properties>

log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.apache.logging.log4j.status.StatusLogger;
7979
import org.apache.logging.log4j.util.LoaderUtil;
8080
import org.apache.logging.log4j.util.PropertiesUtil;
81+
import org.apache.logging.log4j.util.Strings;
8182

8283
/**
8384
* The base Configuration. Many configuration implementations will extend this class.
@@ -774,8 +775,11 @@ public static Level getDefaultLevel() {
774775
}
775776

776777
protected void setToDefault() {
777-
// LOG4J2-1176 facilitate memory leak investigation
778-
setName(DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(hashCode()));
778+
// LOG4J2-3431 don't set a default name if one has already been set
779+
if (Strings.isBlank(getName())) {
780+
// LOG4J2-1176 facilitate memory leak investigation
781+
setName(DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(hashCode()));
782+
}
779783
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(DefaultLayout.INSTANCE);
780784
appender.start();
781785
addAppender(appender);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="3431" link="https://github.com/apache/logging-log4j2/issues/3431"/>
7+
<description format="asciidoc">
8+
Don't overwrite configured configuration name if the configuration has no loggers / no root logger.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)