This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 3c1290f  Log4j 1.2 bridge class ConsoleAppender should extend 
WriterAppender and provide better compatibility with custom appenders.
3c1290f is described below

commit 3c1290f8ac99a409a1bacc48701386617740d101
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Dec 29 19:37:42 2021 -0500

    Log4j 1.2 bridge class ConsoleAppender should extend WriterAppender and
    provide better compatibility with custom appenders.
---
 .../java/org/apache/log4j/ConsoleAppender.java     | 101 ++++++++++++++++++++-
 .../java/org/apache/log4j/ConsoleAppenderTest.java |  50 ++++++++++
 .../org/apache/log4j/CustomAppenderSkeleton.java   |  75 +++++++++++++++
 .../org/apache/log4j/CustomConsoleAppender.java    |  84 +++++++++++++++++
 .../org/apache/log4j/CustomWriterAppender.java     |  74 +++++++++++++++
 src/changes/changes.xml                            |   6 ++
 6 files changed, 385 insertions(+), 5 deletions(-)

diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/ConsoleAppender.java 
b/log4j-1.2-api/src/main/java/org/apache/log4j/ConsoleAppender.java
index 9aa2cf1..9c52ca1 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/ConsoleAppender.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/ConsoleAppender.java
@@ -19,15 +19,81 @@ package org.apache.log4j;
 import org.apache.log4j.spi.LoggingEvent;
 
 /**
- * Console-appender.
+ * Placeholder for Log4j 1.2 Console Appender.
  */
-public class ConsoleAppender extends AppenderSkeleton {
+public class ConsoleAppender extends WriterAppender {
+
+    public static final String SYSTEM_OUT = "System.out";
+    public static final String SYSTEM_ERR = "System.err";
+
+    protected String target = SYSTEM_OUT;
+
+    /**
+     * Determines if the appender honors reassignments of System.out or 
System.err made after configuration.
+     */
+    private boolean follow;
+
+    /**
+     * Constructs a non-configured appender.
+     */
+    public ConsoleAppender() {
+    }
+
+    /**
+     * Constructs a configured appender.
+     *
+     * @param layout layout, may not be null.
+     */
+    public ConsoleAppender(final Layout layout) {
+        this(layout, SYSTEM_OUT);
+    }
+
+    /**
+     * Constructs a configured appender.
+     *
+     * @param layout layout, may not be null.
+     * @param target target, either "System.err" or "System.out".
+     */
+    public ConsoleAppender(final Layout layout, final String target) {
+        setLayout(layout);
+        setTarget(target);
+        activateOptions();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void append(final LoggingEvent theEvent) {
+        // NOOP
+    }
 
     /**
      * {@inheritDoc}
      */
     @Override
     public void close() {
+        // NOOP
+    }
+
+    /**
+     * Gets whether the appender honors reassignments of System.out or 
System.err made after configuration.
+     * 
+     * @return true if appender will use value of System.out or System.err in 
force at the time when logging events are
+     *         appended.
+     * @since 1.2.13
+     */
+    public boolean getFollow() {
+        return follow;
+    }
+
+    /**
+     * Gets the current value of the <b>Target</b> property. The default value 
of the option is "System.out".
+     *
+     * See also {@link #setTarget}.
+     */
+    public String getTarget() {
+        return target;
     }
 
     /**
@@ -39,10 +105,35 @@ public class ConsoleAppender extends AppenderSkeleton {
     }
 
     /**
-     * {@inheritDoc}
+     * Sets whether the appender honors reassignments of System.out or 
System.err made after configuration.
+     * 
+     * @param follow if true, appender will use value of System.out or 
System.err in force at the time when logging events
+     *        are appended.
+     * @since 1.2.13
      */
-    @Override
-    protected void append(final LoggingEvent theEvent) {
+    public void setFollow(boolean follow) {
+        this.follow = follow;
+    }
+
+    /**
+     * Sets the value of the <b>Target</b> option. Recognized values are 
"System.out" and "System.err". Any other value will
+     * be ignored.
+     */
+    public void setTarget(final String value) {
+        final String v = value.trim();
+
+        if (SYSTEM_OUT.equalsIgnoreCase(v)) {
+            target = SYSTEM_OUT;
+        } else if (SYSTEM_ERR.equalsIgnoreCase(v)) {
+            target = SYSTEM_ERR;
+        } else {
+            targetWarn(value);
+        }
+    }
+
+    void targetWarn(final String val) {
+//        LogLog.warn("["+val+"] should be System.out or System.err.");
+//        LogLog.warn("Using previously set target, System.out by default.");
     }
 
 }
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/ConsoleAppenderTest.java 
b/log4j-1.2-api/src/test/java/org/apache/log4j/ConsoleAppenderTest.java
new file mode 100644
index 0000000..00a8d00
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/ConsoleAppenderTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.log4j;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Used to test Log4j 1 support.
+ */
+public class ConsoleAppenderTest {
+
+    private ConsoleAppender consoleAppender;
+
+    @BeforeEach
+    public void beforeEach() {
+        consoleAppender = new ConsoleAppender();
+    }
+
+    @Test
+    public void testFollow() {
+        // Only really care that it compiles, behavior is secondary at this 
level.
+        consoleAppender.setFollow(true);
+        assertTrue(consoleAppender.getFollow());
+    }
+
+    @Test
+    public void testTarget() {
+        // Only really care that it compiles, behavior is secondary at this 
level.
+        consoleAppender.setTarget(ConsoleAppender.SYSTEM_OUT);
+        assertEquals(ConsoleAppender.SYSTEM_OUT, consoleAppender.getTarget());
+    }
+}
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/CustomAppenderSkeleton.java 
b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomAppenderSkeleton.java
new file mode 100644
index 0000000..37acfc4
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomAppenderSkeleton.java
@@ -0,0 +1,75 @@
+/*
+ * 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.log4j;
+
+import org.apache.log4j.spi.ErrorHandler;
+import org.apache.log4j.spi.Filter;
+import org.apache.log4j.spi.LoggingEvent;
+
+/**
+ * Used to test Log4j 1 support. All we are looking for here is that this code 
compiles.
+ */
+public class CustomAppenderSkeleton extends AppenderSkeleton {
+
+    @Override
+    protected void append(final LoggingEvent event) {
+        // NOOP @Override
+    }
+
+    @Override
+    public void close() {
+        // NOOP @Override
+    }
+
+    @SuppressWarnings({"cast", "unused"})
+    public void compilerAccessToWriterAppenderSkeletonVariables() {
+        if (closed) {
+            // Yep, it compiles.
+            final boolean compileMe = closed;
+        }
+        if (errorHandler instanceof ErrorHandler) {
+            // Yep, it compiles.
+            final ErrorHandler other = errorHandler;
+        }
+        if (headFilter instanceof Filter) {
+            // Yep, it compiles.
+            final Filter other = headFilter;
+        }
+        if (layout instanceof Layout) {
+            // Yep, it compiles.
+            final Layout other = layout;
+        }
+        if (name instanceof String) {
+            // Yep, it compiles.
+            final String other = name;
+        }
+        if (tailFilter instanceof Filter) {
+            // Yep, it compiles.
+            final Filter other = tailFilter;
+        }
+        if (threshold instanceof Priority) {
+            // Yep, it compiles.
+            final Priority other = threshold;
+        }
+    }
+
+    @Override
+    public boolean requiresLayout() {
+        // NOOP @Override
+        return false;
+    }
+}
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/CustomConsoleAppender.java 
b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomConsoleAppender.java
new file mode 100644
index 0000000..d26b32f
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomConsoleAppender.java
@@ -0,0 +1,84 @@
+/*
+ * 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.log4j;
+
+import org.apache.log4j.helpers.QuietWriter;
+import org.apache.log4j.spi.ErrorHandler;
+import org.apache.log4j.spi.Filter;
+
+/**
+ * Used to test Log4j 1 support. All we are looking for here is that this code 
compiles.
+ */
+public class CustomConsoleAppender extends ConsoleAppender {
+
+    public CustomConsoleAppender() {
+        super();
+    }
+
+    public CustomConsoleAppender(final Layout layout) {
+        super(layout);
+    }
+
+    public CustomConsoleAppender(final Layout layout, final String target) {
+        super(layout, target);
+    }
+
+    @SuppressWarnings({"cast", "unused"})
+    public void compilerAccessToConsoleAppenderInstanceVariables() {
+        if (target instanceof String) {
+            final String other = name;
+        }
+    }
+
+    @SuppressWarnings({"cast", "unused"})
+    public void compilerAccessToWriterAppenderInstanceVariables() {
+        if (immediateFlush) {
+            final boolean other = immediateFlush;
+        }
+        if (encoding instanceof String) {
+            final String other = encoding;
+        }
+        if (qw instanceof QuietWriter) {
+            final QuietWriter other = qw;
+        }
+    }
+
+    @SuppressWarnings({"cast", "unused"})
+    public void compilerAccessToWriterAppenderSkeletonVariables() {
+        if (closed) {
+            final boolean compileMe = closed;
+        }
+        if (errorHandler instanceof ErrorHandler) {
+            final ErrorHandler other = errorHandler;
+        }
+        if (headFilter instanceof Filter) {
+            final Filter other = headFilter;
+        }
+        if (layout instanceof Layout) {
+            final Layout other = layout;
+        }
+        if (name instanceof String) {
+            final String other = name;
+        }
+        if (tailFilter instanceof Filter) {
+            final Filter other = tailFilter;
+        }
+        if (threshold instanceof Priority) {
+            final Priority other = threshold;
+        }
+    }
+}
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/CustomWriterAppender.java 
b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomWriterAppender.java
new file mode 100644
index 0000000..b1be153
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/CustomWriterAppender.java
@@ -0,0 +1,74 @@
+/*
+ * 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.log4j;
+
+import org.apache.log4j.helpers.QuietWriter;
+import org.apache.log4j.spi.ErrorHandler;
+import org.apache.log4j.spi.Filter;
+
+/**
+ * Used to test Log4j 1 support. All we are looking for here is that this code 
compiles.
+ */
+public class CustomWriterAppender extends WriterAppender {
+
+    public void compilerAccessToWriterAppenderInstanceVariables() {
+        if (immediateFlush) {
+            // Yep, it compiles.
+            final boolean other = immediateFlush;
+        }
+        if (encoding instanceof String) {
+            // Yep, it compiles.
+            final String other = encoding;
+        }
+        if (qw instanceof QuietWriter) {
+            // Yep, it compiles.
+            final QuietWriter other = qw;
+        }
+    }
+
+    @SuppressWarnings({"cast", "unused"})
+    public void compilerAccessToWriterAppenderSkeletonVariables() {
+        if (closed) {
+            // Yep, it compiles.
+            final boolean compileMe = closed;
+        }
+        if (errorHandler instanceof ErrorHandler) {
+            // Yep, it compiles.
+            final ErrorHandler other = errorHandler;
+        }
+        if (headFilter instanceof Filter) {
+            // Yep, it compiles.
+            final Filter other = headFilter;
+        }
+        if (layout instanceof Layout) {
+            // Yep, it compiles.
+            final Layout other = layout;
+        }
+        if (name instanceof String) {
+            // Yep, it compiles.
+            final String other = name;
+        }
+        if (tailFilter instanceof Filter) {
+            // Yep, it compiles.
+            final Filter other = tailFilter;
+        }
+        if (threshold instanceof Priority) {
+            // Yep, it compiles.
+            final Priority other = threshold;
+        }
+    }
+}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a25e90c..530e175 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -29,6 +29,12 @@
          - "update" - Change
          - "remove" - Removed
     -->
+   <release version="2.17.2" date="20YY-MM-DD" description="GA Release 2.17.2">
+      <!-- FIXES -->
+      <action dev="ggregory" type="fix">
+               Log4j 1.2 bridge class ConsoleAppender should extend 
WriterAppender and provide better compatibility with custom appenders.
+      </action>
+       </release>
     <release version="2.17.1" date="2021-12-27" description="GA Release 
2.17.1">
       <!-- FIXES -->
       <action issue="LOG4J2-3293" dev="ggregory" type="fix">

Reply via email to