diff --git a/tinylog-impl/src/main/java/org/tinylog/writers/SyslogWriter.java b/tinylog-impl/src/main/java/org/tinylog/writers/SyslogWriter.java index 4b234f518..825be5ef7 100755 --- a/tinylog-impl/src/main/java/org/tinylog/writers/SyslogWriter.java +++ b/tinylog-impl/src/main/java/org/tinylog/writers/SyslogWriter.java @@ -27,12 +27,11 @@ */ public final class SyslogWriter extends AbstractFormatPatternWriter { - private AbstractSocketWriter socketWriter; + private final AbstractSocketWriter socketWriter; /** * @param properties * Configuration for writer - * * @throws IOException * Socket cannot be opened for write access * @throws IllegalArgumentException diff --git a/tinylog-impl/src/main/java/org/tinylog/writers/raw/AbstractSocketWriter.java b/tinylog-impl/src/main/java/org/tinylog/writers/raw/AbstractSocketWriter.java index 9bbab7e46..8cd51449e 100644 --- a/tinylog-impl/src/main/java/org/tinylog/writers/raw/AbstractSocketWriter.java +++ b/tinylog-impl/src/main/java/org/tinylog/writers/raw/AbstractSocketWriter.java @@ -31,7 +31,7 @@ public abstract class AbstractSocketWriter extends AbstractFormatPatternWriter { private static final int DEFAULT_PORT_NUMBER = 514; private static final String DEFAULT_FACILITY = "USER"; private static final int FACILITY_CODE_SHIFT = 3; - private static final String DEFAULT_SEVERITY = "INFO"; + private static final String DEFAULT_SEVERITY = "INFORMATIONAL"; private final InetAddress inetAddress; private final int port; diff --git a/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogFacility.java b/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogFacility.java index 374965913..0e3872850 100644 --- a/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogFacility.java +++ b/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogFacility.java @@ -86,12 +86,12 @@ public enum SyslogFacility { /** * Log audit. */ - LOG_AUDIT(13), + SECURITY(13), /** * Log alert. */ - LOG_ALERT(14), + CONSOLE(14), /** * Clock daemon. diff --git a/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogSeverity.java b/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogSeverity.java index 4512f1eb2..1d087d58f 100644 --- a/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogSeverity.java +++ b/tinylog-impl/src/main/java/org/tinylog/writers/raw/SyslogSeverity.java @@ -16,14 +16,14 @@ import org.tinylog.Level; /** - * Syslog severity codes. + * Syslog severity levels. */ public enum SyslogSeverity { /** * System is unusable. */ - EMERG(0), + EMERGENCY(0), /** * Action must be taken immediately. */ @@ -47,7 +47,7 @@ public enum SyslogSeverity { /** * Informational messages. */ - INFO(6), + INFORMATIONAL(6), /** * Debug level messages. */ @@ -70,24 +70,23 @@ public int getCode() { /** * Returns the Severity for the specified Level. * @param level The Level. - * @return The matching Severity, or INFO if there is no match. + * @return The matching Severity, or INFORMATIONAL if there is no match. */ public static SyslogSeverity getSeverity(final Level level) { switch (level) { case TRACE: - return DEBUG; case DEBUG: return DEBUG; case INFO: - return INFO; + return INFORMATIONAL; case WARN: return WARNING; case ERROR: return ERROR; case OFF: - return EMERG; + return EMERGENCY; default: - return INFO; + return INFORMATIONAL; } } } diff --git a/tinylog-impl/src/test/java/org/tinylog/writers/SyslogWriterTest.java b/tinylog-impl/src/test/java/org/tinylog/writers/SyslogWriterTest.java index 36ab12390..c2c3bb717 100755 --- a/tinylog-impl/src/test/java/org/tinylog/writers/SyslogWriterTest.java +++ b/tinylog-impl/src/test/java/org/tinylog/writers/SyslogWriterTest.java @@ -37,14 +37,6 @@ public final class SyslogWriterTest { private static final Integer TEST_PORT_NUMBER = 9999; private static final String TEST_MESSAGE = "Test Message"; - - private String getExpectedSyslogMessage(final String message, - final SyslogFacility facility, - final SyslogSeverity severity, - final String identification) { - int code = (facility.getCode() << 3) + severity.getCode(); - return "<" + code + ">" + identification + ": " + message; - } /** * Sends udp message with default settings and verifies it is received. @@ -56,12 +48,17 @@ public void sendDefaultUdpSyslogMessage() throws Exception { UdpSyslogServer server = new UdpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogWriter writer = new SyslogWriter(tripletonMap("format", "{message}", "protocol", "udp", "port", TEST_PORT_NUMBER.toString())); - writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); - - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, SyslogFacility.USER, SyslogSeverity.INFO, "")); - server.shutdown(); + try { + Map configuration = tripletonMap("format", "{message}", "protocol", "udp", "port", TEST_PORT_NUMBER.toString()); + SyslogWriter writer = new SyslogWriter(configuration); + writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); + + Thread.sleep(250); + String expectedMessage = generateExpectedMessage(TEST_MESSAGE, SyslogFacility.USER, SyslogSeverity.INFORMATIONAL, ""); + assertThat(server.getLastMessage()).isEqualTo(expectedMessage); + } finally { + server.shutdown(); + } } /** @@ -70,29 +67,32 @@ public void sendDefaultUdpSyslogMessage() throws Exception { * @throws Exception Failed. */ @Test - public void sendNondefaultUdpSyslogMessage() throws Exception { + public void sendNonDefaultUdpSyslogMessage() throws Exception { UdpSyslogServer server = new UdpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogFacility facility = SyslogFacility.LOCAL0; - SyslogSeverity severity = SyslogSeverity.ERROR; - Map properties = new HashMap<>(); - properties.put("format", "{message}"); - properties.put("protocol", "udp"); - properties.put("port", TEST_PORT_NUMBER.toString()); - properties.put("facility", facility.toString()); - properties.put("severity", severity.toString()); - - SyslogWriter writer = new SyslogWriter(properties); - writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); - - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, severity, "")); - server.shutdown(); + try { + SyslogFacility facility = SyslogFacility.LOCAL0; + SyslogSeverity severity = SyslogSeverity.ERROR; + Map properties = new HashMap<>(); + properties.put("format", "{message}"); + properties.put("protocol", "udp"); + properties.put("port", TEST_PORT_NUMBER.toString()); + properties.put("facility", facility.toString()); + properties.put("severity", severity.toString()); + + SyslogWriter writer = new SyslogWriter(properties); + writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); + + Thread.sleep(250); + assertThat(server.getLastMessage()).isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, severity, "")); + } finally { + server.shutdown(); + } } /** - * Verifies that default UDP prrotocol will be used if no protocol is specified. + * Verifies that default UDP protocol will be used if no protocol is specified. * * @throws Exception Failed. */ @@ -101,12 +101,16 @@ public void missingProtocol() throws Exception { UdpSyslogServer server = new UdpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogWriter writer = new SyslogWriter(doubletonMap("format", "{message}", "port", TEST_PORT_NUMBER.toString())); - writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); + try { + SyslogWriter writer = new SyslogWriter(doubletonMap("format", "{message}", "port", TEST_PORT_NUMBER.toString())); + writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, SyslogFacility.USER, SyslogSeverity.INFO, "")); - server.shutdown(); + Thread.sleep(250); + String expectedMessage = generateExpectedMessage(TEST_MESSAGE, SyslogFacility.USER, SyslogSeverity.INFORMATIONAL, ""); + assertThat(server.getLastMessage()).isEqualTo(expectedMessage); + } finally { + server.shutdown(); + } } /** @@ -151,23 +155,26 @@ public void sendUdpSyslogMessageWithIdentification() throws Exception { UdpSyslogServer server = new UdpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogFacility facility = SyslogFacility.KERN; - SyslogSeverity severity = SyslogSeverity.DEBUG; - String identification = "SyslogWriterTest"; - Map properties = new HashMap<>(); - properties.put("format", "{message}"); - properties.put("protocol", "udp"); - properties.put("port", TEST_PORT_NUMBER.toString()); - properties.put("facility", facility.toString()); - properties.put("severity", severity.toString()); - properties.put("identification", identification); - - SyslogWriter writer = new SyslogWriter(properties); - writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); - - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, severity, identification)); - server.shutdown(); + try { + SyslogFacility facility = SyslogFacility.KERN; + SyslogSeverity severity = SyslogSeverity.DEBUG; + String identification = "SyslogWriterTest"; + Map properties = new HashMap<>(); + properties.put("format", "{message}"); + properties.put("protocol", "udp"); + properties.put("port", TEST_PORT_NUMBER.toString()); + properties.put("facility", facility.toString()); + properties.put("severity", severity.toString()); + properties.put("identification", identification); + + SyslogWriter writer = new SyslogWriter(properties); + writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); + + Thread.sleep(250); + assertThat(server.getLastMessage()).isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, severity, identification)); + } finally { + server.shutdown(); + } } /** @@ -180,51 +187,60 @@ public void sendLevelControlledUdpSyslogMessage() throws Exception { UdpSyslogServer server = new UdpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogFacility facility = SyslogFacility.AUTH; - Map properties = new HashMap<>(); - properties.put("format", "{message}"); - properties.put("protocol", "udp"); - properties.put("port", TEST_PORT_NUMBER.toString()); - properties.put("facility", facility.toString()); + try { + SyslogFacility facility = SyslogFacility.AUTH; + Map properties = new HashMap<>(); + properties.put("format", "{message}"); + properties.put("protocol", "udp"); + properties.put("port", TEST_PORT_NUMBER.toString()); + properties.put("facility", facility.toString()); - SyslogWriter writer = new SyslogWriter(properties); - LogEntryBuilder log = LogEntryBuilder.empty().message(TEST_MESSAGE); - log.level(Level.DEBUG); - writer.write(log.create()); + SyslogWriter writer = new SyslogWriter(properties); + LogEntryBuilder log = LogEntryBuilder.empty().message(TEST_MESSAGE); + log.level(Level.DEBUG); + writer.write(log.create()); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); - log.level(Level.ERROR); - writer.write(log.create()); + log.level(Level.ERROR); + writer.write(log.create()); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.ERROR, "")); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.ERROR, "")); - log.level(Level.INFO); - writer.write(log.create()); + log.level(Level.INFO); + writer.write(log.create()); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.INFO, "")); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.INFORMATIONAL, "")); - log.level(Level.OFF); - writer.write(log.create()); + log.level(Level.OFF); + writer.write(log.create()); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.EMERG, "")); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.EMERGENCY, "")); - log.level(Level.TRACE); - writer.write(log.create()); + log.level(Level.TRACE); + writer.write(log.create()); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); - log.level(Level.WARN); - writer.write(log.create()); + log.level(Level.WARN); + writer.write(log.create()); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.WARNING, "")); - server.shutdown(); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.WARNING, "")); + } finally { + server.shutdown(); + } } /** @@ -237,12 +253,17 @@ public void sendDefaultTcpSyslogMessage() throws Exception { TcpSyslogServer server = new TcpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogWriter writer = new SyslogWriter(tripletonMap("format", "{message}", "protocol", "tcp", "port", TEST_PORT_NUMBER.toString())); - writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); - - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, SyslogFacility.USER, SyslogSeverity.INFO, "")); - server.shutdown(); + try { + Map configuration = tripletonMap("format", "{message}", "protocol", "tcp", "port", TEST_PORT_NUMBER.toString()); + SyslogWriter writer = new SyslogWriter(configuration); + writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); + + Thread.sleep(250); + String expectedMessage = generateExpectedMessage(TEST_MESSAGE, SyslogFacility.USER, SyslogSeverity.INFORMATIONAL, ""); + assertThat(server.getLastMessage()).isEqualTo(expectedMessage); + } finally { + server.shutdown(); + } } /** @@ -251,25 +272,28 @@ public void sendDefaultTcpSyslogMessage() throws Exception { * @throws Exception Failed. */ @Test - public void sendNondefaultTcpSyslogMessage() throws Exception { + public void sendNonDefaultTcpSyslogMessage() throws Exception { TcpSyslogServer server = new TcpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogFacility facility = SyslogFacility.LOCAL0; - SyslogSeverity severity = SyslogSeverity.ERROR; - Map properties = new HashMap<>(); - properties.put("format", "{message}"); - properties.put("protocol", "tcp"); - properties.put("port", TEST_PORT_NUMBER.toString()); - properties.put("facility", facility.toString()); - properties.put("severity", severity.toString()); - - SyslogWriter writer = new SyslogWriter(properties); - writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); - - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, severity, "")); - server.shutdown(); + try { + SyslogFacility facility = SyslogFacility.LOCAL0; + SyslogSeverity severity = SyslogSeverity.ERROR; + Map properties = new HashMap<>(); + properties.put("format", "{message}"); + properties.put("protocol", "tcp"); + properties.put("port", TEST_PORT_NUMBER.toString()); + properties.put("facility", facility.toString()); + properties.put("severity", severity.toString()); + + SyslogWriter writer = new SyslogWriter(properties); + writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); + + Thread.sleep(250); + assertThat(server.getLastMessage()).isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, severity, "")); + } finally { + server.shutdown(); + } } /** @@ -282,23 +306,26 @@ public void sendTcpSyslogMessageWithIdentification() throws Exception { TcpSyslogServer server = new TcpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogFacility facility = SyslogFacility.KERN; - SyslogSeverity severity = SyslogSeverity.DEBUG; - String identification = "SyslogWriterTest"; - Map properties = new HashMap<>(); - properties.put("format", "{message}"); - properties.put("protocol", "tcp"); - properties.put("port", TEST_PORT_NUMBER.toString()); - properties.put("facility", facility.toString()); - properties.put("severity", severity.toString()); - properties.put("identification", identification); - - SyslogWriter writer = new SyslogWriter(properties); - writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); - - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, severity, identification)); - server.shutdown(); + try { + SyslogFacility facility = SyslogFacility.KERN; + SyslogSeverity severity = SyslogSeverity.DEBUG; + String identification = "SyslogWriterTest"; + Map properties = new HashMap<>(); + properties.put("format", "{message}"); + properties.put("protocol", "tcp"); + properties.put("port", TEST_PORT_NUMBER.toString()); + properties.put("facility", facility.toString()); + properties.put("severity", severity.toString()); + properties.put("identification", identification); + + SyslogWriter writer = new SyslogWriter(properties); + writer.write(LogEntryBuilder.empty().message(TEST_MESSAGE).create()); + + Thread.sleep(250); + assertThat(server.getLastMessage()).isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, severity, identification)); + } finally { + server.shutdown(); + } } /** @@ -311,51 +338,70 @@ public void sendLevelControlledTcpSyslogMessage() throws Exception { TcpSyslogServer server = new TcpSyslogServer(TEST_PORT_NUMBER); server.start(); - SyslogFacility facility = SyslogFacility.AUTH; - Map properties = new HashMap<>(); - properties.put("format", "{message}"); - properties.put("protocol", "tcp"); - properties.put("port", TEST_PORT_NUMBER.toString()); - properties.put("facility", facility.toString()); + try { + SyslogFacility facility = SyslogFacility.AUTH; + Map properties = new HashMap<>(); + properties.put("format", "{message}"); + properties.put("protocol", "tcp"); + properties.put("port", TEST_PORT_NUMBER.toString()); + properties.put("facility", facility.toString()); + + SyslogWriter writer = new SyslogWriter(properties); + LogEntryBuilder log = LogEntryBuilder.empty().message(TEST_MESSAGE); + log.level(Level.DEBUG); + writer.write(log.create()); - SyslogWriter writer = new SyslogWriter(properties); - LogEntryBuilder log = LogEntryBuilder.empty().message(TEST_MESSAGE); - log.level(Level.DEBUG); - writer.write(log.create()); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); + log.level(Level.ERROR); + writer.write(log.create()); - log.level(Level.ERROR); - writer.write(log.create()); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.ERROR, "")); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.ERROR, "")); + log.level(Level.INFO); + writer.write(log.create()); - log.level(Level.INFO); - writer.write(log.create()); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.INFORMATIONAL, "")); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.INFO, "")); + log.level(Level.OFF); + writer.write(log.create()); - log.level(Level.OFF); - writer.write(log.create()); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.EMERGENCY, "")); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.EMERG, "")); + log.level(Level.TRACE); + writer.write(log.create()); - log.level(Level.TRACE); - writer.write(log.create()); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.DEBUG, "")); + log.level(Level.WARN); + writer.write(log.create()); - log.level(Level.WARN); - writer.write(log.create()); + Thread.sleep(250); + assertThat(server.getLastMessage()) + .isEqualTo(generateExpectedMessage(TEST_MESSAGE, facility, SyslogSeverity.WARNING, "")); + } finally { + server.shutdown(); + } + } - Thread.sleep(250); - assertThat(server.getLastMessage()).isEqualTo(getExpectedSyslogMessage(TEST_MESSAGE, facility, SyslogSeverity.WARNING, "")); - server.shutdown(); + private String generateExpectedMessage( + final String message, + final SyslogFacility facility, + final SyslogSeverity severity, + final String identification + ) { + int code = (facility.getCode() << 3) + severity.getCode(); + return "<" + code + ">" + identification + ": " + message; } }