[LOGBack-dev] svn commit: r455 - logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Aug 10 20:08:28 CEST 2006


Author: ceki
Date: Thu Aug 10 20:08:27 2006
New Revision: 455

Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogAppenderBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogConstants.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogWriter.java
Log:
ongoing work

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogAppenderBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogAppenderBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogAppenderBase.java	Thu Aug 10 20:08:27 2006
@@ -1,6 +1,8 @@
 package ch.qos.logback.core.net;
 
 import java.io.IOException;
+import java.net.SocketException;
+import java.net.UnknownHostException;
 
 import ch.qos.logback.core.AppenderBase;
 import ch.qos.logback.core.Layout;
@@ -11,8 +13,9 @@
   int facility;
   String facilityStr;
   String syslogHost;
+  protected String suffixPattern;
   SyslogWriter sw;
-  //private String localHostname;
+  int port = SyslogConstants.SYSLOG_PORT;
   
   public void start() {
     int errorCount = 0;
@@ -23,14 +26,26 @@
     
     facility = facilityStringToint(facilityStr);
     
-    layout = getLayout();
+    try {
+      sw = new SyslogWriter(syslogHost, port);
+    } catch (UnknownHostException e) {
+      addError("Could not create SyslogWriter", e);
+      errorCount++;
+    } catch (SocketException e) {
+      errorCount++;
+      addError("Failed to bind to a random datagram socket ", e);
+    }
+    
+    if(layout == null) {
+      layout = buildLayout(facilityStr);
+    }
     
     if(errorCount == 0) {
       super.start();
     }
   }
   
-  abstract public Layout buildLayout(int facility);
+  abstract public Layout buildLayout(String facilityStr);
   
   abstract public int getSeverityForEvent(Object eventObject);
   
@@ -39,14 +54,15 @@
     if(!isStarted()) {
       return;
     }
+    
     try {
       String msg = layout.doLayout(eventObject);
       sw.write(msg);
       sw.flush();
+    
     } catch(IOException ioe) {
       addError("Failed to send diagram to "+syslogHost, ioe);
       stop();
-      
     }
   }
 
@@ -143,4 +159,56 @@
     }
     this.facilityStr = facilityStr;
   }
+
+  /**
+   * 
+   * @return
+   */
+  public int getPort() {
+    return port;
+  }
+
+  /**
+   * The port number on the syslog server to connect to. Nornally, wou would not
+   * want to change the default value, that is 514.
+   */
+  public void setPort(int port) {
+    this.port = port;
+  }
+
+  /**
+   * You can override 
+   */
+  public Layout getLayout() {
+    return layout;
+  }
+
+  public void setLayout(Layout layout) {
+    this.layout = layout;
+  }
+  
+  @Override
+  public void stop() {
+    sw.close();
+    super.stop();
+  }
+
+  /**
+   * See {@link #setSuffixPattern(String).
+   * 
+   * @return
+   */
+  public String getSuffixPattern() {
+    return suffixPattern;
+  }
+
+  /**
+   * The <b>suffixPattern</b> option specifies the fortmat of the 
+   * non-standardized part the message sent to the syslog server.
+   * 
+   * @param pattern
+   */
+  public void setSuffixPattern(String suffixPattern) {
+    this.suffixPattern = suffixPattern;
+  }
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogConstants.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogConstants.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogConstants.java	Thu Aug 10 20:08:27 2006
@@ -7,6 +7,9 @@
  **/
 public class SyslogConstants {
   
+  static public final int SYSLOG_PORT = 514;
+  
+  
   // Following constants extracted from RFC 3164, we multiply them by 8
   // in order to precompute the facility part of PRI.
   // See RFC 3164, Section 4.1.1 for exact details.

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogWriter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogWriter.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SyslogWriter.java	Thu Aug 10 20:08:27 2006
@@ -9,24 +9,26 @@
 import java.net.UnknownHostException;
 
 /**
- * SyslogWriter is a wrapper around the {@link DatagramSocket} class 
- * so that it behaves like a {@link Writer}.
+ * SyslogWriter is a wrapper around the {@link DatagramSocket} class so that it
+ * behaves like a {@link Writer}.
  */
 class SyslogWriter extends Writer {
+
   /**
-   * The maximum length after which we discard the existing string buffer and 
+   * The maximum length after which we discard the existing string buffer and
    * start anew.
    */
-  static final int MAX_LEN = 1024;
-  
-  static final int SYSLOG_PORT = 514;
-  
+  private static final int MAX_LEN = 1024;
+
   private InetAddress address;
   private DatagramSocket ds;
   private StringBuffer buf = new StringBuffer();
-  
-  public SyslogWriter(String syslogHost) throws UnknownHostException, SocketException  {
+  final private int port;
+
+  public SyslogWriter(String syslogHost, int port) throws UnknownHostException,
+      SocketException {
     this.address = InetAddress.getByName(syslogHost);
+    this.port = port;
     this.ds = new DatagramSocket();
   }
 
@@ -36,19 +38,19 @@
 
   public void write(String str) throws IOException {
     buf.append(str);
-   
+
   }
 
   public void flush() throws IOException {
     byte[] bytes = buf.toString().getBytes();
-    DatagramPacket packet =
-      new DatagramPacket(bytes, bytes.length, address, SYSLOG_PORT);
+    DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address,
+        port);
 
     if (this.ds != null) {
       ds.send(packet);
     }
     // clean up for next round
-    if(buf.length() > MAX_LEN) {
+    if (buf.length() > MAX_LEN) {
       buf = new StringBuffer();
     } else {
       buf.setLength(0);
@@ -59,5 +61,9 @@
     address = null;
     ds = null;
   }
-}
 
+  public int getPort() {
+    return port;
+  }
+
+}



More information about the logback-dev mailing list