Repository: logging-log4j2
Updated Branches:
  refs/heads/master 16e7ef70c -> a2af6ed49


[LOG4J2-1523] Log4j 1 appenders. NullAppender.

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

Branch: refs/heads/master
Commit: a2af6ed49267c44254891f1c7826ea610d5b5294
Parents: 16e7ef7
Author: Gary Gregory <ggreg...@apache.org>
Authored: Wed Sep 21 19:27:42 2016 -0700
Committer: Gary Gregory <ggreg...@apache.org>
Committed: Wed Sep 21 19:27:42 2016 -0700

----------------------------------------------------------------------
 .../log4j/config/Log4j1ConfigurationParser.java |  10 +-
 .../config/Log4j1ConfigurationFactoryTest.java  | 174 +++++++++++--------
 .../config-1.2/log4j-NullAppender.properties    |   9 +
 .../log4j/core/appender/NullAppender.java       |  29 ++++
 4 files changed, 144 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a2af6ed4/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
----------------------------------------------------------------------
diff --git 
a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
 
b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
index a8c3977..adc512c 100644
--- 
a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
+++ 
b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
@@ -117,12 +117,15 @@ public class Log4j1ConfigurationParser {
         case "org.apache.log4j.FileAppender":
             buildFileAppender(appenderName);
             break;
+        case "org.apache.log4j.varia.NullAppender":
+            buildNullAppender(appenderName);
+            break;
         default:
             reportWarning("Unknown appender class: " + appenderClass + "; 
ignoring appender: " + appenderName);
         }
     }
 
-    private void buildConsoleAppender(final String appenderName) {
+       private void buildConsoleAppender(final String appenderName) {
         final AppenderComponentBuilder appenderBuilder = 
builder.newAppender(appenderName, "Console");
         final String targetValue = getLog4jAppenderValue(appenderName, 
"Target", "System.out");
         if (targetValue != null) {
@@ -185,6 +188,11 @@ public class Log4j1ConfigurationParser {
         }
     }
 
+    private void buildNullAppender(String appenderName) {
+        final AppenderComponentBuilder appenderBuilder = 
builder.newAppender(appenderName, "Null");
+        builder.add(appenderBuilder);
+       }
+
     private void buildAppenderLayout(final String name, final 
AppenderComponentBuilder appenderBuilder) {
         final String layoutClass = getLog4jAppenderValue(name, "layout", null);
         if (layoutClass != null) {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a2af6ed4/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
index ea55dd9..842bf6b 100644
--- 
a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
+++ 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
@@ -16,97 +16,117 @@
  */
 package org.apache.log4j.config;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.net.URISyntaxException;
 import java.net.URL;
 
 import org.apache.log4j.layout.Log4j1XmlLayout;
 import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.Layout;
 import org.apache.logging.log4j.core.appender.ConsoleAppender;
 import org.apache.logging.log4j.core.appender.ConsoleAppender.Target;
 import org.apache.logging.log4j.core.appender.FileAppender;
+import org.apache.logging.log4j.core.appender.NullAppender;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.core.layout.HtmlLayout;
 import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.status.StatusLogger;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
-
 public class Log4j1ConfigurationFactoryTest {
 
-    private Layout<?> testConsole(final String configResource) throws 
Exception {
-        final URL configLocation = 
ClassLoader.getSystemResource(configResource);
-        assertNotNull(configLocation);
-        final Configuration configuration = new 
Log4j1ConfigurationFactory().getConfiguration(null,
-                "test", configLocation.toURI());
-        assertNotNull(configuration);
-        final ConsoleAppender appender = configuration.getAppender("Console");
-        assertNotNull(appender);
-        assertEquals(Target.SYSTEM_ERR, appender.getTarget());
-        //
-        final LoggerConfig loggerConfig = 
configuration.getLoggerConfig("com.example.foo");
-        assertNotNull(loggerConfig);
-        assertEquals(Level.DEBUG, loggerConfig.getLevel());
-        return appender.getLayout();
-    }
-
-    private Layout<?> testFile(final String configResource) throws Exception {
-        final URL configLocation = 
ClassLoader.getSystemResource(configResource);
-        assertNotNull(configLocation);
-        final Configuration configuration = new 
Log4j1ConfigurationFactory().getConfiguration(null,
-                       "test", configLocation.toURI());
-        assertNotNull(configuration);
-        final FileAppender appender = configuration.getAppender("File");
-        assertNotNull(appender);
-        assertEquals("target/mylog.txt", appender.getFileName());
-        //
-        final LoggerConfig loggerConfig = 
configuration.getLoggerConfig("com.example.foo");
-        assertNotNull(loggerConfig);
-        assertEquals(Level.DEBUG, loggerConfig.getLevel());
-        return appender.getLayout();
-    }
-
-    @Test
-    public void testConsoleEnhancedPatternLayout() throws Exception {
-        final PatternLayout layout = (PatternLayout) 
testConsole("config-1.2/log4j-console-EnhancedPatternLayout.properties");
-        assertEquals("%d{ISO8601} [%t][%c] %-5p %properties %ndc: %m%n", 
layout.getConversionPattern());
-    }
-
-    @Test
-    public void testConsoleHtmlLayout() throws Exception {
-        final HtmlLayout layout = (HtmlLayout) 
testConsole("config-1.2/log4j-console-HtmlLayout.properties");
-        assertEquals("Headline", layout.getTitle());
-        assertTrue(layout.isLocationInfo());
-    }
-
-    @Test
-    public void testConsolePatternLayout() throws Exception {
-        final PatternLayout layout = (PatternLayout) 
testConsole("config-1.2/log4j-console-PatternLayout.properties");
-        assertEquals("%d{ISO8601} [%t][%c] %-5p: %m%n", 
layout.getConversionPattern());
-    }
-
-    @Test
-    public void testConsoleSimpleLayout() throws Exception {
-        final PatternLayout layout = (PatternLayout) 
testConsole("config-1.2/log4j-console-SimpleLayout.properties");
-        assertEquals("%level - %m%n", layout.getConversionPattern());
-    }
-
-    @Test
-    public void testConsoleTtccLayout() throws Exception {
-        final PatternLayout layout = (PatternLayout) 
testConsole("config-1.2/log4j-console-TTCCLayout.properties");
-        assertEquals("%r [%t] %p %notEmpty{%ndc }- %m%n", 
layout.getConversionPattern());
-    }
-
-    @Test
-    public void testConsoleXmlLayout() throws Exception {
-        final Log4j1XmlLayout layout = (Log4j1XmlLayout) 
testConsole("config-1.2/log4j-console-XmlLayout.properties");
-        assertTrue(layout.isLocationInfo());
-        assertFalse(layout.isProperties());
-    }
-
-    @Test
-    public void testFileSimpleLayout() throws Exception {
-        final PatternLayout layout = (PatternLayout) 
testFile("config-1.2/log4j-file-SimpleLayout.properties");
-        assertEquals("%level - %m%n", layout.getConversionPattern());
-    }
+       private Layout<?> testConsole(final String configResource) throws 
Exception {
+               final Configuration configuration = configure(configResource);
+               final ConsoleAppender appender = 
configuration.getAppender("Console");
+               assertNotNull(appender);
+               assertEquals(Target.SYSTEM_ERR, appender.getTarget());
+               //
+               final LoggerConfig loggerConfig = 
configuration.getLoggerConfig("com.example.foo");
+               assertNotNull(loggerConfig);
+               assertEquals(Level.DEBUG, loggerConfig.getLevel());
+               return appender.getLayout();
+       }
+
+       private Layout<?> testFile(final String configResource) throws 
Exception {
+               final Configuration configuration = configure(configResource);
+               final FileAppender appender = configuration.getAppender("File");
+               assertNotNull(appender);
+               assertEquals("target/mylog.txt", appender.getFileName());
+               //
+               final LoggerConfig loggerConfig = 
configuration.getLoggerConfig("com.example.foo");
+               assertNotNull(loggerConfig);
+               assertEquals(Level.DEBUG, loggerConfig.getLevel());
+               return appender.getLayout();
+       }
+
+       private Configuration configure(final String configResource) throws 
URISyntaxException {
+               final URL configLocation = 
ClassLoader.getSystemResource(configResource);
+               assertNotNull(configLocation);
+               final Configuration configuration = new 
Log4j1ConfigurationFactory().getConfiguration(null, "test",
+                               configLocation.toURI());
+               assertNotNull(configuration);
+               return configuration;
+       }
+
+       @Test
+       public void testConsoleEnhancedPatternLayout() throws Exception {
+               final PatternLayout layout = (PatternLayout) testConsole(
+                               
"config-1.2/log4j-console-EnhancedPatternLayout.properties");
+               assertEquals("%d{ISO8601} [%t][%c] %-5p %properties %ndc: 
%m%n", layout.getConversionPattern());
+       }
+
+       @Test
+       public void testConsoleHtmlLayout() throws Exception {
+               final HtmlLayout layout = (HtmlLayout) 
testConsole("config-1.2/log4j-console-HtmlLayout.properties");
+               assertEquals("Headline", layout.getTitle());
+               assertTrue(layout.isLocationInfo());
+       }
+
+       @Test
+       public void testConsolePatternLayout() throws Exception {
+               final PatternLayout layout = (PatternLayout) 
testConsole("config-1.2/log4j-console-PatternLayout.properties");
+               assertEquals("%d{ISO8601} [%t][%c] %-5p: %m%n", 
layout.getConversionPattern());
+       }
+
+       @Test
+       public void testConsoleSimpleLayout() throws Exception {
+               final PatternLayout layout = (PatternLayout) 
testConsole("config-1.2/log4j-console-SimpleLayout.properties");
+               assertEquals("%level - %m%n", layout.getConversionPattern());
+       }
+
+       @Test
+       public void testConsoleTtccLayout() throws Exception {
+               final PatternLayout layout = (PatternLayout) 
testConsole("config-1.2/log4j-console-TTCCLayout.properties");
+               assertEquals("%r [%t] %p %notEmpty{%ndc }- %m%n", 
layout.getConversionPattern());
+       }
+
+       @Test
+       public void testConsoleXmlLayout() throws Exception {
+               final Log4j1XmlLayout layout = (Log4j1XmlLayout) 
testConsole("config-1.2/log4j-console-XmlLayout.properties");
+               assertTrue(layout.isLocationInfo());
+               assertFalse(layout.isProperties());
+       }
+
+       @Test
+       public void testFileSimpleLayout() throws Exception {
+               final PatternLayout layout = (PatternLayout) 
testFile("config-1.2/log4j-file-SimpleLayout.properties");
+               assertEquals("%level - %m%n", layout.getConversionPattern());
+       }
+
+       @Test
+       public void testNullAppender() throws Exception {
+               StatusLogger.getLogger().setLevel(Level.TRACE);
+               final Configuration configuration = 
configure("config-1.2/log4j-NullAppender.properties");
+               final Appender appender = 
configuration.getAppender("NullAppender");
+               assertNotNull(appender);
+               assertEquals("NullAppender", appender.getName());
+               assertTrue(appender.getClass().getName(), appender instanceof 
NullAppender);
+       }
+
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a2af6ed4/log4j-1.2-api/src/test/resources/config-1.2/log4j-NullAppender.properties
----------------------------------------------------------------------
diff --git 
a/log4j-1.2-api/src/test/resources/config-1.2/log4j-NullAppender.properties 
b/log4j-1.2-api/src/test/resources/config-1.2/log4j-NullAppender.properties
new file mode 100644
index 0000000..d89a4f4
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/config-1.2/log4j-NullAppender.properties
@@ -0,0 +1,9 @@
+###############################################################################
+#
+# Log4J 1.2 Configuration.
+#
+
+log4j.rootLogger=TRACE, NullAppender
+
+# Null Appender
+log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a2af6ed4/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/NullAppender.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/NullAppender.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/NullAppender.java
new file mode 100644
index 0000000..ad89b96
--- /dev/null
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/NullAppender.java
@@ -0,0 +1,29 @@
+package org.apache.logging.log4j.core.appender;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+
+/**
+ * An Appender that ignores log events. Use for compatibility with version 1.2.
+ */
+@Plugin(name = "Null", category = "Core", elementType = "appender", 
printObject = true)
+public class NullAppender extends AbstractAppender {
+
+    @PluginFactory
+    public static NullAppender createAppender(@PluginAttribute("name") final 
String name) {
+        return new NullAppender(name);
+    }
+
+    private NullAppender(final String name) {
+        super(name, null, null);
+        // Do nothing
+    }
+
+    @Override
+    public void append(LogEvent event) {
+        // Do nothing
+    }
+
+}

Reply via email to