You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2017/01/07 18:10:29 UTC

[2/2] logging-log4j2 git commit: [LOG4J2-1773]: Add StatusLoggerRule

[LOG4J2-1773]: Add StatusLoggerRule

This rule allows unit tests to configure a StatusLogger level without
using a system property or configuration file.


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

Branch: refs/heads/master
Commit: 489d70846f3a724790dd7759cf8d73ee2d568042
Parents: f5a2fca
Author: Matt Sicker <ma...@spr.com>
Authored: Sat Jan 7 12:09:49 2017 -0600
Committer: Matt Sicker <ma...@spr.com>
Committed: Sat Jan 7 12:09:49 2017 -0600

----------------------------------------------------------------------
 .../logging/log4j/junit/StatusLoggerRule.java   | 49 ++++++++++++++++++++
 src/changes/changes.xml                         |  3 ++
 2 files changed, 52 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/489d7084/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java
new file mode 100644
index 0000000..11f945a
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/junit/StatusLoggerRule.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.junit;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.junit.rules.ExternalResource;
+
+/**
+ * JUnit rule to set a particular StatusLogger level. This rule is useful for debugging unit tests that do not use a
+ * Log4j configuration file.
+ *
+ * @since 2.8
+ */
+public class StatusLoggerRule extends ExternalResource {
+
+    private final StatusLogger statusLogger = StatusLogger.getLogger();
+    private final Level level;
+    private Level oldLevel;
+
+    public StatusLoggerRule(final Level level) {
+        this.level = level;
+    }
+
+    @Override
+    protected void before() throws Throwable {
+        oldLevel = statusLogger.getLevel();
+        statusLogger.setLevel(level);
+    }
+
+    @Override
+    protected void after() {
+        statusLogger.setLevel(oldLevel);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/489d7084/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 80de8de..b349e70 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -231,6 +231,9 @@
       <action issue="LOG4J2-1302" dev="rpopma" type="update">
         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
       </action>
+      <action issue="LOG4J2-1773" dev="mattsicker" type="add">
+        Add StatusLoggerRule to allow unit tests to set a status level.
+      </action>
       <action issue="LOG4J2-424" dev="mattsicker" type="add">
         Add non-string data type support to JdbcAppender via new ColumnMapping plugin.
       </action>