Skip to content

Commit be09e84

Browse files
JWT007ppkarwasz
authored andcommitted
Only set default configuration name if none has been previously set (#3431)
1 parent fbb3345 commit be09e84

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed
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/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,11 @@ public static Level getDefaultLevel() {
774774
}
775775

776776
protected void setToDefault() {
777-
// LOG4J2-1176 facilitate memory leak investigation
778-
setName(DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(hashCode()));
777+
// LOG4J2-3431 don't set a default name if one has already been set
778+
if (this.getName() == null || this.getName().trim().isEmpty()) {
779+
// LOG4J2-1176 facilitate memory leak investigation
780+
setName(DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(hashCode()));
781+
}
779782
final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(DefaultLayout.INSTANCE);
780783
appender.start();
781784
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)