logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1133258 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src: main/java/org/apache/logging/log4j/core/appender/ main/java/org/apache/logging/log4j/core/appender/rolling/ main/java/org/apache/logging/log4j/core/layout...
Date Wed, 08 Jun 2011 06:41:32 GMT
Author: rgoers
Date: Wed Jun  8 06:41:31 2011
New Revision: 1133258

URL: http://svn.apache.org/viewvc?rev=1133258&view=rev
Log:
Add syslog and socket appenders

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SocketAppender.java
      - copied, changed from r1084050, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SyslogAppender.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/AbstractSocketManager.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramOutputStream.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramSocketManager.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/Protocol.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java
      - copied, changed from r1084027, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java
Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderBase.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ManagerFactory.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamAppender.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderBase.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderBase.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderBase.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AppenderBase.java Wed Jun  8 06:41:31 2011
@@ -94,21 +94,6 @@ public abstract class AppenderBase exten
         return name;
     }
 
-    /**
-     * Set the layout. Synchronization is not required since this can only occur before the appender is started.
-     */
-    public void setLayout(Layout layout) {
-        if (isStarted()) {
-            logger.error("The layout cannot be changed once the appender is started");
-            return;
-        }
-        if (layout == null) {
-            logger.error("The layout for appender " + getName() + " cannot be set to null");
-            return;
-        }
-        this.layout = layout;
-    }
-
     public Layout getLayout() {
         return layout;
     }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ConsoleAppender.java Wed Jun  8 06:41:31 2011
@@ -90,7 +90,7 @@ public class ConsoleAppender extends Out
 
     private static class ConsoleManagerFactory implements ManagerFactory<OutputStreamManager, FactoryData> {
 
-        public OutputStreamManager createManager(FactoryData data) {
+        public OutputStreamManager createManager(String name, FactoryData data) {
             return new OutputStreamManager(data.os, data.type);
         }
     }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java Wed Jun  8 06:41:31 2011
@@ -63,7 +63,7 @@ public class FileAppender extends Output
             isBuffered = false;
         }
         boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);;
-        boolean handleExceptions = locking == null ? true : Boolean.valueOf(suppress);
+        boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
 
         if (name == null) {
             logger.error("No name provided for FileAppender");

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java Wed Jun  8 06:41:31 2011
@@ -49,7 +49,7 @@ public class FileManager extends OutputS
         if (locking && bufferedIO) {
             locking = false;
         }
-        return (FileManager) getManager(fileName, factory, new FactoryData(fileName, append, locking, bufferedIO));
+        return (FileManager) getManager(fileName, factory, new FactoryData(append, locking, bufferedIO));
     }
 
     public FileManager(String fileName, OutputStream os, boolean append, boolean locking) {
@@ -98,13 +98,11 @@ public class FileManager extends OutputS
     }
 
     private static class FactoryData {
-        String fileName;
         boolean append;
         boolean locking;
         boolean bufferedIO;
 
-        public FactoryData(String fileName, boolean append, boolean locking, boolean bufferedIO) {
-            this.fileName = fileName;
+        public FactoryData(boolean append, boolean locking, boolean bufferedIO) {
             this.append = append;
             this.locking = locking;
             this.bufferedIO = bufferedIO;
@@ -113,8 +111,8 @@ public class FileManager extends OutputS
 
     private static class FileManagerFactory implements ManagerFactory<FileManager, FactoryData> {
 
-        public FileManager createManager(FactoryData data) {
-            File file = new File(data.fileName);
+        public FileManager createManager(String name, FactoryData data) {
+            File file = new File(name);
             final File parent = file.getParentFile();
             if (null != parent && !parent.exists()) {
                 parent.mkdirs();
@@ -122,13 +120,13 @@ public class FileManager extends OutputS
 
             OutputStream os;
             try {
-                os = new FileOutputStream(data.fileName, data.append);
+                os = new FileOutputStream(name, data.append);
                 if (data.bufferedIO) {
                     os = new BufferedOutputStream(os);
                 }
-                return new FileManager(data.fileName, os, data.append, data.locking);
+                return new FileManager(name, os, data.append, data.locking);
             } catch (FileNotFoundException ex) {
-                logger.error("FileManager (" + data.fileName + ") " + ex);
+                logger.error("FileManager (" + name + ") " + ex);
             }
             return null;
         }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ManagerFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ManagerFactory.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ManagerFactory.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/ManagerFactory.java Wed Jun  8 06:41:31 2011
@@ -5,5 +5,5 @@ package org.apache.logging.log4j.core.ap
  */
 public interface ManagerFactory<F, T> {
 
-    F createManager(T data);
+    F createManager(String name, T data);
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamAppender.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamAppender.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamAppender.java Wed Jun  8 06:41:31 2011
@@ -89,31 +89,6 @@ public abstract class OutputStreamAppend
 
     }
 
-    /**
-     * If the <b>ImmediateFlush</b> option is set to
-     * <code>true</code>, the appender will flush at the end of each
-     * write. This is the default behavior. If the option is set to
-     * <code>false</code>, then the underlying stream can defer writing
-     * to physical medium to a later time.
-     * <p/>
-     * <p>Avoiding the flush operation at the end of each append results in
-     * a performance gain of 10 to 20 percent. However, there is safety
-     * tradeoff involved in skipping flushing. Indeed, when flushing is
-     * skipped, then it is likely that the last few log events will not
-     * be recorded on disk when the application exits. This is a high
-     * price to pay even for a 20% performance gain.
-     */
-    public void setImmediateFlush(boolean value) {
-        immediateFlush = value;
-    }
-
-    /**
-     * Returns value of the <b>ImmediateFlush</b> option.
-     */
-    public boolean getImmediateFlush() {
-        return immediateFlush;
-    }
-
     @Override
     public void start() {
         if (getLayout() == null) {

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java Wed Jun  8 06:41:31 2011
@@ -58,7 +58,7 @@ public class OutputStreamManager {
         try {
             OutputStreamManager manager = map.get(name);
             if (manager == null) {
-                manager = factory.createManager(data);
+                manager = factory.createManager(name, data);
                 map.put(name, manager);
             }
             manager.count++;

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SocketAppender.java (from r1084050, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SocketAppender.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SocketAppender.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java&r1=1084050&r2=1133258&rev=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SocketAppender.java Wed Jun  8 06:41:31 2011
@@ -16,69 +16,73 @@
  */
 package org.apache.logging.log4j.core.appender;
 
-import org.apache.logging.log4j.core.Filter;
 import org.apache.logging.log4j.core.Layout;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
 import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.filter.Filters;
+import org.apache.logging.log4j.core.layout.SerializedLayout;
+import org.apache.logging.log4j.core.net.AbstractSocketManager;
+import org.apache.logging.log4j.core.net.DatagramSocketManager;
+import org.apache.logging.log4j.core.net.Protocol;
+import org.apache.logging.log4j.core.net.TCPSocketManager;
 
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.OutputStream;
+import java.net.ProtocolException;
 
 /**
  *
  */
-@Plugin(name="File",type="Core",elementType="appender",printObject=true)
-public class FileAppender extends OutputStreamAppender {
+@Plugin(name="Socket",type="Core",elementType="appender",printObject=true)
+public class SocketAppender extends OutputStreamAppender {
 
-    public final String fileName;
 
-    public FileAppender(String name, Layout layout, Filters filters, FileManager manager, String filename,
-                        boolean handleException, boolean immediateFlush) {
+    public SocketAppender(String name, Layout layout, Filters filters, AbstractSocketManager manager,
+                          boolean handleException, boolean immediateFlush) {
         super(name, layout, filters, handleException, immediateFlush, manager);
-        this.fileName = filename;
+
     }
 
     @PluginFactory
-    public static FileAppender createAppender(@PluginAttr("fileName") String fileName,
-                                              @PluginAttr("append") String append,
-                                              @PluginAttr("locking") String locking,
-                                              @PluginAttr("name") String name,
-                                              @PluginAttr("immediateFlush") String immediateFlush,
-                                              @PluginAttr("suppressExceptions") String suppress,
-                                              @PluginAttr("bufferedIO") String bufferedIO,
-                                              @PluginElement("layout") Layout layout,
-                                              @PluginElement("filters") Filters filters) {
-
-        boolean isAppend = append == null ? true : Boolean.valueOf(append);
-        boolean isLocking = locking == null ? false : Boolean.valueOf(locking);
-        boolean isBuffered = bufferedIO == null ? true : Boolean.valueOf(bufferedIO);;
-        if (isLocking && isBuffered) {
-            if (bufferedIO != null) {
-                logger.warn("Locking and buffering are mutually exclusive. No buffereing will occur for " + fileName);
-            }
-            isBuffered = false;
-        }
+    public static SocketAppender createAppender(@PluginAttr("host") String host,
+                                                @PluginAttr("port") String portNum,
+                                                @PluginAttr("protocol") String protocol,
+                                                @PluginAttr("reconnectionDelay") String delay,
+                                                @PluginAttr("name") String name,
+                                                @PluginAttr("immediateFlush") String immediateFlush,
+                                                @PluginAttr("suppressExceptions") String suppress,
+                                                @PluginElement("layout") Layout layout,
+                                                @PluginElement("filters") Filters filters) {
+
         boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);;
-        boolean handleExceptions = locking == null ? true : Boolean.valueOf(suppress);
+        boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
+        int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
+        int port = portNum == null ? 0 : Integer.parseInt(portNum);
+        if (layout == null) {
+            layout = SerializedLayout.createLayout();
+        }
 
         if (name == null) {
-            logger.error("No name provided for FileAppender");
+            logger.error("No name provided for SocketAppender");
             return null;
         }
 
-        if (fileName == null) {
-            logger.error("No filename provided for FileAppender with name "  + name);
+        AbstractSocketManager manager = createSocketManager(protocol, host, port, reconnectDelay);
+        if (manager == null) {
             return null;
         }
+        return new SocketAppender(name, layout, filters, manager, handleExceptions, isFlush);
+    }
 
-        FileManager manager = FileManager.getFileManager(fileName, isAppend, isLocking, isBuffered);
-        if (manager == null) {
-            return null;
+    protected static AbstractSocketManager createSocketManager(String protocol, String host, int port, int delay) {
+        Protocol p = Protocol.valueOf(protocol.toUpperCase());
+        switch (p) {
+            case TCP:
+                return TCPSocketManager.getSocketManager(host, port, delay);
+            case UDP:
+                return DatagramSocketManager.getSocketManager(host, port);
+            default:
+                return null;
         }
-        return new FileAppender(name, layout, filters, manager, fileName, handleExceptions, isFlush);
     }
 }

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SyslogAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SyslogAppender.java?rev=1133258&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SyslogAppender.java (added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/SyslogAppender.java Wed Jun  8 06:41:31 2011
@@ -0,0 +1,100 @@
+/*
+ * 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.logging.log4j.core.appender;
+
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttr;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.filter.Filters;
+import org.apache.logging.log4j.core.layout.RFC5424Layout;
+import org.apache.logging.log4j.core.layout.SyslogLayout;
+import org.apache.logging.log4j.core.net.AbstractSocketManager;
+
+import java.nio.charset.Charset;
+
+/**
+ *
+ */
+@Plugin(name="Syslog",type="Core",elementType="appender",printObject=true)
+public class SyslogAppender extends SocketAppender {
+
+    public static final String BSD = "bsd";
+
+    public static final String RFC5424 = "RFC5424";
+
+    public SyslogAppender(String name, Layout layout, Filters filters,
+                          boolean handleException, boolean immediateFlush, AbstractSocketManager manager) {
+        super(name, layout, filters, manager, handleException, immediateFlush);
+
+    }
+
+
+    @PluginFactory
+    public static SyslogAppender createAppender(@PluginAttr("host") String host,
+                                                @PluginAttr("port") String portNum,
+                                                @PluginAttr("protocol") String protocol,
+                                                @PluginAttr("reconnectionDelay") String delay,
+                                                @PluginAttr("name") String name,
+                                                @PluginAttr("immediateFlush") String immediateFlush,
+                                                @PluginAttr("suppressExceptions") String suppress,
+                                                @PluginAttr("facility") String facility,
+                                                @PluginAttr("id") String id,
+                                                @PluginAttr("enterpriseNumber") String ein,
+                                                @PluginAttr("includeMDC") String includeMDC,
+                                                @PluginAttr("mdcId") String mdcId,
+                                                @PluginAttr("newLine") String includeNL,
+                                                @PluginAttr("appName") String appName,
+                                                @PluginAttr("messageId") String msgId,
+                                                @PluginAttr("mdcExcludes") String excludes,
+                                                @PluginAttr("mdcINcludes") String includes,
+                                                @PluginAttr("format") String format,
+                                                @PluginElement("filters") Filters filters,
+                                                @PluginAttr("charset") String charset) {
+
+        boolean isFlush = immediateFlush == null ? true : Boolean.valueOf(immediateFlush);;
+        boolean handleExceptions = suppress == null ? true : Boolean.valueOf(suppress);
+        int reconnectDelay = delay == null ? 0 : Integer.parseInt(delay);
+        int port = portNum == null ? 0 : Integer.parseInt(portNum);
+        Charset c = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
+        if (charset != null) {
+            if (Charset.isSupported(charset)) {
+                c = Charset.forName(charset);
+            } else {
+                logger.error("Charset " + charset + " is not supported for layout, using " + c.displayName());
+            }
+        }
+        Layout layout = (format.equalsIgnoreCase(RFC5424)) ?
+            RFC5424Layout.createLayout(facility, id, ein, includeMDC, mdcId, includeNL, appName,  msgId,
+                excludes, includes, charset) :
+            SyslogLayout.createLayout(facility, includeNL, charset);
+
+        if (name == null) {
+            logger.error("No name provided for SyslogAppender");
+            return null;
+        }
+        AbstractSocketManager manager = createSocketManager(protocol, host, port, reconnectDelay);
+        if (manager == null) {
+            return null;
+        }
+
+        return new SyslogAppender(name, layout, filters, handleExceptions, isFlush, manager);
+    }
+
+
+}

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java Wed Jun  8 06:41:31 2011
@@ -30,9 +30,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
 
 /**
  *
@@ -55,7 +53,7 @@ public class RollingFileManager extends 
     public static RollingFileManager getFileManager(String fileName, String pattern, boolean append,
                                                     boolean bufferedIO) {
 
-        return (RollingFileManager) getManager(fileName, factory, new FactoryData(fileName, pattern, append,
+        return (RollingFileManager) getManager(fileName, factory, new FactoryData(pattern, append,
             bufferedIO));
     }
 
@@ -187,13 +185,11 @@ public class RollingFileManager extends 
     }
 
     private static class FactoryData {
-        String fileName;
         String pattern;
         boolean append;
         boolean bufferedIO;
 
-        public FactoryData(String fileName, String pattern, boolean append, boolean bufferedIO) {
-            this.fileName = fileName;
+        public FactoryData(String pattern, boolean append, boolean bufferedIO) {
             this.pattern = pattern;
             this.append = append;
             this.bufferedIO = bufferedIO;
@@ -202,8 +198,8 @@ public class RollingFileManager extends 
 
     private static class RollingFileManagerFactory implements ManagerFactory<RollingFileManager, FactoryData> {
 
-        public RollingFileManager createManager(FactoryData data) {
-            File file = new File(data.fileName);
+        public RollingFileManager createManager(String name, FactoryData data) {
+            File file = new File(name);
             final File parent = file.getParentFile();
             if (null != parent && !parent.exists()) {
                 parent.mkdirs();
@@ -211,7 +207,7 @@ public class RollingFileManager extends 
             try {
                 file.createNewFile();
             } catch (IOException ioe) {
-                logger.error("Unable to create file " + data.fileName, ioe);
+                logger.error("Unable to create file " + name, ioe);
                 return null;
             }
             long size = data.append ? file.length() : 0;
@@ -219,13 +215,13 @@ public class RollingFileManager extends 
 
             OutputStream os;
             try {
-                os = new FileOutputStream(data.fileName, data.append);
+                os = new FileOutputStream(name, data.append);
                 if (data.bufferedIO) {
                     os = new BufferedOutputStream(os);
                 }
-                return new RollingFileManager(data.fileName, data.pattern, os, data.append, size, time);
+                return new RollingFileManager(name, data.pattern, os, data.append, size, time);
             } catch (FileNotFoundException ex) {
-                logger.error("FileManager (" + data.fileName + ") " + ex);
+                logger.error("FileManager (" + name + ") " + ex);
             }
             return null;
         }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java Wed Jun  8 06:41:31 2011
@@ -32,11 +32,9 @@ import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.charset.Charset;
 import java.text.DecimalFormat;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.SortedMap;
@@ -62,6 +60,7 @@ public class RFC5424Layout extends Layou
     private final List<String> mdcExcludes;
     private final List<String> mdcIncludes;
     private final ListChecker checker;
+    private final ListChecker noopChecker = new NoopChecker();
     private final boolean includeNewLine;
 
     private long lastTimestamp = -1;
@@ -115,7 +114,7 @@ public class RFC5424Layout extends Layou
         } else {
             mdcIncludes = null;
         }
-        this.checker = c != null ? c : new NoopChecker();
+        this.checker = c != null ? c : noopChecker;
         LoggerContext ctx = (LoggerContext) LogManager.getContext();
         String name = ctx.getConfiguration().getName();
         configName = (name != null && name.length() > 0) ? name : null;
@@ -159,12 +158,12 @@ public class RFC5424Layout extends Layou
             StructuredDataMessage data = (StructuredDataMessage) msg;
             Map map = data.getData();
             StructuredDataId id = data.getId();
-            formatStructuredElement(id, map, buf);
+            formatStructuredElement(id, map, buf, noopChecker);
             if (includeMDC)
             {
                 int ein = id.getEnterpriseNumber() < 0 ? enterpriseNumber : id.getEnterpriseNumber();
                 StructuredDataId mdcSDID = new StructuredDataId(mdcId, ein, null, null);
-                formatStructuredElement(mdcSDID, event.getContextMap(), buf);
+                formatStructuredElement(mdcSDID, event.getContextMap(), buf, checker);
             }
             String text = data.getMessageFormat();
             if (text != null && text.length() > 0) {
@@ -266,7 +265,8 @@ public class RFC5424Layout extends Layou
         buf.append(Integer.toString(val));
     }
 
-    private void formatStructuredElement(StructuredDataId id, Map<String, Object> data, StringBuilder sb)
+    private void formatStructuredElement(StructuredDataId id, Map<String, Object> data, StringBuilder sb,
+                                         ListChecker checker)
     {
         if (id == null && defaultId == null)
         {
@@ -274,7 +274,7 @@ public class RFC5424Layout extends Layou
         }
         sb.append("[");
         sb.append(getId(id));
-        appendMap(data, sb);
+        appendMap(data, sb, checker);
         sb.append("]");
     }
 
@@ -295,7 +295,7 @@ public class RFC5424Layout extends Layou
         return sb.toString();
     }
 
-    private void appendMap(Map<String, Object> map, StringBuilder sb)
+    private void appendMap(Map<String, Object> map, StringBuilder sb, ListChecker checker)
     {
         SortedMap<String, Object> sorted = new TreeMap<String, Object>(map);
         for (Map.Entry<String, Object> entry : sorted.entrySet())

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java?rev=1133258&r1=1133257&r2=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/SyslogLayout.java Wed Jun  8 06:41:31 2011
@@ -53,7 +53,6 @@ public class SyslogLayout extends Layout
 
     public SyslogLayout(Facility facility, boolean includeNL, Charset c) {
         this.facility = facility;
-        this.header = header;
         this.includeNewLine = includeNL;
         this.charset = c;
     }

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/AbstractSocketManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/AbstractSocketManager.java?rev=1133258&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/AbstractSocketManager.java (added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/AbstractSocketManager.java Wed Jun  8 06:41:31 2011
@@ -0,0 +1,47 @@
+/*
+ * 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.logging.log4j.core.net;
+
+import org.apache.logging.log4j.core.appender.AppenderRuntimeException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.appender.OutputStreamManager;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+/**
+ *
+ */
+public abstract class AbstractSocketManager extends OutputStreamManager {
+
+    protected final InetAddress address;
+    protected final String host;
+    protected final int port;
+
+
+    public AbstractSocketManager(String name, OutputStream os, InetAddress addr, String host, int port) {
+        super(os, name);
+        this.address = addr;
+        this.host = host;
+        this.port = port;
+    }
+
+}

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramOutputStream.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramOutputStream.java?rev=1133258&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramOutputStream.java (added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramOutputStream.java Wed Jun  8 06:41:31 2011
@@ -0,0 +1,111 @@
+/*
+ * 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.logging.log4j.core.net;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.AppenderRuntimeException;
+import org.apache.logging.log4j.internal.StatusLogger;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+
+/**
+ *
+ */
+public class DatagramOutputStream extends OutputStream {
+
+    private DatagramSocket ds;
+    private InetAddress address;
+    private int port;
+
+    private byte[] data;
+
+     /**
+     * Allow subclasses access to the status logger without creating another instance.
+     */
+    protected static final Logger logger = StatusLogger.getLogger();
+
+    public DatagramOutputStream(String host, int port) {
+        this.port = port;
+        try {
+            address = InetAddress.getByName(host);
+        } catch (UnknownHostException ex) {
+            String msg = "Could not find host " + host;
+            logger.error(msg, ex);
+            throw new AppenderRuntimeException(msg, ex);
+        }
+
+        try {
+            ds = new DatagramSocket();
+        } catch(SocketException ex) {
+            String msg = "Could not instantiate DatagramSocket to " + host;
+            logger.error(msg, ex);
+            throw new AppenderRuntimeException(msg, ex);
+        }
+    }
+
+    @Override
+    public synchronized void write(byte[] bytes, int offset, int length) throws IOException {
+        copy(bytes, offset, length);
+    }
+
+    @Override
+    public synchronized void write(int i) throws IOException {
+        copy(new byte[] { (byte)(i >>> 24),(byte)(i >>> 16),(byte)(i >>> 8),(byte)i}, 0, 4);
+    }
+
+    @Override
+    public synchronized void write(byte[] bytes) throws IOException {
+        copy(bytes, 0, bytes.length);
+    }
+
+    @Override
+    public synchronized void flush() throws IOException {
+        if (this.ds != null && this.address != null) {
+            DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
+            ds.send(packet);
+        }
+        data = null;
+    }
+
+    @Override
+    public synchronized void close() throws IOException {
+        if (ds != null) {
+            if (data != null) {
+                flush();
+            }
+            ds.close();
+            ds = null;
+        }
+    }
+
+    private void copy(byte[] bytes, int offset, int length) {
+        int index = data == null ? 0 : data.length;
+        byte[] copy = new byte[length + index];
+        if (data != null) {
+            System.arraycopy(data, 0, copy, 0, index);
+        }
+        System.arraycopy(bytes, offset, copy, index, length);
+        data = copy;
+    }
+}

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramSocketManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramSocketManager.java?rev=1133258&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramSocketManager.java (added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/DatagramSocketManager.java Wed Jun  8 06:41:31 2011
@@ -0,0 +1,62 @@
+/*
+ * 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.logging.log4j.core.net;
+
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+
+import java.io.OutputStream;
+
+/**
+ *
+ */
+public class DatagramSocketManager extends AbstractSocketManager {
+
+    private static ManagerFactory factory = new DatagramSocketManagerFactory();
+
+    public static DatagramSocketManager getSocketManager(String host, int port) {
+        if (host == null && host.length() == 0) {
+            throw new IllegalArgumentException("A host name is required");
+        }
+        if (port <= 0) {
+            throw new IllegalArgumentException("A port value is required");
+        }
+        return (DatagramSocketManager) getManager("UDP:" + host +":" + port, factory, new FactoryData(host, port));
+    }
+
+    public DatagramSocketManager(OutputStream os, String name, String host, int port) {
+        super(name, os, null, host, port);
+    }
+
+
+    private static class FactoryData {
+        String host;
+        int port;
+
+        public FactoryData(String host, int port) {
+            this.host = host;
+            this.port = port;
+        }
+    }
+
+    private static class DatagramSocketManagerFactory implements ManagerFactory<DatagramSocketManager, FactoryData> {
+
+        public DatagramSocketManager createManager(String name, FactoryData data) {
+            OutputStream os = new DatagramOutputStream(data.host, data.port);
+            return new DatagramSocketManager(os, name, data.host, data.port);
+        }
+    }
+}

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/Protocol.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/Protocol.java?rev=1133258&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/Protocol.java (added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/Protocol.java Wed Jun  8 06:41:31 2011
@@ -0,0 +1,13 @@
+package org.apache.logging.log4j.core.net;
+
+/**
+ *
+ */
+public enum Protocol {
+
+    TCP, UDP;
+
+    public boolean equals(String name) {
+        return this.name().equalsIgnoreCase(name);
+    }
+}

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java?rev=1133258&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java (added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/net/TCPSocketManager.java Wed Jun  8 06:41:31 2011
@@ -0,0 +1,175 @@
+/*
+ * 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.logging.log4j.core.net;
+
+import org.apache.logging.log4j.core.appender.AppenderRuntimeException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.appender.OutputStreamManager;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+/**
+ *
+ */
+public class TCPSocketManager extends AbstractSocketManager {
+
+    private final int reconnectionDelay;
+
+    private Reconnector connector = null;
+
+    private Socket socket;
+
+    private final boolean retry;
+
+    /**
+      The default port number of remote logging server (4560).
+     */
+    private static final int DEFAULT_PORT = 4560;
+
+    /**
+      The default reconnection delay (30000 milliseconds or 30 seconds).
+     */
+    public static final int DEFAULT_RECONNECTION_DELAY   = 30000;
+
+    private static ManagerFactory factory = new TCPSocketManagerFactory();
+
+    public static TCPSocketManager getSocketManager(String host, int port, int delay) {
+        if (host == null && host.length() == 0) {
+            throw new IllegalArgumentException("A host name is required");
+        }
+        if (port <= 0) {
+            port = DEFAULT_PORT;
+        }
+        if (delay == 0) {
+            delay = DEFAULT_RECONNECTION_DELAY;
+        }
+        return (TCPSocketManager) getManager("TCP:" + host +":" + port, factory, new FactoryData(host, port, delay));
+    }
+
+
+    public TCPSocketManager(String name, OutputStream os, Socket sock, InetAddress addr, String host, int port, int delay) {
+        super(name, os, addr, host, port);
+        this.reconnectionDelay = delay;
+        this.socket = sock;
+        retry = delay > 0;
+    }
+
+    @Override
+    protected synchronized void write(byte[] bytes, int offset, int length)  {
+        try {
+            getOutputStream().write(bytes, offset, length);
+            socket.setSendBufferSize(length);
+        } catch (IOException ex) {
+            if (retry && connector == null) {
+                connector = new Reconnector(this);
+                connector.setDaemon(true);
+                connector.setPriority(Thread.MIN_PRIORITY);
+                connector.start();
+            }
+            String msg = "Error writing to " + getName();
+            throw new AppenderRuntimeException(msg, ex);
+        }
+    }
+
+    @Override
+    protected synchronized void close() {
+        super.close();
+        if (connector != null) {
+            connector.shutdown();
+            connector.interrupt();
+            connector = null;
+        }
+    }
+
+    private class Reconnector extends Thread {
+
+        boolean shutdown = false;
+
+        private final Object owner;
+
+        public Reconnector(OutputStreamManager owner) {
+            this.owner = owner;
+        }
+
+        public void shutdown() {
+            shutdown = true;
+        }
+
+        public void run() {
+            while (!shutdown) {
+                try {
+                    sleep(reconnectionDelay);
+                    Socket sock = new Socket(address, port);
+                    OutputStream newOS = sock.getOutputStream();
+                    synchronized (owner) {
+                        try {
+                            getOutputStream().close();
+                        } catch (IOException ioe) {
+                            // Ignore this.
+                        }
+
+                        setOutputStream(newOS);
+                        socket = sock;
+                        connector = null;
+                    }
+                    logger.debug("Connection to " + host + ":" + port + " reestablished.");
+                } catch (InterruptedException ie) {
+                    logger.debug("Reconnection interrupted.");
+                } catch (ConnectException ex) {
+                    logger.debug(host + ":" + port + " refused connection");
+                } catch (IOException ioe) {
+                    logger.debug("Unable to reconnect to " + host + ":" + port);
+                }
+            }
+        }
+    }
+
+    private static class FactoryData {
+        String host;
+        int port;
+        int delay;
+
+        public FactoryData(String host, int port, int delay) {
+            this.host = host;
+            this.port = port;
+            this.delay = delay;
+        }
+    }
+
+    private static class TCPSocketManagerFactory implements ManagerFactory<TCPSocketManager, FactoryData> {
+
+        public TCPSocketManager createManager(String name, FactoryData data) {
+            try {
+                InetAddress address = InetAddress.getByName(data.host);
+                Socket socket = new Socket(data.host, data.port);
+                OutputStream os = socket.getOutputStream();
+                return new TCPSocketManager(name, os, socket, address, data.host, data.port, data.delay);
+            } catch (UnknownHostException ex) {
+                logger.error("Could not find address of " + data.host, ex);
+            } catch (IOException ex) {
+                logger.error("TCPSocketManager (" + name + ") " + ex);
+            }
+            return null;
+        }
+    }
+
+}

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java (from r1084027, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java&r1=1084027&r2=1133258&rev=1133258&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SocketAppenderTest.java Wed Jun  8 06:41:31 2011
@@ -17,225 +17,179 @@
 package org.apache.logging.log4j.core.appender;
 
 import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.ThreadContext;
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.Log4jLogEvent;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
 import org.junit.After;
-import org.junit.Test;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
+import org.junit.Test;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
-import java.io.DataInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.Map;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.TimeUnit;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
 
 /**
  *
  */
-public class FileAppenderTest {
+public class SocketAppenderTest {
+
+    private static final String HOST = "localhost";
+    private static final String PORT = "8199";
+    private static final int PORTNUM = Integer.parseInt(PORT);
+
+    private static BlockingQueue<LogEvent> list = new ArrayBlockingQueue<LogEvent>(10);
 
-    private static final String FILENAME = "target/fileAppenderTest.log";
-    private static final int THREADS = 2;
+    private static TCPSocketServer tcp;
+    private static UDPSocketServer udp;
+
+    LoggerContext ctx = (LoggerContext) LogManager.getContext();
+    Logger root = ctx.getLogger("SocketAppenderTest");
+
+    private static int tcpCount = 0;
+    private static int udpCount = 0;
 
     @BeforeClass
-    public static void setupClass() {
-        deleteFile();
+    public static void setupClass() throws Exception {
+        tcp = new TCPSocketServer();
+        tcp.start();
+        udp = new UDPSocketServer();
+        udp.start();
+        ((LoggerContext) LogManager.getContext()).reconfigure();
     }
 
     @AfterClass
     public static void cleanupClass() {
-        deleteFile();
-        assertTrue("Manager for " + FILENAME + " not removed", !OutputStreamManager.hasManager(FILENAME));
+        tcp.shutdown();
+        udp.shutdown();
     }
 
     @After
     public void teardown() {
-        deleteFile();
-    }
-
-    @Test
-    public void testAppender() throws Exception {
-        writer(false, 1, "test");
-        verifyFile(1);
+        Map<String,Appender> map = root.getAppenders();
+        for (Map.Entry<String, Appender> entry : map.entrySet()) {
+            Appender app = entry.getValue();
+            root.removeAppender(app);
+            app.stop();
+        }
+        tcpCount = 0;
+        udpCount = 0;
     }
 
     @Test
-    public void testLockingAppender() throws Exception {
-        writer(true, 1, "test");
-        verifyFile(1);
-    }
+    public void testTCPAppender() throws Exception {
 
-    @Test
-    public void testMultipleAppenders() throws Exception {
-        ExecutorService pool = Executors.newFixedThreadPool(THREADS);
-        int count = 10;
-        Runnable runnable = new FileWriterRunnable(false, count);
-        for (int i=0; i < THREADS; ++i) {
-            pool.execute(runnable);
-        }
-        pool.shutdown();
-        pool.awaitTermination(10, TimeUnit.SECONDS);
-        verifyFile(THREADS * count);
+        SocketAppender appender = SocketAppender.createAppender("localhost", PORT, "tcp", "-1",
+            "Test", null, null, null, null);
+        appender.start();
+
+        // set appender on root and set level to debug
+        root.addAppender(appender);
+        root.setLevel(Level.DEBUG);
+        root.debug("This is a test message");
+        LogEvent event = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", event);
+        assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a test message"));
+        assertTrue("Message not delivered via TCP", tcpCount > 0);
     }
 
 
     @Test
-    public void testMultipleLockedAppenders() throws Exception {
-        ExecutorService pool = Executors.newFixedThreadPool(THREADS);
-        int count = 10;
-        Runnable runnable = new FileWriterRunnable(true, count);
-        for (int i=0; i < THREADS; ++i) {
-            pool.execute(runnable);
-        }
-        pool.shutdown();
-        pool.awaitTermination(10, TimeUnit.SECONDS);
-        verifyFile(THREADS * count);
-    }
-
+    public void testUDPAppender() throws Exception {
 
-    //@Test
-    public void testMultipleVMs() throws Exception {
-
-        String classPath = System.getProperty("java.class.path");
-        Integer count = 10;
-        int processes = 3;
-        Process[] process = new Process[processes];
-        ProcessBuilder[] builders = new ProcessBuilder[processes];
-        for (int index=0; index < processes; ++index) {
-            builders[index] = new ProcessBuilder("java","-cp", classPath, ProcessTest.class.getName(),
-                "Process " + index, count.toString(), "true");
-        }
-        for (int index=0; index < processes; ++index) {
-            process[index] = builders[index].start();
-        }
-        for (int index=0; index < processes; ++index) {
-            Process p = process[index];
-            //System.out.println("Process " + index + " exited with " + p.waitFor());
-            InputStream is = p.getInputStream();
-            InputStreamReader isr = new InputStreamReader(is);
-            BufferedReader br = new BufferedReader(isr);
-            String line;
-            while ((line = br.readLine()) != null) {
-                System.out.println(line);
-            }
-
-            p.destroy();
+        SocketAppender appender = SocketAppender.createAppender("localhost", PORT, "udp", "-1",
+            "Test", null, null, null, null);
+        appender.start();
+
+        // set appender on root and set level to debug
+        root.addAppender(appender);
+        root.setLevel(Level.DEBUG);
+        root.debug("This is a test message");
+        LogEvent event = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", event);
+        assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a test message"));
+        assertTrue("Message not delivered via UDP", udpCount > 0);
+    }
+
+    public static class UDPSocketServer extends Thread {
+        private final DatagramSocket sock;
+        private boolean shutdown = false;
+        private Thread thread;
+
+        public UDPSocketServer() throws IOException {
+            this.sock = new DatagramSocket(PORTNUM);
+        }
+
+        public void shutdown() {
+            this.shutdown = true;
+            thread.interrupt();
         }
-        verifyFile(count * processes);
-    }
 
-    private static void writer(boolean lock, int count, String name) throws Exception {
-        Layout layout = new PatternLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN);
-        FileManager manager = FileManager.getFileManager(FILENAME, true, lock, false);
-        FileAppender app = new FileAppender("test", layout, null, manager, FILENAME, false, false);
-        Thread t = Thread.currentThread();
-        app.start();
-        assertTrue("Appender did not start", app.isStarted());
-        for (int i=0; i < count; ++i) {
-            LogEvent event = new Log4jLogEvent("TestLogger", null, FileAppenderTest.class.getName(), Level.INFO,
-                new SimpleMessage("Test"), null, null, null, name, null, System.currentTimeMillis());
+        public void run() {
+            this.thread = Thread.currentThread();
+            byte[] bytes = new byte[4096];
+            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
             try {
-                app.append(event);
-                t.sleep(25);  // Give up control long enough for another thread/process to occasionally do something.
+                while (!shutdown) {
+                    sock.receive(packet);
+                    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(packet.getData()));
+                    ++udpCount;
+                    list.add((LogEvent) ois.readObject());
+                }
             } catch (Exception ex) {
-                throw ex;
+                if (!shutdown) {
+                    throw new RuntimeException(ex);
+                }
             }
         }
-        app.stop();
-        assertFalse("Appender did not stop", app.isStarted());
     }
 
-    private void verifyFile(int count) throws Exception {
-        //String expected = "[\\w]* \\[\\s*\\] INFO TestLogger - Test$";
-        String expected = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3} \\[[^\\]]*\\] INFO TestLogger - Test";
-        Pattern pattern = Pattern.compile(expected);
-        DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(FILENAME)));
-        int counter = 0;
-        String str = "";
-        while (is.available() != 0) {
-            str = is.readLine();
-            //System.out.println(str);
-            ++counter;
-            Matcher matcher = pattern.matcher(str);
-            assertTrue("Bad data: " + str, matcher.matches());
-        }
-        assertTrue("Incorrect count: was " + counter + " should be " + counter, count == counter);
-
-    }
+    public static class TCPSocketServer extends Thread {
 
+        private final ServerSocket sock;
+        private boolean shutdown = false;
+        private Thread thread;
 
-    private static void deleteFile() {
-        File file = new File(FILENAME);
-        if (file.exists()) {
-            file.delete();
+        public TCPSocketServer() throws IOException {
+            this.sock = new ServerSocket(PORTNUM);
         }
-    }
-
-    public class FileWriterRunnable implements Runnable {
-        private final boolean lock;
-        private final int count;
 
-        public FileWriterRunnable(boolean lock, int count)  {
-            this.lock = lock;
-            this.count = count;
+        public void shutdown() {
+            this.shutdown = true;
+            thread.interrupt();
         }
-        public void run() {
-            Thread thread = Thread.currentThread();
 
+        public void run() {
+            this.thread = Thread.currentThread();
             try {
-                writer(lock, count, thread.getName());
-
+                while (!shutdown) {
+                    Socket socket = sock.accept();
+                    if (socket != null) {
+                        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
+                        ++tcpCount;
+                        list.add((LogEvent) ois.readObject());
+                    }
+                }
             } catch (Exception ex) {
-                throw new RuntimeException(ex);
+                if (!shutdown) {
+                    throw new RuntimeException(ex);
+                }
             }
         }
     }
 
-    public static class ProcessTest {
-
-        public static void main(String[] args) {
-
-            if (args.length != 3) {
-                System.out.println("Required arguments 'id', 'count' and 'lock' not provided");
-                System.exit(-1);
-            }
-            String id = args[0];
-
-            int count = Integer.parseInt(args[1]);
-
-            if (count <= 0) {
-                System.out.println("Invalid count value: " + args[1]);
-                System.exit(-1);
-            }
-            boolean lock = Boolean.parseBoolean(args[2]);
-
-            //System.out.println("Got arguments " + id + ", " + count + ", " + lock);
-
-            try {
-                writer(lock, count, id);
-                //thread.sleep(50);
-
-            } catch (Exception ex) {
-                throw new RuntimeException(ex);
-            }
-
-
-        }
-    }
 }

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java?rev=1133258&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java (added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/appender/SyslogAppenderTest.java Wed Jun  8 06:41:31 2011
@@ -0,0 +1,279 @@
+/*
+ * 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.logging.log4j.core.appender;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.message.StructuredDataMessage;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class SyslogAppenderTest {
+
+    private static final String HOST = "localhost";
+    private static final String PORT = "8199";
+    private static final int PORTNUM = Integer.parseInt(PORT);
+
+    private static BlockingQueue<String> list = new ArrayBlockingQueue<String>(10);
+
+    private static TCPSocketServer tcp;
+    private static UDPSocketServer udp;
+
+    LoggerContext ctx = (LoggerContext) LogManager.getContext();
+    Logger root = ctx.getLogger("SyslogAppenderTest");
+
+    private static int tcpCount = 0;
+    private static int udpCount = 0;
+
+    private static final String line1 =
+        "TestApp - Audit [Transfer@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"]" +
+        "[RequestContext@18060 ipAddress=\"192.168.0.120\" loginId=\"JohnDoe\"] Transfer Complete\n";
+
+    @BeforeClass
+    public static void setupClass() throws Exception {
+        tcp = new TCPSocketServer();
+        tcp.start();
+        udp = new UDPSocketServer();
+        udp.start();
+        ((LoggerContext) LogManager.getContext()).reconfigure();
+    }
+
+    @AfterClass
+    public static void cleanupClass() {
+        tcp.shutdown();
+        udp.shutdown();
+    }
+
+    @After
+    public void teardown() {
+        Map<String,Appender> map = root.getAppenders();
+        for (Map.Entry<String, Appender> entry : map.entrySet()) {
+            Appender app = entry.getValue();
+            root.removeAppender(app);
+            app.stop();
+        }
+        tcpCount = 0;
+        udpCount = 0;
+    }
+
+    @Test
+    public void testTCPAppender() throws Exception {
+        SyslogAppender appender = createAppender("tcp", "bsd");
+        appender.start();
+
+        // set appender on root and set level to debug
+        root.addAppender(appender);
+        root.setAdditive(false);
+        root.setLevel(Level.DEBUG);
+        root.debug("This is a test message");
+        String msg = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", msg);
+        assertTrue("Incorrect msg: " + msg, msg.endsWith("This is a test message\n"));
+        assertTrue("Message not delivered via TCP", tcpCount > 0);
+        root.debug("This is test message 2");
+        msg = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", msg);
+        assertTrue("Incorrect msg: " + msg, msg.endsWith("This is test message 2\n"));
+        assertTrue("Message not delivered via TCP", tcpCount > 1);
+    }
+
+
+    @Test
+    public void testTCPStructuredAppender() throws Exception {
+        SyslogAppender appender = createAppender("tcp", "RFC5424");
+        appender.start();
+
+        // set appender on root and set level to debug
+        root.addAppender(appender);
+        root.setLevel(Level.DEBUG);
+        root.setAdditive(false);
+        ThreadContext.put("loginId", "JohnDoe");
+        ThreadContext.put("ipAddress", "192.168.0.120");
+        ThreadContext.put("locale", Locale.US.getDisplayName());
+        StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
+        msg.put("ToAccount", "123456");
+        msg.put("FromAccount", "123457");
+        msg.put("Amount", "200.00");
+        root.info(MarkerManager.getMarker("EVENT"), msg);
+        String str = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", str);
+        assertTrue("Incorrect msg: " + str, str.endsWith(line1));
+        assertTrue("Message not delivered via TCP", tcpCount > 0);
+    }
+
+
+    @Test
+    public void testUDPAppender() throws Exception {
+
+        SyslogAppender appender = createAppender("udp", "bsd");
+        appender.start();
+
+        // set appender on root and set level to debug
+        root.addAppender(appender);
+        root.setLevel(Level.DEBUG);
+        root.setAdditive(false);
+        root.debug("This is a test message");
+        String str = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", str);
+        assertTrue("Incorrect msg: " + str, str.endsWith("This is a test message\n"));
+        assertTrue("Message not delivered via UDP", udpCount > 0);
+        root.removeAppender(appender);
+        appender.stop();
+    }
+
+
+    @Test
+    public void testUDPStructuredAppender() throws Exception {
+        SyslogAppender appender = createAppender("udp", "RFC5424");
+        appender.start();
+
+        // set appender on root and set level to debug
+        root.addAppender(appender);
+        root.setLevel(Level.DEBUG);
+        root.setAdditive(false);
+        ThreadContext.put("loginId", "JohnDoe");
+        ThreadContext.put("ipAddress", "192.168.0.120");
+        ThreadContext.put("locale", Locale.US.getDisplayName());
+        StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
+        msg.put("ToAccount", "123456");
+        msg.put("FromAccount", "123457");
+        msg.put("Amount", "200.00");
+        root.info(MarkerManager.getMarker("EVENT"), msg);
+        String str = list.poll(3, TimeUnit.SECONDS);
+        assertNotNull("No event retrieved", str);
+        assertTrue("Incorrect msg: " + str, str.endsWith(line1));
+        assertTrue("Message not delivered via TCP", udpCount > 0);
+        root.removeAppender(appender);
+        appender.stop();
+    }
+
+    private SyslogAppender createAppender(String protocol, String format) {
+        return SyslogAppender.createAppender("localhost", PORT, protocol, "-1", "Test", "true", "false", "LOCAL0", "Audit",
+            "18060", "true", "RequestContext", "true", "TestApp", "Test", null, "ipAddress,loginId", format, null, null);
+    }
+
+    public static class UDPSocketServer extends Thread {
+        private final DatagramSocket sock;
+        private boolean shutdown = false;
+        private Thread thread;
+
+        public UDPSocketServer() throws IOException {
+            this.sock = new DatagramSocket(PORTNUM);
+        }
+
+        public void shutdown() {
+            this.shutdown = true;
+            thread.interrupt();
+        }
+
+        public void run() {
+            this.thread = Thread.currentThread();
+            byte[] bytes = new byte[4096];
+            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
+            try {
+                while (!shutdown) {
+                    sock.receive(packet);
+                    String str = new String(packet.getData(), 0, packet.getLength());
+                    ++udpCount;
+                    list.add(str);
+                }
+            } catch (Exception ex) {
+                if (!shutdown) {
+                    throw new RuntimeException(ex);
+                }
+            }
+        }
+    }
+
+    public static class TCPSocketServer extends Thread {
+
+        private final ServerSocket sock;
+        private boolean shutdown = false;
+        private Thread thread;
+
+        public TCPSocketServer() throws IOException {
+            this.sock = new ServerSocket(PORTNUM);
+        }
+
+        public void shutdown() {
+            this.shutdown = true;
+            thread.interrupt();
+        }
+
+        public void run() {
+            this.thread = Thread.currentThread();
+            try {
+                byte[] buffer = new byte[4096];
+                while (!shutdown) {
+                    Socket socket = sock.accept();
+                    StringBuilder sb = new StringBuilder();
+                    if (socket != null) {
+                        InputStream in = socket.getInputStream();
+                        int i = in.read(buffer, 0, buffer.length);
+                        while (i != -1) {
+                            if (i < buffer.length) {
+                                String line = new String(buffer, 0, i);
+                                ++tcpCount;
+                                list.add(line);
+                                i = in.read(buffer, 0, buffer.length);
+                            } else if (i == 0) {
+                                System.out.println("No data received");
+                            } else {
+                                System.out.println("Message too long");
+                            }
+                        }
+
+                        socket.close();
+                    }
+                }
+            } catch (Exception ex) {
+                if (!shutdown) {
+                    throw new RuntimeException(ex);
+                }
+            }
+        }
+    }
+
+}



---------------------------------------------------------------------
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