[logback-dev] Missing logging of Throwable in SyslogAppender

Tobias Lund-Melcher tlm at knowit.no
Tue Oct 5 14:43:27 CEST 2010


Hi,

the project I'm working on decided to switch to slf4j and logback for logging, and our experiences so far are very good. Thanks for the great work!!

During implementation and testing we discovered that the SyslogAppender does not seem to log the throwable type and its message at all. To overcome this we've overridden the the SyslogAppender.postProcess() method. I believe this is a bug which should be addressed. The git diff after patching this is given below.

Best Regards,

Tobias Lund-Melcher


-----------------git diff-------------------------



diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java b/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
index 67845db..bc35399 100644
--- a/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
+++ b/logback-classic/src/main/java/ch/qos/logback/classic/net/SyslogAppender.java
@@ -21,6 +21,7 @@ import ch.qos.logback.classic.pattern.SyslogStartConverter;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.classic.spi.IThrowableProxy;
 import ch.qos.logback.classic.spi.StackTraceElementProxy;
+import ch.qos.logback.classic.spi.ThrowableProxyUtil;
 import ch.qos.logback.classic.util.LevelToSyslogSeverity;
 import ch.qos.logback.core.CoreConstants;
 import ch.qos.logback.core.Layout;
@@ -82,10 +83,15 @@ public class SyslogAppender extends SyslogAppenderBase<ILoggingEvent> {
 
     IThrowableProxy tp = event.getThrowableProxy();
     while (tp != null) {
-      StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
       try {
+        StringBuilder sb = new StringBuilder(prefix);
+        ThrowableProxyUtil.printFirstLine(sb, tp);
+        sw.write(sb.toString().getBytes());
+        sw.flush();
+
+        StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
         for (StackTraceElementProxy step : stepArray) {
-          StringBuilder sb = new StringBuilder();
+          sb = new StringBuilder();
           sb.append(prefix).append(CoreConstants.TAB).append(step);
           sw.write(sb.toString().getBytes());
           sw.flush();
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
index 83056a6..269f387 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
@@ -132,6 +133,10 @@ public class SyslogAppenderTest {
     String regex = expectedPrefix + "\\[" + threadName + "\\] " + loggerName
         + " " + logMsg;
     checkRegexMatch(msg, regex);
+    
+    msg = mockServer.getMessageList().get(1);
+    assertTrue(msg.contains(ex.getClass().getName()));
+    assertTrue(msg.contains(ex.getMessage()));
   }
 
   private void checkRegexMatch(String s, String regex) {



More information about the logback-dev mailing list