From 3551d59fdbfa849f0ab78acc0d11b33b63102a68 Mon Sep 17 00:00:00 2001 From: Vinh Nguyen Date: Wed, 22 Apr 2015 14:25:37 -0700 Subject: [PATCH] SyslogAppender - support higher and configurable max message size --- .../org/apache/log4j/net/SyslogAppender.java | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/log4j/net/SyslogAppender.java b/src/main/java/org/apache/log4j/net/SyslogAppender.java index 6ab7eddfd..300b10bd6 100644 --- a/src/main/java/org/apache/log4j/net/SyslogAppender.java +++ b/src/main/java/org/apache/log4j/net/SyslogAppender.java @@ -91,6 +91,14 @@ protected static final int SYSLOG_HOST_OI = 0; protected static final int FACILITY_OI = 1; + /** + * Max lengths in bytes of a message. Per RFC 5424, size limits are dictated + * by the syslog transport mapping in use. But, the practical upper limit of + * UDP over IPV4 is 65507 (65535 − 8 byte UDP header − 20 byte IP header). + */ + protected static final int LOWER_MAX_MSG_LENGTH = 480; + protected static final int UPPER_MAX_MSG_LENGTH = 65507; + static final String TAB = " "; // Have LOG_USER as default @@ -102,6 +110,11 @@ SyslogQuietWriter sqw; String syslogHost; + /** + * Max length in bytes of a message. + */ + int maxMessageLength = UPPER_MAX_MSG_LENGTH; + /** * If true, the appender will generate the HEADER (timestamp and host name) * part of the syslog packet. @@ -278,12 +291,10 @@ int getFacility(String facilityName) { private void splitPacket(final String header, final String packet) { int byteCount = packet.getBytes().length; - // - // if packet is less than RFC 3164 limit - // of 1024 bytes, then write it - // (must allow for up 5to 5 characters in the PRI section - // added by SyslogQuietWriter) - if (byteCount <= 1019) { + + // If packet is less than limit, then write it. + // Else, write in chunks. + if (byteCount <= maxMessageLength) { sqw.write(packet); } else { int split = header.length() + (packet.length() - header.length())/2; @@ -533,4 +544,24 @@ private void sendLayoutMessage(final String msg) { sqw.write(packet); } } + + /** + * @return The max message length in bytes. + */ + public int getMaxMessageLength() { + return maxMessageLength; + } + + /** + * @param byteLen The max message length in bytes. + */ + public void setMaxMessageLength(int len) { + if (len < LOWER_MAX_MSG_LENGTH || len > UPPER_MAX_MSG_LENGTH) { + System.err.println(len + + " is an invalid message length. Defaulting to " + + UPPER_MAX_MSG_LENGTH + "."); + len = UPPER_MAX_MSG_LENGTH; + } + maxMessageLength = len; + } }