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/15 17:14:46 UTC

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

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 6843d7d7d -> c93c40531


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

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

Branch: refs/heads/master
Commit: c93c40531ff91d0e94d36a4ba1ebfe11b1706319
Parents: 6843d7d
Author: ggregory <gg...@apache.org>
Authored: Sat Aug 15 08:14:39 2015 -0700
Committer: ggregory <gg...@apache.org>
Committed: Sat Aug 15 08:14:39 2015 -0700

----------------------------------------------------------------------
 .../logging/log4j/core/config/Configurator.java | 53 ++++++++++++++++----
 .../apache/logging/log4j/core/LoggerTest.java   | 25 ++++++++-
 2 files changed, 66 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c93c4053/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 132d047..613867d 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
@@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.config;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Map;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
@@ -161,10 +162,47 @@ public final class Configurator {
         return initialize(name, null, configLocation);
     }
 
+    private static boolean setLevel(final LoggerConfig loggerConfig, Level level) {
+        boolean set = !loggerConfig.getLevel().equals(level);
+        if (set) {
+            loggerConfig.setLevel(level);
+        }
+        return set;
+    }
+
+    private static void setLevel(final LoggerContext loggerContext, final LoggerConfig loggerConfig,
+            final Level level) {
+        if (!loggerConfig.getLevel().equals(level)) {
+            loggerConfig.setLevel(level);
+            loggerContext.updateLoggers();
+        }
+    }
+
+    /**
+     * Sets a logger levels.
+     * @param level
+     *            a levelMap where keys are level names and values are new Levels.
+     */
+    public static void setLevel(final Map<String, Level> levelMap) {
+        final LoggerContext loggerContext = LoggerContext.getContext(false);
+        boolean set = false;
+        for (Map.Entry<String, Level> entry : levelMap.entrySet()) {
+            final LoggerConfig loggerConfig = loggerContext.getConfiguration().getLoggerConfig(entry.getKey());
+            Level level = entry.getValue();
+            set |= setLevel(loggerConfig, level);
+        }
+        if (set) {
+            loggerContext.updateLoggers();
+        }
+    }
+
     /**
      * Sets a logger's level.
-     * @param loggerName the logger name
-     * @param level the new 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.getContext(false);
@@ -174,7 +212,9 @@ public final class Configurator {
 
     /**
      * Sets the root logger's level.
-     * @param level the new level
+     * 
+     * @param level
+     *            the new level
      */
     public static void setRootLevel(final Level level) {
         final LoggerContext loggerContext = LoggerContext.getContext(false);
@@ -182,13 +222,6 @@ public final class Configurator {
         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/c93c4053/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 73d993d..29a8cc3 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
@@ -16,10 +16,17 @@
  */
 package org.apache.logging.log4j.core;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertTrue;
+
 import java.io.File;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.logging.log4j.Level;
@@ -39,8 +46,6 @@ import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
-
 /**
  *
  */
@@ -118,6 +123,22 @@ public class LoggerTest {
     }
 
     @Test
+    public void debugChangeLevels() {
+        logger.debug("Debug message 1");
+        final List<LogEvent> events = app.getEvents();
+        assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
+        Map<String, Level> map = new HashMap<>();
+        map.put(logger.getName(), Level.OFF);
+        Configurator.setLevel(map);
+        logger.debug("Debug message 2");
+        assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
+        map.put(logger.getName(), Level.DEBUG);
+        Configurator.setLevel(map);
+        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);