Skip to content

Commit

Permalink
Merge remote-tracking branch 'pasieronen/logback-141-syslog-truncation'
Browse files Browse the repository at this point in the history
  • Loading branch information
ceharris committed Apr 18, 2013
2 parents c1e31a8 + 29427d1 commit e0e97b2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void checkRegexMatch(String s, String regex) {

@Test
public void large() throws InterruptedException {
setMockServerAndConfigure(1);
setMockServerAndConfigure(2);
StringBuilder largeBuf = new StringBuilder();
for (int i = 0; i < 2 * 1024 * 1024; i++) {
largeBuf.append('a');
Expand All @@ -197,15 +197,24 @@ public void large() throws InterruptedException {

mockServer.join(8000);
assertTrue(mockServer.isFinished());
// the first message is wasted
assertEquals(1, mockServer.getMessageList().size());
String msg = mockServer.getMessageList().get(0);
String expected = "<"

// both messages received
assertEquals(2, mockServer.getMessageList().size());

String expected = "<"
+ (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();

// large message is truncated
String largeMsg = mockServer.getMessageList().get(0);
assertTrue(largeMsg.startsWith(expected));
String largeRegex = expectedPrefix + "\\[" + threadName + "\\] " + loggerName
+ " " + "a{64000,66000}";
checkRegexMatch(largeMsg, largeRegex);

String msg = mockServer.getMessageList().get(1);
assertTrue(msg.startsWith(expected));
String regex = expectedPrefix + "\\[" + threadName + "\\] " + loggerName
+ " " + logMsg;
checkRegexMatch(msg, regex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void run() {
socket = new DatagramSocket(port);

for (int i = 0; i < loopLen; i++) {
byte[] buf = new byte[2048];
byte[] buf = new byte[65536];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
//System.out.println("Waiting for message");
socket.receive(packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public abstract class SyslogAppenderBase<E> extends AppenderBase<E> {

final static String SYSLOG_LAYOUT_URL = CoreConstants.CODES_URL
+ "#syslog_layout";
final static int MSG_SIZE_LIMIT = 256 * 1024;

Layout<E> layout;
String facilityStr;
String syslogHost;
protected String suffixPattern;
SyslogOutputStream sos;
int port = SyslogConstants.SYSLOG_PORT;
int maxMessageSize = 65000;

public void start() {
int errorCount = 0;
Expand Down Expand Up @@ -84,8 +84,8 @@ protected void append(E eventObject) {
if(msg == null) {
return;
}
if (msg.length() > MSG_SIZE_LIMIT) {
msg = msg.substring(0, MSG_SIZE_LIMIT);
if (msg.length() > maxMessageSize) {
msg = msg.substring(0, maxMessageSize);
}
sos.write(msg.getBytes());
sos.flush();
Expand Down Expand Up @@ -211,6 +211,25 @@ public void setPort(int port) {
this.port = port;
}

/**
*
* @return
*/
public int getMaxMessageSize() {
return maxMessageSize;
}

/**
* Maximum size for the syslog message (in characters); messages
* longer than this are truncated. The default value is 65400 (which
* is near the maximum for syslog-over-UDP). Note that the value is
* characters; the number of bytes may vary if non-ASCII characters
* are present.
*/
public void setMaxMessageSize(int maxMessageSize) {
this.maxMessageSize = maxMessageSize;
}

public Layout<E> getLayout() {
return layout;
}
Expand Down

0 comments on commit e0e97b2

Please sign in to comment.