This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 565bef42c6 [LOG4J-3631] Fix `Configurator#setLevel` for internal
classes
565bef42c6 is described below
commit 565bef42c6c29e893317e4fbe51b4bab50514a21
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Wed Nov 9 19:07:28 2022 +0100
[LOG4J-3631] Fix `Configurator#setLevel` for internal classes
---
.../apache/logging/log4j/spi/AbstractLogger.java | 3 ++-
.../logging/log4j/core/config/ConfiguratorTest.java | 21 +++++++++++++++++++++
.../logging/log4j/core/config/Configurator.java | 8 +++++---
src/changes/changes.xml | 3 +++
4 files changed, 31 insertions(+), 4 deletions(-)
diff --git
a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
index 17b329b622..50d583a748 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
@@ -117,7 +117,8 @@ public abstract class AbstractLogger implements
ExtendedLogger, LocationAwareLog
* Creates a new logger named after this class (or subclass).
*/
public AbstractLogger() {
- this.name = getClass().getName();
+ final String canonicalName = getClass().getCanonicalName();
+ this.name = canonicalName != null ? canonicalName :
getClass().getName();
this.messageFactory = createDefaultMessageFactory();
this.flowMessageFactory = createDefaultFlowMessageFactory();
this.logBuilder = new LocalLogBuilder(this);
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
index 9fcfdf96c8..17dae4e4e6 100644
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
@@ -19,10 +19,12 @@ package org.apache.logging.log4j.core.config;
import java.io.File;
import java.net.URI;
+import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
+import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
@Tag("functional")
@@ -70,4 +72,23 @@ public class ConfiguratorTest {
assertNotNull(loggerContext.getConfiguration().getAppender("List"));
}
}
+
+ /**
+ * LOG4J2-3631: Configurator uses getName() instead of getCanonicalName().
+ */
+ @Test
+ public void testSetLevelUsesCanonicalName() {
+ final String path = new
File("src/test/resources/log4j-list.xml").getAbsolutePath();
+ try (final LoggerContext loggerContext =
Configurator.initialize(getClass().getName(), null, path)) {
+ Configurator.setLevel(Internal.class, Level.DEBUG);
+ final Configuration config = loggerContext.getConfiguration();
+ assertNotNull(config);
+ final String canonicalName = Internal.class.getCanonicalName();
+
assertThat(config.getLoggerConfig(canonicalName)).extracting(LoggerConfig::getName,
LoggerConfig::getExplicitLevel)
+ .containsExactly(canonicalName, Level.DEBUG);
+ }
+ }
+
+ private static class Internal {
+ }
}
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
index e0f0e4f711..563b0f2d43 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
@@ -340,7 +340,7 @@ public final class Configurator {
setLevel(LoggerContext.getContext(StackLocatorUtil.getCallerClassLoader(2),
false, null), logger.getName(), level);
return logger;
}
-
+
/**
* Sets a logger's level.
*
@@ -350,9 +350,11 @@ public final class Configurator {
* the new level
*/
public static void setLevel(final Class<?> clazz, final Level level) {
-
setLevel(LoggerContext.getContext(StackLocatorUtil.getCallerClassLoader(2),
false, null), clazz.getName(), level);
+ final String canonicalName = clazz.getCanonicalName();
+
setLevel(LoggerContext.getContext(StackLocatorUtil.getCallerClassLoader(2),
false, null),
+ canonicalName != null ? canonicalName : clazz.getName(),
level);
}
-
+
private static boolean setLevel(final LoggerConfig loggerConfig, final
Level level) {
final boolean set = !loggerConfig.getLevel().equals(level);
if (set) {
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0f63d2c71a..a09912dbc6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -33,6 +33,9 @@
<action issue="LOG4J2-2678" dev="pkarwasz" type="update"
due-to="Federico D'Ambrosio">
Add LogEvent timestamp to ProducerRecord in KafkaAppender.
</action>
+ <action issue="LOG4J2-3631" dev="pkarwasz" type="fix" due-to="Jeff
Thomas">
+ Fix `Configurator#setLevel` for internal classes.
+ </action>
</release>
<release version="2.19.0" date="2022-09-09" description="GA Release
2.19.0">
<action issue="LOG4J2-3584" dev="vy" type="fix">