logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1592258 - in /logging/log4j/log4j2/trunk: log4j-api/src/main/java/org/apache/logging/log4j/status/ log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/ log4j-core/src/test/java/org/apache/logging/log4j/junit/
Date Sat, 03 May 2014 17:58:52 GMT
Author: mattsicker
Date: Sat May  3 17:58:51 2014
New Revision: 1592258

URL: http://svn.apache.org/r1592258
Log:
Make StatusListener extend Closeable.

  - Added implementation for StatusConsoleListener to close its own stream.
  - Added empty implementation for StatusLoggerAdmin.
  - Modified StatusLogger reset and removeListener methods to close listeners before removing
them.
  - Updated InitialLoggerContext rule to reset the StatusLogger afterward. This should help
the Windows problem.

Modified:
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusListener.java
    logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/StatusLoggerAdmin.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java?rev=1592258&r1=1592257&r2=1592258&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java
Sat May  3 17:58:51 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.status;
 
+import java.io.IOException;
 import java.io.PrintStream;
 
 import org.apache.logging.log4j.Level;
@@ -43,8 +44,12 @@ public class StatusConsoleListener imple
      * to avoid creating an infinite loop of indirection!
      * @param level The Level of status messages that should appear on the console.
      * @param stream The PrintStream to write to.
+     * @throws IllegalArgumentException if the PrintStream argument is {@code null}.
      */
     public StatusConsoleListener(final Level level, final PrintStream stream) {
+        if (stream == null) {
+            throw new IllegalArgumentException("You must provide a stream to use for this
listener.");
+        }
         this.level = level;
         this.stream = stream;
     }
@@ -98,4 +103,8 @@ public class StatusConsoleListener imple
         return false;
     }
 
+    @Override
+    public void close() throws IOException {
+        this.stream.close();
+    }
 }

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusListener.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusListener.java?rev=1592258&r1=1592257&r2=1592258&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusListener.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusListener.java
Sat May  3 17:58:51 2014
@@ -16,12 +16,14 @@
  */
 package org.apache.logging.log4j.status;
 
+import java.io.Closeable;
+
 import org.apache.logging.log4j.Level;
 
 /**
  * Interface that allows implementers to be notified of events in the logging system.
  */
-public interface StatusListener {
+public interface StatusListener extends Closeable {
 
     /**
      * Called as events occur to process the StatusData.

Modified: logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java?rev=1592258&r1=1592257&r2=1592258&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
Sat May  3 17:58:51 2014
@@ -16,6 +16,8 @@
  */
 package org.apache.logging.log4j.status;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -107,6 +109,7 @@ public final class StatusLogger extends 
      * @param listener The StatusListener to remove.
      */
     public void removeListener(final StatusListener listener) {
+        closeSilently(listener);
         listenersLock.writeLock().lock();
         try {
             listeners.remove(listener);
@@ -135,8 +138,24 @@ public final class StatusLogger extends 
      * Clears the list of status events and listeners.
      */
     public void reset() {
-        listeners.clear();
-        clear();
+        listenersLock.writeLock().lock();
+        try {
+            for (final StatusListener listener : listeners) {
+                closeSilently(listener);
+            }
+        } finally {
+            listeners.clear();
+            listenersLock.writeLock().unlock();
+            // note this should certainly come after the unlock to avoid unnecessary nested
locking
+            clear();
+        }
+    }
+
+    private static void closeSilently(final Closeable resource) {
+        try {
+            resource.close();
+        } catch (final IOException ignored) {
+        }
     }
 
     /**

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/StatusLoggerAdmin.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/StatusLoggerAdmin.java?rev=1592258&r1=1592257&r2=1592258&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/StatusLoggerAdmin.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/StatusLoggerAdmin.java
Sat May  3 17:58:51 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.core.jmx;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.concurrent.Executor;
 import java.util.concurrent.atomic.AtomicLong;
@@ -129,7 +130,7 @@ public class StatusLoggerAdmin extends N
      * Returns the {@code ObjectName} of this mbean.
      * 
      * @return the {@code ObjectName}
-     * @see StatusLoggerAdminMBean#NAME
+     * @see StatusLoggerAdminMBean#PATTERN
      */
     @Override
     public ObjectName getObjectName() {
@@ -143,4 +144,9 @@ public class StatusLoggerAdmin extends N
     private long now() {
         return System.currentTimeMillis();
     }
+
+    @Override
+    public void close() throws IOException {
+        // nothing to close here
+    }
 }

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java?rev=1592258&r1=1592257&r2=1592258&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/junit/InitialLoggerContext.java
Sat May  3 17:58:51 2014
@@ -21,6 +21,7 @@ import org.apache.logging.log4j.core.Log
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.status.StatusLogger;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
@@ -34,7 +35,7 @@ public class InitialLoggerContext implem
 
     private LoggerContext context;
 
-    public InitialLoggerContext(String configLocation) {
+    public InitialLoggerContext(final String configLocation) {
         this.configLocation = configLocation;
     }
 
@@ -52,6 +53,7 @@ public class InitialLoggerContext implem
                     base.evaluate();
                 } finally {
                     Configurator.shutdown(context);
+                    StatusLogger.getLogger().reset();
                 }
             }
         };



Mime
View raw message