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

noreply.ceki at qos.ch noreply.ceki at qos.ch
Wed Aug 9 18:53:51 CEST 2006


Author: ceki
Date: Wed Aug  9 18:53:49 2006
New Revision: 421

Added:
   logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/
   logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/MockSyslogServer.java
   logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/PackageTest.java
   logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
Modified:
   logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
   logback/classic/trunk/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java
   logback/classic/trunk/src/test/java/ch/qos/logback/classic/AllTest.java
   logback/classic/trunk/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java
Log:
Syslog relateed work

Modified: logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
==============================================================================
--- logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java	(original)
+++ logback/classic/trunk/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java	Wed Aug  9 18:53:49 2006
@@ -10,6 +10,7 @@
 package ch.qos.logback.classic.net;
 
 import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.pattern.SyslogStartConverter;
 import ch.qos.logback.classic.spi.LoggingEvent;
 import ch.qos.logback.classic.util.LevelToSyslogSeverity;
 import ch.qos.logback.core.Layout;
@@ -22,11 +23,22 @@
 public class SyslogAppender extends SyslogAppenderBase {
 
   Layout layout;
+  String pattern;
+  String patternSuffix;
   
-  public Layout makeDefaultLayout(int facility) {
+  public Layout buildLayout(int facility) {
     PatternLayout pl = new PatternLayout();
-    pl.getInstanceConverterMap().put("syslogStart", "sfsdf");
-    pl.setPattern("%syslogStart{"+facility+"} ");
+    pl.getInstanceConverterMap().put("syslogStart", SyslogStartConverter.class.getName());
+    
+    if(patternSuffix == null) {
+      patternSuffix = "%syslogStart{"+facility+"}";
+    }
+    
+    if(pattern == null) {
+      pattern = "%m%exception";
+    }
+    
+    pl.setPattern(patternSuffix+pattern);
     pl.setContext(getContext());
     pl.start();
     return pl;
@@ -45,7 +57,8 @@
   }
 
   /*
-   * Set the layout.
+   * 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)
    */

Modified: logback/classic/trunk/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java
==============================================================================
--- logback/classic/trunk/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java	(original)
+++ logback/classic/trunk/src/main/java/ch/qos/logback/classic/pattern/SyslogStartConverter.java	Wed Aug  9 18:53:49 2006
@@ -11,49 +11,44 @@
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.text.DateFormatSymbols;
 import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.List;
-import java.util.TimeZone;
+import java.util.Locale;
 
 import ch.qos.logback.classic.spi.LoggingEvent;
 import ch.qos.logback.classic.util.LevelToSyslogSeverity;
-import ch.qos.logback.core.CoreGlobal;
+import ch.qos.logback.core.net.SyslogAppenderBase;
 
 public class SyslogStartConverter extends ClassicConverter {
 
   long lastTimestamp = -1;
   String timesmapStr = null;
-  SimpleDateFormat simpleFormat = null;
+  SimpleDateFormat simpleFormat;
   String localHostName;
   int facility;
 
   public void start() {
-
-    String datePattern = getFirstOption();
-    if (datePattern == null) {
-      datePattern = CoreGlobal.ISO8601_PATTERN;
+    int errorCount = 0;
+    
+    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(datePattern);
-      // maximumCacheValidity =
-      // CachedDateFormat.getMaximumCacheValidity(pattern);
+      simpleFormat = new SimpleDateFormat("MMM dd hh:mm:ss", new DateFormatSymbols(Locale.US));
     } catch (IllegalArgumentException e) {
-      getLogger().warn(
-          "Could not instantiate SimpleDateFormat with pattern " + datePattern,
-          e);
-      // default to the ISO8601 format
-      simpleFormat = new SimpleDateFormat(CoreGlobal.ISO8601_PATTERN);
+      addError("Could not instantiate SimpleDateFormat", e);
+      errorCount++;
     }
 
-    List optionList = getOptionList();
-
-    // if the option list contains a TZ option, then set it.
-    if (optionList != null && optionList.size() > 1) {
-      TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
-      simpleFormat.setTimeZone(tz);
+    if(errorCount == 0) {
+      super.start();
     }
   }
 

Modified: logback/classic/trunk/src/test/java/ch/qos/logback/classic/AllTest.java
==============================================================================
--- logback/classic/trunk/src/test/java/ch/qos/logback/classic/AllTest.java	(original)
+++ logback/classic/trunk/src/test/java/ch/qos/logback/classic/AllTest.java	Wed Aug  9 18:53:49 2006
@@ -22,6 +22,7 @@
     suite.addTest(ch.qos.logback.classic.PackageTest.suite());
     suite.addTest(ch.qos.logback.classic.pattern.PackageTest.suite());
     suite.addTest(ch.qos.logback.classic.joran.PackageTest.suite());
+    suite.addTest(ch.qos.logback.classic.net.PackageTest.suite());
     return suite;
   }
 }

Added: logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/MockSyslogServer.java
==============================================================================
--- (empty file)
+++ logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/MockSyslogServer.java	Wed Aug  9 18:53:49 2006
@@ -0,0 +1,35 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.classic.net;
+
+import java.net.DatagramSocket;
+
+/**
+ *
+ * @author Ceki G&uumllcü
+ */
+public class MockSyslogServer extends Thread {
+
+  final int loopLen;
+  DatagramSocket socket;
+  
+  MockSyslogServer(int loopLen) {
+    super();
+    this.loopLen = loopLen;
+    
+  }
+  
+  @Override
+  public void run() {
+     for(int i = 0; i < loopLen; i++) {
+       
+     }
+  }
+}

Added: logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/PackageTest.java
==============================================================================
--- (empty file)
+++ logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/PackageTest.java	Wed Aug  9 18:53:49 2006
@@ -0,0 +1,24 @@
+/**
+ * LOGBack: the reliable, fast and flexible logging library for Java.
+ *
+ * Copyright (C) 1999-2006, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.net;
+
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class PackageTest extends TestCase {
+  
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(SyslogAppenderTest.class);
+    return suite;
+  }
+}
\ No newline at end of file

Added: logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
==============================================================================
--- (empty file)
+++ logback/classic/trunk/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java	Wed Aug  9 18:53:49 2006
@@ -0,0 +1,28 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.classic.net;
+
+import junit.framework.TestCase;
+
+public class SyslogAppenderTest extends TestCase {
+
+  public SyslogAppenderTest(String arg0) {
+    super(arg0);
+  }
+
+  protected void setUp() throws Exception {
+    super.setUp();
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+}

Modified: logback/classic/trunk/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java
==============================================================================
--- logback/classic/trunk/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java	(original)
+++ logback/classic/trunk/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java	Wed Aug  9 18:53:49 2006
@@ -19,20 +19,9 @@
 import org.slf4j.MarkerFactory;
 
 import ch.qos.logback.classic.Level;
-import ch.qos.logback.classic.pattern.CallerDataConverter;
-import ch.qos.logback.classic.pattern.ClassOfCallerConverter;
-import ch.qos.logback.classic.pattern.FileOfCallerConverter;
-import ch.qos.logback.classic.pattern.LevelConverter;
-import ch.qos.logback.classic.pattern.LineOfCallerConverter;
-import ch.qos.logback.classic.pattern.LineSeparatorConverter;
-import ch.qos.logback.classic.pattern.LoggerConverter;
-import ch.qos.logback.classic.pattern.MessageConverter;
-import ch.qos.logback.classic.pattern.MethodOfCallerConverter;
-import ch.qos.logback.classic.pattern.RelativeTimeConverter;
-import ch.qos.logback.classic.pattern.ThreadConverter;
-import ch.qos.logback.classic.pattern.ThrowableInformationConverter;
 import ch.qos.logback.classic.spi.LoggingEvent;
 import ch.qos.logback.core.CoreGlobal;
+import ch.qos.logback.core.net.SyslogConstants;
 import ch.qos.logback.core.pattern.DynamicConverter;
 import ch.qos.logback.core.pattern.FormatInfo;
 
@@ -82,7 +71,7 @@
       StringBuffer buf = new StringBuffer();
       converter.write(buf, le);
       // the number below should be the line number of the previous line
-      assertEquals("83", buf.toString());
+      assertEquals("72", buf.toString());
     }
   }
 
@@ -281,5 +270,23 @@
       }
     }
   }
+  
+  public void testSyslogStart() throws Exception {
+    {
+      DynamicConverter converter = new SyslogStartConverter();
+      this.optionList.clear();
+      this.optionList.add("MAIL");
+      converter.setOptionList(this.optionList);
+      converter.start();
+      
+      LoggingEvent event = makeLoggingEvent(null);
+      
+      StringBuffer buf = new StringBuffer();
+      converter.write(buf, event);
+ 
+      String expected = "<"+(SyslogConstants.LOG_MAIL+SyslogConstants.INFO_SEVERITY)+">";
+      assertTrue(buf.toString().startsWith(expected));      
+    }
+  }
 
 }



More information about the logback-dev mailing list