logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r510279 - in /logging/log4j/branches/v1_2-branch: src/java/org/apache/log4j/helpers/SyslogWriter.java src/java/org/apache/log4j/net/SyslogAppender.java tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
Date Wed, 21 Feb 2007 22:45:34 GMT
Author: carnold
Date: Wed Feb 21 14:45:33 2007
New Revision: 510279

URL: http://svn.apache.org/viewvc?view=rev&rev=510279
Log:
Bug 37282: SyslogAppender leaks descriptors

Modified:
    logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/helpers/SyslogWriter.java
    logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java

Modified: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/helpers/SyslogWriter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/helpers/SyslogWriter.java?view=diff&rev=510279&r1=510278&r2=510279
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/helpers/SyslogWriter.java
(original)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/helpers/SyslogWriter.java
Wed Feb 21 14:45:33 2007
@@ -101,7 +101,7 @@
       this.ds = new DatagramSocket();
     }
     catch (SocketException e) {
-      e.printStackTrace(); 
+      e.printStackTrace();
       LogLog.error("Could not instantiate DatagramSocket to " + host +
 			 ". All logging will FAIL.", e);
     }
@@ -128,6 +128,9 @@
   public
   void flush() {}
 
-  public
-  void close() {}
+  public void close() {
+      if (ds != null) {
+          ds.close();
+      }
+  }
 }

Modified: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java?view=diff&rev=510279&r1=510278&r2=510279
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java (original)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java Wed
Feb 21 14:45:33 2007
@@ -121,9 +121,14 @@
   public
   void close() {
     closed = true;
-    // A SyslogWriter is UDP based and needs no opening. Hence, it
-    // can't be closed. We just unset the variables here.
-    sqw = null;
+    if (sqw != null) {
+        try {
+            sqw.close();
+            sqw = null;
+        } catch(java.io.IOException ex) {
+            sqw = null;
+        }
+    }
   }
 
   private

Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java?view=diff&rev=510279&r1=510278&r2=510279
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
(original)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
Wed Feb 21 14:45:33 2007
@@ -25,6 +25,14 @@
 import org.apache.log4j.Logger;
 import org.apache.log4j.PatternLayout;
 import org.apache.log4j.VectorErrorHandler;
+import org.apache.log4j.helpers.SyslogWriter;
+
+import java.util.StringTokenizer;
+import java.io.IOException;
+import java.net.DatagramSocket;
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 
 
 /**
@@ -342,7 +350,7 @@
 
   /**
     *  Tests SyslogAppender with IPv6 address.
-    */  
+    */
   public void testIPv6() {
       SyslogAppender appender = new SyslogAppender();
       appender.setSyslogHost("::1");
@@ -350,7 +358,7 @@
 
   /**
     *  Tests SyslogAppender with IPv6 address enclosed in square brackets.
-    */  
+    */
   public void testIPv6InBrackets() {
       SyslogAppender appender = new SyslogAppender();
       appender.setSyslogHost("[::1]");
@@ -359,7 +367,7 @@
   /**
     *  Tests SyslogAppender with IPv6 address enclosed in square brackets
     *     followed by port specification.
-    */  
+    */
   public void testIPv6AndPort() {
       SyslogAppender appender = new SyslogAppender();
       appender.setSyslogHost("[::1]:1514");
@@ -368,7 +376,7 @@
   /**
     *  Tests SyslogAppender with host name enclosed in square brackets
     *     followed by port specification.
-    */  
+    */
   public void testHostNameAndPort() {
       SyslogAppender appender = new SyslogAppender();
       appender.setSyslogHost("localhost:1514");
@@ -377,9 +385,35 @@
 
   /**
     *  Tests SyslogAppender with IPv4 address followed by port specification.
-    */  
+    */
   public void testIPv4AndPort() {
       SyslogAppender appender = new SyslogAppender();
       appender.setSyslogHost("127.0.0.1:1514");
   }
+
+
+    public void testActualLogging() throws Exception {
+        DatagramSocket ds = new DatagramSocket();
+        ds.setSoTimeout(2000);
+        DatagramPacket p = new DatagramPacket(new byte[1000], 0, 1000);
+
+      SyslogAppender appender = new SyslogAppender();
+      appender.setSyslogHost("localhost:" + ds.getLocalPort());
+      appender.setName("name");
+      PatternLayout pl = new PatternLayout("%m");
+      appender.setLayout(pl);
+      appender.activateOptions();
+
+      Logger l = Logger.getRootLogger();
+      l.addAppender(appender);
+      l.info("greetings");
+      appender.close();
+      ds.receive(p);
+      ds.close();
+      String s = new String(p.getData(), 0, p.getLength());
+      StringTokenizer st = new StringTokenizer(s, "<>() ");
+      assertEquals("14", st.nextToken());
+      assertEquals("greetings", st.nextToken());
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message