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/20 04:15:24 UTC
logging-log4j2 git commit: Fix Configurator setLevel(Map) and setLevel(String, Level) for child loggers.
Repository: logging-log4j2
Updated Branches:
refs/heads/master 273d2880a -> faed5fe86
Fix Configurator setLevel(Map<String, Level>) and setLevel(String,
Level) for child loggers.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/faed5fe8
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/faed5fe8
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/faed5fe8
Branch: refs/heads/master
Commit: faed5fe86ad36876a70e42aa7ce35aa2d2c8fed5
Parents: 273d288
Author: ggregory <gg...@apache.org>
Authored: Wed Aug 19 19:15:18 2015 -0700
Committer: ggregory <gg...@apache.org>
Committed: Wed Aug 19 19:15:18 2015 -0700
----------------------------------------------------------------------
.../logging/log4j/core/config/Configurator.java | 35 ++++++++++--
.../apache/logging/log4j/core/LoggerTest.java | 58 +++++++++++++++-----
2 files changed, 74 insertions(+), 19 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/faed5fe8/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 747c44d..5b9c4d2 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
@@ -28,6 +28,7 @@ import org.apache.logging.log4j.core.impl.Log4jContextFactory;
import org.apache.logging.log4j.core.util.FileUtils;
import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.Strings;
/**
* Initializes and configure the Logging system. This class provides several ways to construct a LoggerContext using
@@ -180,17 +181,19 @@ public final class Configurator {
/**
* Sets a logger levels.
+ *
* @param level
- * a levelMap where keys are level names and values are new Levels.
+ * 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);
- final Configuration configuration = loggerContext.getConfiguration();
+ final Configuration config = loggerContext.getConfiguration();
boolean set = false;
for (final Map.Entry<String, Level> entry : levelMap.entrySet()) {
- final String name = entry.getKey();
+ final String loggerName = entry.getKey();
final Level level = entry.getValue();
- set |= setLevel(configuration.getLoggerConfig(name), level);
+ set |= setLevel(loggerName, level, config);
}
if (set) {
loggerContext.updateLoggers();
@@ -207,8 +210,28 @@ public final class Configurator {
*/
public static void setLevel(final String loggerName, final Level level) {
final LoggerContext loggerContext = LoggerContext.getContext(false);
- final LoggerConfig loggerConfig = loggerContext.getConfiguration().getLoggerConfig(loggerName);
- setLevel(loggerContext, loggerConfig, level);
+ if (Strings.isEmpty(loggerName)) {
+ setRootLevel(level);
+ } else {
+ if (setLevel(loggerName, level, loggerContext.getConfiguration())) {
+ loggerContext.updateLoggers();
+ }
+ }
+ }
+
+ private static boolean setLevel(final String loggerName, final Level level, final Configuration config) {
+ boolean set;
+ LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
+ if (!loggerName.equals(loggerConfig.getName())) {
+ // TODO Should additivity be inherited?
+ loggerConfig = new LoggerConfig(loggerName, level, true);
+ config.addLogger(loggerName, loggerConfig);
+ loggerConfig.setLevel(level);
+ set = true;
+ } else {
+ set = setLevel(loggerConfig, level);
+ }
+ return set;
}
/**
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/faed5fe8/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 6c05a5d..1f70523 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
@@ -43,8 +43,7 @@ import org.apache.logging.log4j.message.StringFormatterMessageFactory;
import org.apache.logging.log4j.message.StructuredDataMessage;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Ignore;
+import org.junit.Rule;
import org.junit.Test;
/**
@@ -57,8 +56,8 @@ public class LoggerTest {
private ListAppender host;
private ListAppender noThrown;
- @ClassRule
- public static InitialLoggerContext context = new InitialLoggerContext(CONFIG);
+ @Rule
+ public InitialLoggerContext context = new InitialLoggerContext(CONFIG);
private void assertEventCount(final List<LogEvent> events, final int expected) {
assertEquals("Incorrect number of events.", expected, events.size());
@@ -66,12 +65,18 @@ public class LoggerTest {
@Before
public void before() {
+ logger = context.getLogger("LoggerTest");
+ loggerChild = context.getLogger("LoggerTest.child");
+ loggerGrandchild = context.getLogger("LoggerTest.child.grand");
+ //
app = context.getListAppender("List").clear();
host = context.getListAppender("HostTest").clear();
noThrown = context.getListAppender("NoThrowable").clear();
}
- org.apache.logging.log4j.Logger logger = context.getLogger("LoggerTest");
+ org.apache.logging.log4j.Logger logger;
+ org.apache.logging.log4j.Logger loggerChild;
+ org.apache.logging.log4j.Logger loggerGrandchild;
@Test
public void basicFlow() {
@@ -128,26 +133,27 @@ public class LoggerTest {
}
@Test
- public void debugChangeLevelsChild() {
- org.apache.logging.log4j.Logger loggerChild = context.getLogger(logger.getName() + ".child");
- // Use logger AND loggerChild
+ public void debugChangeLevelChildLogger() {
+ // Use logger AND child loggers
logger.debug("Debug message 1");
loggerChild.debug("Debug message 1 child");
+ loggerGrandchild.debug("Debug message 1 grandchild");
final List<LogEvent> events = app.getEvents();
- assertEventCount(events, 2);
+ assertEventCount(events, 3);
Configurator.setLevel(logger.getName(), Level.OFF);
logger.debug("Debug message 2");
loggerChild.debug("Debug message 2 child");
- assertEventCount(events, 2);
+ loggerGrandchild.debug("Debug message 2 grandchild");
+ assertEventCount(events, 3);
Configurator.setLevel(logger.getName(), Level.DEBUG);
logger.debug("Debug message 3");
loggerChild.debug("Debug message 3 child");
- assertEventCount(events, 4);
+ loggerGrandchild.debug("Debug message 3 grandchild");
+ assertEventCount(events, 6);
}
@Test
- @Ignore
- public void debugChangeLevelsChildDifferentLevels() {
+ public void debugChangeLevelsChildLoggers() {
org.apache.logging.log4j.Logger loggerChild = context.getLogger(logger.getName() + ".child");
// Use logger AND loggerChild
logger.debug("Debug message 1");
@@ -182,6 +188,32 @@ public class LoggerTest {
}
@Test
+ public void debugChangeLevelsMapChildLoggers() {
+ logger.debug("Debug message 1");
+ loggerChild.debug("Debug message 1 C");
+ loggerGrandchild.debug("Debug message 1 GC");
+ final List<LogEvent> events = app.getEvents();
+ assertEventCount(events, 3);
+ Map<String, Level> map = new HashMap<>();
+ map.put(logger.getName(), Level.OFF);
+ map.put(loggerChild.getName(), Level.DEBUG);
+ map.put(loggerGrandchild.getName(), Level.WARN);
+ Configurator.setLevel(map);
+ logger.debug("Debug message 2");
+ loggerChild.debug("Debug message 2 C");
+ loggerGrandchild.debug("Debug message 2 GC");
+ assertEventCount(events, 4);
+ map.put(logger.getName(), Level.DEBUG);
+ map.put(loggerChild.getName(), Level.OFF);
+ map.put(loggerGrandchild.getName(), Level.DEBUG);
+ Configurator.setLevel(map);
+ logger.debug("Debug message 3");
+ loggerChild.debug("Debug message 3 C");
+ loggerGrandchild.debug("Debug message 3 GC");
+ assertEventCount(events, 6);
+ }
+
+ @Test
public void debugChangeRootLevel() {
logger.debug("Debug message 1");
final List<LogEvent> events = app.getEvents();