Skip to content

Commit

Permalink
Merge pull request #173 from ericdahl/logback-936
Browse files Browse the repository at this point in the history
LOGBACK-936 Update SyslogAppender to format days according to the spec
  • Loading branch information
tony19 committed Feb 11, 2014
2 parents 2c7982b + 4a29d91 commit 58c12dc
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 16 deletions.
Expand Up @@ -15,8 +15,8 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

Expand All @@ -28,7 +28,10 @@ public class SyslogStartConverter extends ClassicConverter {

long lastTimestamp = -1;
String timesmapStr = null;
SimpleDateFormat simpleFormat;
SimpleDateFormat simpleMonthFormat;
SimpleDateFormat simpleTimeFormat;
private final Calendar calendar = Calendar.getInstance(Locale.US);

String localHostName;
int facility;

Expand All @@ -46,7 +49,8 @@ public void start() {
localHostName = getLocalHostname();
try {
// hours should be in 0-23, see also http://jira.qos.ch/browse/LBCLASSIC-48
simpleFormat = new SimpleDateFormat("MMM dd HH:mm:ss", new DateFormatSymbols(Locale.US));
simpleMonthFormat = new SimpleDateFormat("MMM", Locale.US);
simpleTimeFormat = new SimpleDateFormat("HH:mm:ss", Locale.US);
} catch (IllegalArgumentException e) {
addError("Could not instantiate SimpleDateFormat", e);
errorCount++;
Expand Down Expand Up @@ -91,9 +95,14 @@ public String getLocalHostname() {

String computeTimeStampString(long now) {
synchronized (this) {
if (now != lastTimestamp) {
lastTimestamp = now;
timesmapStr = simpleFormat.format(new Date(now));
// Since the formatted output is only precise to the second, we can use the same cached string if the current
// second is the same (stripping off the milliseconds).
if ((now / 1000) != lastTimestamp) {
lastTimestamp = now / 1000;
Date nowDate = new Date(now);
calendar.setTime(nowDate);
timesmapStr = String.format("%s %2d %s", simpleMonthFormat.format(nowDate),
calendar.get(Calendar.DAY_OF_MONTH), simpleTimeFormat.format(nowDate));
}
return timesmapStr;
}
Expand Down
Expand Up @@ -35,6 +35,8 @@

public class SyslogAppenderTest {

private static final String SYSLOG_PREFIX_REGEX = "<\\d{2}>\\w{3} [\\d ]\\d \\d{2}(:\\d{2}){2} [\\w.-]* ";

LoggerContext lc = new LoggerContext();
SyslogAppender sa = new SyslogAppender();
MockSyslogServer mockServer;
Expand Down Expand Up @@ -101,8 +103,7 @@ public void basic() throws InterruptedException {
+ (SyslogConstants.LOG_MAIL + SyslogConstants.DEBUG_SEVERITY) + ">";
assertTrue(msg.startsWith(expected));

String first = "<\\d{2}>\\w{3} \\d{2} \\d{2}(:\\d{2}){2} [\\w.-]* ";
checkRegexMatch(msg, first + "\\[" + threadName + "\\] " + loggerName + " "
checkRegexMatch(msg, SYSLOG_PREFIX_REGEX + "\\[" + threadName + "\\] " + loggerName + " "
+ logMsg);

}
Expand All @@ -127,8 +128,7 @@ public void suffixPatternWithTag() throws InterruptedException {
+ (SyslogConstants.LOG_MAIL + SyslogConstants.DEBUG_SEVERITY) + ">";
assertTrue(msg.startsWith(expected));

String first = "<\\d{2}>\\w{3} \\d{2} \\d{2}(:\\d{2}){2} [\\w.-]* ";
checkRegexMatch(msg, first + "test/something \\[" + threadName + "\\] " + loggerName + " "
checkRegexMatch(msg, SYSLOG_PREFIX_REGEX + "test/something \\[" + threadName + "\\] " + loggerName + " "
+ logMsg);

}
Expand Down Expand Up @@ -160,9 +160,8 @@ public void tException() throws InterruptedException {
+ (SyslogConstants.LOG_MAIL + SyslogConstants.DEBUG_SEVERITY) + ">";
assertTrue(msg.startsWith(expected));

String expectedPrefix = "<\\d{2}>\\w{3} \\d{2} \\d{2}(:\\d{2}){2} [\\w.-]* ";
String threadName = Thread.currentThread().getName();
String regex = expectedPrefix + "\\[" + threadName + "\\] " + loggerName
String regex = SYSLOG_PREFIX_REGEX + "\\[" + threadName + "\\] " + loggerName
+ " " + logMsg;
checkRegexMatch(msg, regex);

Expand All @@ -172,7 +171,7 @@ public void tException() throws InterruptedException {

msg = mockServer.getMessageList().get(2);
assertTrue(msg.startsWith(expected));
regex = expectedPrefix + "\\[" + threadName + "\\] " + "foo "+CoreConstants.TAB + "at ch\\.qos.*";
regex = SYSLOG_PREFIX_REGEX + "\\[" + threadName + "\\] " + "foo "+CoreConstants.TAB + "at ch\\.qos.*";
checkRegexMatch(msg, regex);
}

Expand Down Expand Up @@ -203,20 +202,19 @@ public void large() throws Exception {

String expected = "<"
+ (SyslogConstants.LOG_MAIL + SyslogConstants.DEBUG_SEVERITY) + ">";
String expectedPrefix = "<\\d{2}>\\w{3} \\d{2} \\d{2}(:\\d{2}){2} [\\w.-]* ";
String threadName = Thread.currentThread().getName();

// large message is truncated
final int maxMessageSize = sa.getMaxMessageSize();
String largeMsg = mockServer.getMessageList().get(0);
assertTrue(largeMsg.startsWith(expected));
String largeRegex = expectedPrefix + "\\[" + threadName + "\\] " + loggerName
String largeRegex = SYSLOG_PREFIX_REGEX + "\\[" + threadName + "\\] " + loggerName
+ " " + "a{" + (maxMessageSize - 2000) + "," + maxMessageSize + "}";
checkRegexMatch(largeMsg, largeRegex);

String msg = mockServer.getMessageList().get(1);
assertTrue(msg.startsWith(expected));
String regex = expectedPrefix + "\\[" + threadName + "\\] " + loggerName
String regex = SYSLOG_PREFIX_REGEX + "\\[" + threadName + "\\] " + loggerName
+ " " + logMsg;
checkRegexMatch(msg, regex);
}
Expand Down
@@ -0,0 +1,135 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2014, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.pattern;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Locale;

import static org.junit.Assert.assertEquals;

public class SyslogStartConverterTest {

private LoggerContext lc;
private SyslogStartConverter converter;
private final String HOSTNAME = findHostname();
private final Calendar calendar = Calendar.getInstance(Locale.US);

@Before
public void setUp() throws Exception {
lc = new LoggerContext();
converter = new SyslogStartConverter();
converter.setOptionList(Arrays.asList("local7"));
converter.start();
}

@After
public void tearDown() throws Exception {
lc = null;
converter.stop();
converter = null;
}

@Test
public void datesLessThanTen() {
// RFC 3164, section 4.1.2:
// If the day of the month is less than 10, then it MUST be represented as
// a space and then the number. For example, the 7th day of August would be
// represented as "Aug 7", with two spaces between the "g" and the "7".
LoggingEvent le = createLoggingEvent();
calendar.set(2012, Calendar.AUGUST, 7, 13, 15, 0);
le.setTimeStamp(calendar.getTimeInMillis());
assertEquals("<191>Aug 7 13:15:00 " + HOSTNAME + " ", converter.convert(le));
}

@Test
public void datesGreaterThanTen() {
LoggingEvent le = createLoggingEvent();
calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
le.setTimeStamp(calendar.getTimeInMillis());
assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le));
}

@Test
public void multipleConversions() {
LoggingEvent le = createLoggingEvent();
calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
le.setTimeStamp(calendar.getTimeInMillis());
assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le));
assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le));

calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 16);
le.setTimeStamp(calendar.getTimeInMillis());
assertEquals("<191>Oct 11 22:14:16 " + HOSTNAME + " ", converter.convert(le));
}

@Test
public void ignoreDefaultLocale() {
Locale originalDefaultLocale = Locale.getDefault();
Locale.setDefault(Locale.TRADITIONAL_CHINESE);

try {
converter.start();

LoggingEvent le = createLoggingEvent();
calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
le.setTimeStamp(calendar.getTimeInMillis());
String result = converter.convert(le);
assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", result);
} finally {
Locale.setDefault(originalDefaultLocale);
}
}

@Test
@Ignore
public void hostnameShouldNotIncludeDomain() throws Exception {
// RFC 3164, section 4.1.2:
// The Domain Name MUST NOT be included in the HOSTNAME field.
String host = HOSTNAME;
final int firstPeriod = host.indexOf(".");
if (firstPeriod != -1) {
host = host.substring(0, firstPeriod);
}
LoggingEvent le = createLoggingEvent();
calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
le.setTimeStamp(calendar.getTimeInMillis());
assertEquals("<191>Oct 11 22:14:15 " + host + " ", converter.convert(le));
}

private LoggingEvent createLoggingEvent() {
return new LoggingEvent(this.getClass().getName(), lc
.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", null,
null);
}

private static String findHostname() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
return "UNKNOWN_LOCALHOST";
}
}
}

0 comments on commit 58c12dc

Please sign in to comment.