This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 786476ec40 [LOG4J-3631] Fix `Configurator#setLevel` for internal
classes
786476ec40 is described below
commit 786476ec40b5dfd6b44aeae27d033808168014e7
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 | 16 +++++++++++++++-
src/changes/changes.xml | 3 +++
4 files changed, 41 insertions(+), 2 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 c4f6b416bd..41b43c69e0 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, Serializable {
* 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 d43bfdf4ed..dfd1a51c61 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
@@ -327,7 +327,21 @@ public final class Configurator {
setLevel(logger.getName(), level);
return logger;
}
-
+
+ /**
+ * Sets a logger's level.
+ *
+ * @param clazz
+ * the logger
+ * @param level
+ * the new level
+ */
+ public static void setLevel(final Class<?> clazz, final Level 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 c96d40f79a..22d61a238d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -205,6 +205,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">