Skip to content

Commit 573b8e8

Browse files
author
Cyrille Le Clerc
authored
Merge pull request #26 from ENOVACOM/rfc-5425-message-format
RFC 5425 message format
2 parents 3723853 + e92c60c commit 573b8e8

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/main/java/com/cloudbees/syslog/MessageFormat.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ public enum MessageFormat {
2828
/**
2929
* <a href="https://tools.ietf.org/html/rfc5424">RFC 5424 - The Syslog Protocol</a>
3030
*/
31-
RFC_5424
31+
RFC_5424,
32+
/**
33+
* <a href="https://tools.ietf.org/html/rfc5425">RFC 5425 - Transport Layer Security (TLS) Transport Mapping for Syslog</a>
34+
*/
35+
RFC_5425
3236
}

src/main/java/com/cloudbees/syslog/SyslogMessage.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
import javax.annotation.Nonnull;
1919
import javax.annotation.Nullable;
20+
2021
import java.io.CharArrayWriter;
2122
import java.io.IOException;
2223
import java.io.StringWriter;
2324
import java.io.Writer;
2425
import java.net.InetAddress;
2526
import java.net.UnknownHostException;
27+
import java.nio.charset.StandardCharsets;
2628
import java.util.Date;
2729
import java.util.HashSet;
2830
import java.util.Locale;
@@ -249,6 +251,8 @@ public String toSyslogMessage(MessageFormat messageFormat) {
249251
return toRfc3164SyslogMessage();
250252
case RFC_5424:
251253
return toRfc5424SyslogMessage();
254+
case RFC_5425:
255+
return toRfc5425SyslogMessage();
252256
default:
253257
throw new IllegalStateException("Unsupported message format '" + messageFormat + "'");
254258
}
@@ -269,11 +273,40 @@ public void toSyslogMessage(@Nonnull MessageFormat messageFormat, @Nonnull Write
269273
case RFC_5424:
270274
toRfc5424SyslogMessage(out);
271275
break;
276+
case RFC_5425:
277+
toRfc5425SyslogMessage(out);
278+
break;
272279
default:
273280
throw new IllegalStateException("Unsupported message format '" + messageFormat + "'");
274281
}
275282
}
276283

284+
/**
285+
* Generates an <a href="http://tools.ietf.org/html/rfc5424">RFC-5425</a> message.
286+
*/
287+
public String toRfc5425SyslogMessage() {
288+
289+
StringWriter sw = new StringWriter(msg == null ? 32 : msg.size() + 32);
290+
try {
291+
toRfc5425SyslogMessage(sw);
292+
} catch (IOException e) {
293+
throw new IllegalStateException(e);
294+
}
295+
return sw.toString();
296+
}
297+
298+
/**
299+
* Generates an <a href="http://tools.ietf.org/html/rfc5425">RFC-5425</a> message.
300+
*/
301+
public void toRfc5425SyslogMessage(Writer out) throws IOException {
302+
303+
String rfc5424Message = toRfc5424SyslogMessage();
304+
int length = rfc5424Message.getBytes(StandardCharsets.UTF_8).length;
305+
out.write(String.valueOf(length));
306+
out.write(SP);
307+
out.write(rfc5424Message);
308+
}
309+
277310
/**
278311
* Generates an <a href="http://tools.ietf.org/html/rfc5424">RFC-5424</a> message.
279312
*/

src/test/java/com/cloudbees/syslog/SyslogMessageTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@
2828
*/
2929
public class SyslogMessageTest {
3030

31+
@Test
32+
public void testRfc5425Format() throws Exception {
33+
// GIVEN
34+
Calendar cal = Calendar.getInstance();
35+
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
36+
cal.set(2013, Calendar.DECEMBER, 5, 10, 30, 5);
37+
cal.set(Calendar.MILLISECOND, 0);
38+
39+
System.out.println(SyslogMessage.rfc3339DateFormat.format(cal.getTime()));
40+
System.out.println(cal.getTimeInMillis());
41+
42+
43+
SyslogMessage message = new SyslogMessage()
44+
.withTimestamp(cal.getTimeInMillis())
45+
.withAppName("my_app")
46+
.withHostname("myserver.example.com")
47+
.withFacility(Facility.USER)
48+
.withSeverity(Severity.INFORMATIONAL)
49+
.withTimestamp(cal.getTimeInMillis())
50+
.withMsg("a syslog message");
51+
52+
// WHEN
53+
String actual = message.toRfc5425SyslogMessage();
54+
55+
// THEN
56+
String expected = "81 <14>1 2013-12-05T10:30:05.000Z myserver.example.com my_app - - - a syslog message";
57+
assertThat(actual, is(expected));
58+
}
59+
3160
@Test
3261
public void testRfc5424Format() throws Exception {
3362

0 commit comments

Comments
 (0)