[LOGBack-dev] svn commit: r456 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/net main/java/ch/qos/logback/classic/pattern main/java/ch/qos/logback/classic/util test/java/ch/qos/logback/classic/net

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Aug 10 20:09:22 CEST 2006


Author: ceki
Date: Thu Aug 10 20:09:20 2006
New Revision: 456

Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/LevelToSyslogSeverity.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/MockSyslogServer.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
Log:
ongoing work on syslog appender

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java	Thu Aug 10 20:09:20 2006
@@ -22,23 +22,24 @@
  */
 public class SyslogAppender extends SyslogAppenderBase {
 
-  Layout layout;
-  String pattern;
-  String patternSuffix;
+  String prefixPattern;
+  
+  static final public String DEFAULT_SUFFIX_PATTERN = "[%thread] %logger %m%exception";
+  
+  public Layout buildLayout(String facilityStr) {
   
-  public Layout buildLayout(int facility) {
     PatternLayout pl = new PatternLayout();
     pl.getInstanceConverterMap().put("syslogStart", SyslogStartConverter.class.getName());
     
-    if(patternSuffix == null) {
-      patternSuffix = "%syslogStart{"+facility+"}";
+    if(prefixPattern == null) {
+      prefixPattern = "%syslogStart{"+facilityStr+"}";
     }
     
-    if(pattern == null) {
-      pattern = "%m%exception";
+    if(suffixPattern == null) {
+      suffixPattern = DEFAULT_SUFFIX_PATTERN;
     }
     
-    pl.setPattern(patternSuffix+pattern);
+    pl.setPattern(prefixPattern+suffixPattern);
     pl.setContext(getContext());
     pl.start();
     return pl;
@@ -56,22 +57,4 @@
     return LevelToSyslogSeverity.convert(event);
   }
 
-  /*
-   * Set the layout directly. Normally, for the user SyslogAppender would want to set 
-   * the patterns and not the layout directly. 
-   * 
-   * @see ch.qos.logback.core.Appender#setLayout(ch.qos.logback.core.Layout)
-   */
-  public void setLayout(Layout layout) {
-    this.layout = layout;
-  }
-
-  /*
-   * 
-   * @see ch.qos.logback.core.Appender#getLayout()
-   */
-  public Layout getLayout() {
-    return layout;
-  }
-
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java	Thu Aug 10 20:09:20 2006
@@ -33,12 +33,12 @@
     
     String facilityStr = getFirstOption();
     if (facilityStr == null) {
-      //errorCount++;
       addError("was expecting a facility string as an option");
       return;
     }
 
     facility = SyslogAppenderBase.facilityStringToint(facilityStr);
+  
     localHostName = getLocalHostname();
     try {
       simpleFormat = new SimpleDateFormat("MMM dd hh:mm:ss", new DateFormatSymbols(Locale.US));
@@ -57,7 +57,7 @@
     StringBuilder sb = new StringBuilder();
 
     int pri = facility + LevelToSyslogSeverity.convert(le);
-    System.out.println("" + pri);
+  
     sb.append("<");
     sb.append(pri);
     sb.append(">");

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/LevelToSyslogSeverity.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/LevelToSyslogSeverity.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/LevelToSyslogSeverity.java	Thu Aug 10 20:09:20 2006
@@ -33,7 +33,7 @@
     case Level.INFO_INT:
       return SyslogConstants.INFO_SEVERITY;
     case Level.DEBUG_INT:
-      return SyslogConstants.LOG_ALERT;
+      return SyslogConstants.DEBUG_SEVERITY;
     default:
       throw new IllegalArgumentException("Level " + level
           + " is not a valid level for a printing method");

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/MockSyslogServer.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/MockSyslogServer.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/MockSyslogServer.java	Thu Aug 10 20:09:20 2006
@@ -9,27 +9,49 @@
  */
 package ch.qos.logback.classic.net;
 
+import java.net.DatagramPacket;
 import java.net.DatagramSocket;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
- *
+ * 
  * @author Ceki G&uumllc&uuml;
  */
 public class MockSyslogServer extends Thread {
 
+  static final int PORT = 14805;
+
   final int loopLen;
-  DatagramSocket socket;
+
+  List<String> msgList = new ArrayList<String>();
+  boolean finished = false;
   
   MockSyslogServer(int loopLen) {
     super();
     this.loopLen = loopLen;
-    
   }
-  
+
   @Override
   public void run() {
-     for(int i = 0; i < loopLen; i++) {
-       
-     }
+    DatagramSocket socket = null;
+    try {
+      socket = new DatagramSocket(PORT);
+
+      for (int i = 0; i < loopLen; i++) {
+        byte[] buf = new byte[1024];
+        DatagramPacket packet = new DatagramPacket(buf, buf.length);
+        socket.receive(packet);
+        String msg = new String(buf, 0, packet.getLength());
+        msgList.add(msg);
+      }
+    } catch (Exception se) {
+      se.printStackTrace();
+    } finally {
+      if(socket != null) {
+        socket.close();
+      }
+    }
+    finished = true;
   }
 }

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java	Thu Aug 10 20:09:20 2006
@@ -10,6 +10,10 @@
 package ch.qos.logback.classic.net;
 
 import junit.framework.TestCase;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.core.net.SyslogConstants;
+import ch.qos.logback.core.util.StatusPrinter;
 
 public class SyslogAppenderTest extends TestCase {
 
@@ -25,4 +29,80 @@
     super.tearDown();
   }
 
+  public void testBasic() throws InterruptedException {
+    MockSyslogServer mockServer = new MockSyslogServer(1);
+    mockServer.start();
+
+    LoggerContext lc = new LoggerContext();
+    lc.setName("test");
+    SyslogAppender sa = new SyslogAppender();
+    sa.setContext(lc);
+    sa.setSyslogHost("localhost");
+    sa.setFacility("MAIL");
+    sa.setPort(MockSyslogServer.PORT);
+    sa.start();
+    assertTrue(sa.isStarted());
+    
+    String loggerName = this.getClass().getName();
+    Logger logger = lc.getLogger(loggerName);
+    logger.addAppender(sa);
+    String logMsg = "hello";
+    logger.debug(logMsg);
+    StatusPrinter.print(lc.getStatusManager());
+    
+    // wait max 2 seconds for mock server to finish. However, it should
+    // much sooner than that.
+    mockServer.join(2000);
+    assertTrue(mockServer.finished);
+    assertEquals(1, mockServer.msgList.size());
+    String msg = mockServer.msgList.get(0);
+   
+    String expected = "<"+(SyslogConstants.LOG_MAIL+SyslogConstants.DEBUG_SEVERITY)+">";
+    assertTrue(msg.startsWith(expected));
+
+    String first = "<\\d{2}>\\w{3} \\d{2} \\d{2}(:\\d{2}){2} \\w* ";
+    String threadName = Thread.currentThread().getName();
+   
+    assertTrue(msg.matches(first +"\\["+threadName+"\\] "+ loggerName +" " +logMsg));
+   
+ }
+  
+  public void testExceptoin() throws InterruptedException {
+    MockSyslogServer mockServer = new MockSyslogServer(1);
+    mockServer.start();
+
+    LoggerContext lc = new LoggerContext();
+    lc.setName("test");
+    SyslogAppender sa = new SyslogAppender();
+    sa.setContext(lc);
+    sa.setSyslogHost("localhost");
+    sa.setFacility("MAIL");
+    sa.setPort(MockSyslogServer.PORT);
+    sa.start();
+    assertTrue(sa.isStarted());
+    
+    String loggerName = this.getClass().getName();
+    Logger logger = lc.getLogger(loggerName);
+    logger.addAppender(sa);
+    String logMsg = "hello";
+    logger.debug(logMsg, new Exception("just testing"));
+    StatusPrinter.print(lc.getStatusManager());
+    
+    // wait max 2 seconds for mock server to finish. However, it should
+    // much sooner than that.
+    mockServer.join(2000);
+    assertTrue(mockServer.finished);
+    assertEquals(1, mockServer.msgList.size());
+    String msg = mockServer.msgList.get(0);
+   
+    String expected = "<"+(SyslogConstants.LOG_MAIL+SyslogConstants.DEBUG_SEVERITY)+">";
+    assertTrue(msg.startsWith(expected));
+
+    String first = "<\\d{2}>\\w{3} \\d{2} \\d{2}(:\\d{2}){2} \\w* ";
+    String threadName = Thread.currentThread().getName();
+    System.out.println(msg);
+    //assertTrue(msg.matches(first +"\\["+threadName+"\\] "+ loggerName +" " +logMsg));
+   
+    //fail("check exceptions");
+  }
 }



More information about the logback-dev mailing list