You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2015/08/09 02:26:30 UTC

[4/4] logging-log4j2 git commit: [LOG4J2-1090] Add Core Configurator APIs to change a logger's level.

[LOG4J2-1090] Add Core Configurator APIs to change a logger's level.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/44943fd5
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/44943fd5
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/44943fd5

Branch: refs/heads/master
Commit: 44943fd5de499a932744c869e0f8f9788174acd2
Parents: 16a83c6
Author: ggregory <gg...@apache.org>
Authored: Sat Aug 8 17:26:24 2015 -0700
Committer: ggregory <gg...@apache.org>
Committed: Sat Aug 8 17:26:24 2015 -0700

----------------------------------------------------------------------
 .../log4j/core/config/Configuration.java        |  7 +++++
 .../logging/log4j/core/config/Configurator.java | 29 ++++++++++++++++++++
 .../apache/logging/log4j/core/LoggerTest.java   | 14 ++++++++++
 src/changes/changes.xml                         |  3 ++
 4 files changed, 53 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/44943fd5/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
index c592b05..d8cd780 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
@@ -91,6 +91,12 @@ public interface Configuration extends Filterable {
 
     Map<String, String> getProperties();
 
+    /**
+     * Returns the root Logger.
+     * @return the root Logger.
+     */
+    LoggerConfig getRootLogger();
+
     void addListener(ConfigurationListener listener);
 
     void removeListener(ConfigurationListener listener);
@@ -137,4 +143,5 @@ public interface Configuration extends Filterable {
      * @return the custom levels defined in the current configuration
      */
     List<CustomLevelConfig> getCustomLevels();
+
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/44943fd5/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
----------------------------------------------------------------------
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 7b3aea7..33ea726 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
@@ -19,6 +19,7 @@ package org.apache.logging.log4j.core.config;
 import java.net.URI;
 import java.net.URISyntaxException;
 
+import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LoggerContext;
@@ -161,6 +162,34 @@ public final class Configurator {
     }
 
     /**
+     * Sets a logger's level.
+     * @param loggerName the logger name
+     * @param level the new level
+     */
+    public static void setLevel(final String loggerName, final Level level) {
+        final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
+        final LoggerConfig loggerConfig = loggerContext.getConfiguration().getLoggerConfig(loggerName);
+        setLevel(loggerContext, loggerConfig, level);
+    }
+
+    /**
+     * Sets the root logger's level.
+     * @param level the new level
+     */
+    public static void setRootLevel(final Level level) {
+        final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
+        final LoggerConfig loggerConfig = loggerContext.getConfiguration().getRootLogger();
+        setLevel(loggerContext, loggerConfig, level);
+    }
+
+    private static void setLevel(final LoggerContext loggerContext, final LoggerConfig loggerConfig, final Level level) {
+        if (!loggerConfig.getLevel().equals(level)) {
+            loggerConfig.setLevel(level);
+            loggerContext.updateLoggers();
+        }
+    }
+
+    /**
      * Shuts down the given logging context.
      * @param ctx the logging context to shut down, may be null.
      */

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/44943fd5/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
index 13f5b7e..73d993d 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
@@ -27,6 +27,7 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.MarkerManager;
 import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.Configurator;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.junit.InitialLoggerContext;
 import org.apache.logging.log4j.message.MessageFactory;
@@ -104,6 +105,19 @@ public class LoggerTest {
     }
 
     @Test
+    public void debugChangeLevel() {
+        logger.debug("Debug message 1");
+        final List<LogEvent> events = app.getEvents();
+        assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
+        Configurator.setLevel(logger.getName(), Level.OFF);
+        logger.debug("Debug message 2");
+        assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
+        Configurator.setLevel(logger.getName(), Level.DEBUG);
+        logger.debug("Debug message 3");
+        assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 2, events.size());
+    }
+
+    @Test
     public void getLogger_String_MessageFactoryMismatch() {
         final Logger testLogger = testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatch",
                 StringFormatterMessageFactory.INSTANCE, ParameterizedMessageFactory.INSTANCE);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/44943fd5/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c381735..f85aa3e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -36,6 +36,9 @@
       <action issue="LOG4J2-1010" dev="rgoers" type="update">
         Pass log event when interpolating logger properties.
       </action>
+      <action issue="LOG4J2-1090" dev="ggregory" type="add">
+        Add Core Configurator APIs to change a logger's level.
+      </action>
       <action issue="LOG4J2-1076" dev="rpopma" type="add">
         Added support for system nanosecond time in pattern layout.
       </action>