logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1413034 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/appender/ core/src/main/java/org/apache/logging/log4j/core/config/ core/src/test/java/org/apache/logging/log4j/core/appender/ core/src/test/java/org...
Date Fri, 23 Nov 2012 19:20:18 GMT
Author: rgoers
Date: Fri Nov 23 19:20:15 2012
New Revision: 1413034

URL: http://svn.apache.org/viewvc?rev=1413034&view=rev
Log:
LOG4J2-128 - Add follow attribute to Console Appender.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderTest.java
      - copied, changed from r1411345, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/OutputStreamAppenderTest.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSQueueTest.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSTopicTest.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java
Fri Nov 23 19:20:15 2012
@@ -25,6 +25,7 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.helpers.Loader;
 import org.apache.logging.log4j.core.layout.PatternLayout;
 
+import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.lang.reflect.Constructor;
@@ -74,6 +75,7 @@ public final class ConsoleAppender exten
                                                  @PluginElement("filters") Filter filter,
                                                  @PluginAttr("target") String t,
                                                  @PluginAttr("name") String name,
+                                                 @PluginAttr("follow") String follow,
                                                  @PluginAttr("suppressExceptions") String
suppress) {
         if (name == null) {
             LOGGER.error("No name provided for ConsoleAppender");
@@ -82,20 +84,23 @@ public final class ConsoleAppender exten
         if (layout == null) {
             layout = PatternLayout.createLayout(null, null, null, null);
         }
+        boolean isFollow = follow == null ? false : Boolean.valueOf(follow);
         boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
         Target target = t == null ? Target.SYSTEM_OUT : Target.valueOf(t);
-        return new ConsoleAppender(name, layout, filter, getManager(target), handleExceptions);
+        return new ConsoleAppender(name, layout, filter, getManager(isFollow, target), handleExceptions);
     }
 
-    private static OutputStreamManager getManager(Target target) {
+    private static OutputStreamManager getManager(boolean follow, Target target) {
         String type = target.name();
-        OutputStream os = getOutputStream(target);
+        OutputStream os = getOutputStream(follow, target);
         return OutputStreamManager.getManager(target.name(), new FactoryData(os, type), factory);
     }
 
-    private static OutputStream getOutputStream(Target target) {
-        final PrintStream printStream = target == Target.SYSTEM_OUT ? System.out : System.err;
-        if (!System.getProperty("os.name").startsWith("Windows")) {
+    private static OutputStream getOutputStream(boolean follow, Target target) {
+        final PrintStream printStream = target == Target.SYSTEM_OUT ?
+            follow ? new PrintStream(new SystemOutStream()) : System.out :
+            follow ? new PrintStream(new SystemErrStream()) : System.err;
+        if (!System.getProperty("os.name").startsWith("Windows") || Boolean.getBoolean("log4j.skipJansi"))
{
             return printStream;
         } else {
             try {
@@ -116,6 +121,76 @@ public final class ConsoleAppender exten
     }
 
     /**
+     * An implementation of OutputStream that redirects to the
+     * current System.err.
+     *
+     */
+    private static class SystemErrStream extends OutputStream {
+        public SystemErrStream() {
+        }
+
+        @Override
+        public void close() {
+        }
+
+        @Override
+        public void flush() {
+            System.err.flush();
+        }
+
+        @Override
+        public void write(final byte[] b) throws IOException {
+            System.err.write(b);
+        }
+
+        @Override
+        public void write(final byte[] b, final int off, final int len)
+            throws IOException {
+            System.err.write(b, off, len);
+        }
+
+        @Override
+        public void write(final int b) {
+            System.err.write(b);
+        }
+    }
+
+    /**
+     * An implementation of OutputStream that redirects to the
+     * current System.out.
+     *
+     */
+    private static class SystemOutStream extends OutputStream {
+        public SystemOutStream() {
+        }
+
+        @Override
+        public void close() {
+        }
+
+        @Override
+        public void flush() {
+            System.out.flush();
+        }
+
+        @Override
+        public void write(final byte[] b) throws IOException {
+            System.out.write(b);
+        }
+
+        @Override
+        public void write(final byte[] b, final int off, final int len)
+            throws IOException {
+            System.out.write(b, off, len);
+        }
+
+        @Override
+        public void write(final int b) throws IOException {
+            System.out.write(b);
+        }
+    }
+
+    /**
      * Data to pass to factory method.
      */
     private static class FactoryData {

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
Fri Nov 23 19:20:15 2012
@@ -215,7 +215,7 @@ public class BaseConfiguration extends A
         setName(DefaultConfiguration.DEFAULT_NAME);
         Layout layout = PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}
- %msg%n",
             null, null, null);
-        Appender appender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console",
"true");
+        Appender appender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console",
"false", "true");
         appender.start();
         addAppender(appender);
         LoggerConfig root = getRootLogger();

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
Fri Nov 23 19:20:15 2012
@@ -46,7 +46,7 @@ public class DefaultConfiguration extend
         setName(DEFAULT_NAME);
         Layout layout = PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}
- %msg%n",
             null, null, null);
-        Appender appender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console",
"true");
+        Appender appender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console",
"false", "true");
         appender.start();
         addAppender(appender);
         LoggerConfig root = getRootLogger();

Copied: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderTest.java
(from r1411345, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/OutputStreamAppenderTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/OutputStreamAppenderTest.java&r1=1411345&r2=1413034&rev=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/OutputStreamAppenderTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/ConsoleAppenderTest.java
Fri Nov 23 19:20:15 2012
@@ -18,37 +18,60 @@ package org.apache.logging.log4j.core.ap
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.core.layout.PatternLayout;
 import org.apache.logging.log4j.message.SimpleMessage;
-
 import org.apache.logging.log4j.test.appender.InMemoryAppender;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.*;
 
 /**
  *
  */
-public class OutputStreamAppenderTest {
+public class ConsoleAppenderTest {
 
     private static final String LINE_SEP = System.getProperty("line.separator");
 
+    private ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+    @BeforeClass
+    public static void before() {
+        System.setProperty("log4j.skipJansi", "true");
+    }
+
+    @AfterClass
+    public static void after() {
+        System.clearProperty("log4j.skipJansi");
+    }
+
     @Test
-    public void testAppender() {
+    public void testFollow() {
+        PrintStream ps = System.out;
         Layout layout = PatternLayout.createLayout(null, null, null, null);
-        InMemoryAppender app = new InMemoryAppender("test", layout, null, false);
-        LogEvent event = new Log4jLogEvent("TestLogger", null, OutputStreamAppenderTest.class.getName(),
Level.INFO,
-            new SimpleMessage("Test"), null);
+        ConsoleAppender app = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "true", "false");
         app.start();
+        LogEvent event = new Log4jLogEvent("TestLogger", null, ConsoleAppenderTest.class.getName(),
Level.INFO,
+            new SimpleMessage("Test"), null);
+
         assertTrue("Appender did not start", app.isStarted());
+        System.setOut(new PrintStream(baos));
         app.append(event);
-        String msg = app.toString();
+        System.setOut(ps);
+        String msg = baos.toString();
         assertNotNull("No message", msg);
         assertTrue("Incorrect message: " + msg , msg.endsWith("Test" + LINE_SEP));
         app.stop();
         assertFalse("Appender did not stop", app.isStarted());
     }
+
+
 }

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSQueueTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSQueueTest.java?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSQueueTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSQueueTest.java
Fri Nov 23 19:20:15 2012
@@ -103,7 +103,7 @@ public class JMSQueueTest {
         ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
         listApp.start();
         PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null);
-        ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "true");
+        ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "false", "true");
         console.start();
         Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
         serverLogger.addAppender(console);

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSTopicTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSTopicTest.java?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSTopicTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/JMSTopicTest.java
Fri Nov 23 19:20:15 2012
@@ -103,7 +103,7 @@ public class JMSTopicTest {
         ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
         listApp.start();
         PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null);
-        ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "true");
+        ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "false", "true");
         console.start();
         Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
         serverLogger.addAppender(console);

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/net/SocketServerTest.java
Fri Nov 23 19:20:15 2012
@@ -97,7 +97,7 @@ public class SocketServerTest {
         ListAppender listApp = new ListAppender("Events", serverFilter, null, false, false);
         listApp.start();
         PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null);
-        ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "true");
+        ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT",
"Console", "false", "true");
         Logger serverLogger = ctx.getLogger(SocketServer.class.getName());
         serverLogger.addAppender(console);
         serverLogger.setAdditive(false);

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Nov 23 19:20:15 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-128" dev="rgoers" type="update">
+        Add follow attribute to Console Appender.
+      </action>
       <action issue="LOG4J2-127" dev="rgoers" type="fix">
         AbstractLogger methods were not passing Markers to the isEnabled methods.
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml?rev=1413034&r1=1413033&r2=1413034&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml Fri Nov 23 19:20:15 2012
@@ -147,6 +147,12 @@
               of "%m%n" will be used.</td>
             </tr>
             <tr>
+              <td>follow</td>
+              <td>boolean</td>
+              <td>Identifies whether the appender honors reassignments of System.out
or System.err
+                via System.setOut or System.setErr made after configuration.</td>
+            </tr>
+            <tr>
               <td>name</td>
               <td>String</td>
               <td>The name of the Appender.</td>



Mime
View raw message