Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions src/main/java/org/apache/log4j/net/SyslogAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.helpers.SyslogQuietWriter;
import org.apache.log4j.helpers.SyslogWriter;
import org.apache.log4j.spi.LoggingEvent;
Expand Down Expand Up @@ -98,6 +99,7 @@ public class SyslogAppender extends AppenderSkeleton {
protected static final int FACILITY_OI = 1;

static final String TAB = " ";
static final String BLANK = "";

static final Pattern NOT_ALPHANUM = Pattern.compile("[^\\p{Alnum}]");

Expand All @@ -116,7 +118,17 @@ public class SyslogAppender extends AppenderSkeleton {
* @since 1.2.15
*/
private boolean header = false;


/**
* The ConversionPattern appended to all Throwable Messages
*/
private String throwableConversionPattern = null;

/**
* The PatternLayout used to appended the ConversationPattern to all Throwable Messages
*/
private PatternLayout throwablePatternLayout = null;

/**
* The TAG part of the syslog message.
*
Expand Down Expand Up @@ -356,13 +368,17 @@ void append(LoggingEvent event) {
}

if (layout == null || layout.ignoresThrowable()) {
String thrHdr = BLANK;
if (throwablePatternLayout != null) {
thrHdr = throwablePatternLayout.format(event) + " ";
}
String[] s = event.getThrowableStrRep();
if (s != null) {
for(int i = 0; i < s.length; i++) {
if (s[i].startsWith("\t")) {
sqw.write(hdr+TAB+s[i].substring(1));
sqw.write(hdr+thrHdr+TAB+s[i].substring(1));
} else {
sqw.write(hdr+s[i]);
sqw.write(hdr+thrHdr+s[i]);
}
}
}
Expand Down Expand Up @@ -493,6 +509,34 @@ public final void setHeader(final boolean val) {
header = val;
}

/**
* Returns the PatternLayout for Throwable Messages.
*/
public final String getThrowableConversionPattern() {
return throwableConversionPattern;
}

/**
* Sets the conversionPattern for a Layout of type PatternLayout.
* The string will be appended to a log message when encountering a Throwable Message (java exception with stacktrace).
*
* If header is true, message format will be header + ThrowablePatternLayout + string
*/
public final void setThrowableConversionPattern(final String pattern) {
if (pattern != null) {
this.throwableConversionPattern = pattern;
this.throwablePatternLayout = new PatternLayout(pattern);
}
}

/**
* Returns the PatternLayout for Throwable Messages.
*/
public final PatternLayout getThrowablePatternLayout() {
return throwablePatternLayout;
}


/**
* Sets the <b>Tag</b> option.
*
Expand Down
30 changes: 24 additions & 6 deletions tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ public void testNonAlnumTag() {
}

private static String[] log(final boolean header,
final String throwablePattern,
final String tag,
final String msg,
final Exception ex,
Expand All @@ -449,6 +450,7 @@ private static String[] log(final boolean header,
appender.setSyslogHost("localhost:" + ds.getLocalPort());
appender.setName("name");
appender.setHeader(header);
appender.setThrowableConversionPattern(throwablePattern);
appender.setTag(tag);
PatternLayout pl = new PatternLayout("%m");
appender.setLayout(pl);
Expand All @@ -474,7 +476,7 @@ private static String[] log(final boolean header,
}

public void testActualLogging() throws Exception {
String s = log(false, null, "greetings", null, 1)[0];
String s = log(false, null, null, "greetings", null, 1)[0];
StringTokenizer st = new StringTokenizer(s, "<>() ");
assertEquals("14", st.nextToken());
assertEquals("greetings", st.nextToken());
Expand Down Expand Up @@ -509,7 +511,7 @@ public void printStackTrace(final java.io.PrintWriter w) {
* @throws Exception on IOException.
*/
public void testBadTabbing() throws Exception {
String[] s = log(false, null, "greetings", new MishandledException(), 6);
String[] s = log(false, null, null, "greetings", new MishandledException(), 6);
StringTokenizer st = new StringTokenizer(s[0], "<>() ");
assertEquals("11", st.nextToken());
assertEquals("greetings", st.nextToken());
Expand All @@ -520,14 +522,30 @@ public void testBadTabbing() throws Exception {
assertEquals("<11>" + SyslogAppender.TAB, s[5]);
}

/**
* Tests inclusion of throwableConversionPattern on every line of the exception
*/
public void testThrowableConversionPatternExceptionLogging() throws Exception {
String[] s = log(true, "tCP", null, "greetings", new Exception(), 6);
// Skip the first logged event because it only contains the message
for(int i=1; i < s.length; i++) {
System.err.println(s[i]);
assertEquals("<11>", s[i].substring(0, 4));
StringTokenizer st = new StringTokenizer(s[i].substring(21), " ");
// Throw away the hostname
st.nextToken();
assertEquals("tCP", st.nextToken());
}
}

/**
* Tests presence of timestamp if header = true.
*
* @throws Exception if IOException.
*/
public void testHeaderLogging() throws Exception {
Date preDate = new Date();
String s = log(true, null, "greetings", null, 1)[0];
String s = log(true, null, null, "greetings", null, 1)[0];
Date postDate = new Date();
assertEquals("<14>", s.substring(0, 4));

Expand Down Expand Up @@ -564,7 +582,7 @@ public void testHeaderLogging() throws Exception {
* Tests presence of tag if set
*/
public void testHeaderTagLogging() throws Exception {
String s = log(true, "testtag", "greetings", null, 1)[0];
String s = log(true, null, "testtag", "greetings", null, 1)[0];
assertEquals("<14>", s.substring(0, 4));

StringTokenizer st = new StringTokenizer(s.substring(21), " ");
Expand All @@ -579,7 +597,7 @@ public void testHeaderTagLogging() throws Exception {
* Tests presence of tag on every line of the exception
*/
public void testHeaderTagExceptionLogging() throws Exception {
String[] s = log(true, "testtag", "greetings", new Exception(), 6);
String[] s = log(true, null, "testtag", "greetings", new Exception(), 6);
for(int i=0; i < s.length; i++) {
System.err.println(s[i]);
assertEquals("<11>", s[i].substring(0, 4));
Expand All @@ -594,7 +612,7 @@ public void testHeaderTagExceptionLogging() throws Exception {
* Tests absesence of tag if set to null
*/
public void testHeaderNullTagLogging() throws Exception {
String s = log(true, null, "greetings", null, 1)[0];
String s = log(true, null, null, "greetings", null, 1)[0];
assertEquals("<14>", s.substring(0, 4));

StringTokenizer st = new StringTokenizer(s.substring(21), " ");
Expand Down