diff --git a/logging-modules/pom.xml b/logging-modules/pom.xml index c7a770891d..5a1bd32288 100644 --- a/logging-modules/pom.xml +++ b/logging-modules/pom.xml @@ -19,6 +19,7 @@ log4j2 logback log-mdc + tinylog2 \ No newline at end of file diff --git a/logging-modules/tinylog2/pom.xml b/logging-modules/tinylog2/pom.xml new file mode 100644 index 0000000000..df9dcca7ab --- /dev/null +++ b/logging-modules/tinylog2/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + tinylog2 + 0.1-SNAPSHOT + tinylog2 + + + com.baeldung + logging-modules + 1.0.0-SNAPSHOT + + + + + org.tinylog + tinylog-api + ${tinylog.version} + + + org.tinylog + tinylog-impl + ${tinylog.version} + + + + + 2.5.0 + + + \ No newline at end of file diff --git a/logging-modules/tinylog2/src/main/java/com/baeldung/tinylog/TinylogExamples.java b/logging-modules/tinylog2/src/main/java/com/baeldung/tinylog/TinylogExamples.java new file mode 100644 index 0000000000..3e0e8d79d4 --- /dev/null +++ b/logging-modules/tinylog2/src/main/java/com/baeldung/tinylog/TinylogExamples.java @@ -0,0 +1,43 @@ +package com.baeldung.tinylog; + +import org.tinylog.Logger; + +public class TinylogExamples { + + public static void main(String[] args) { + /* Static text */ + + Logger.info("Hello World!"); + + /* Placeholders */ + + Logger.info("Hello {}!", "Alice"); + Logger.info("π = {0.00}", Math.PI); + + /* Lazy Logging */ + + Logger.debug("Expensive computation: {}", () -> compute()); // Visible in log files but not on the console + + /* Exceptions */ + + int a = 42; + int b = 0; + + try { + int i = a / b; + } catch (Exception ex) { + Logger.error(ex, "Cannot divide {} by {}", a, b); + } + + try { + int i = a / b; + } catch (Exception ex) { + Logger.error(ex); + } + } + + private static int compute() { + return 42; // In real applications, we would perform an expensive computation here + } + +} diff --git a/logging-modules/tinylog2/src/main/resources/tinylog.properties b/logging-modules/tinylog2/src/main/resources/tinylog.properties new file mode 100644 index 0000000000..303aac129a --- /dev/null +++ b/logging-modules/tinylog2/src/main/resources/tinylog.properties @@ -0,0 +1,13 @@ +writer1 = console +writer1.level = info +writer1.format = {date: HH:mm:ss.SSS} [{level}] {class}: {message} + +writer2 = rolling file +writer2.file = logs/myapp_{date: yyyy-MM-dd}_{count}.log +writer2.buffered = true +writer2.policies = startup, daily: 06:00 +writer2.format = {class} [{thread}] {level}: {message} +writer2.convert = gzip +writer2.backups = 100 + +writingthread = true diff --git a/logging-modules/tinylog2/src/test/java/com/baeldung/tinylog/TinylogIntegrationTest.java b/logging-modules/tinylog2/src/test/java/com/baeldung/tinylog/TinylogIntegrationTest.java new file mode 100644 index 0000000000..74637366e8 --- /dev/null +++ b/logging-modules/tinylog2/src/test/java/com/baeldung/tinylog/TinylogIntegrationTest.java @@ -0,0 +1,72 @@ +package com.baeldung.tinylog; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.tinylog.Logger; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TinylogIntegrationTest { + + private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream(); + + @BeforeEach + public void init() throws UnsupportedEncodingException { + System.setOut(new PrintStream(consoleOutput, false, "UTF-8")); + } + + @Test + public void whenLoggingStaticText_thenOutputIt() throws UnsupportedEncodingException { + Logger.info("Hello World!"); + + String outputLog = consoleOutput.toString("UTF-8"); + assertThat(outputLog).isEqualToIgnoringNewLines("Hello World!"); + } + + @Test + public void whenLoggingParamizedText_thenOutputItResolved() throws UnsupportedEncodingException { + Logger.info("Hello {}!", "Alice"); + + String outputLog = consoleOutput.toString("UTF-8"); + assertThat(outputLog).isEqualToIgnoringNewLines("Hello Alice!"); + } + + @Test + public void whenLoggingNumberWithFormatPattern_thenOutputItFormatted() throws UnsupportedEncodingException { + Logger.info("π = {0.00}", Math.PI); + + String outputLog = consoleOutput.toString("UTF-8"); + assertThat(outputLog).isEqualToIgnoringNewLines("π = 3.14"); + } + + @Test + public void whenLoggingExceptionWithMessage_thenOutputMessageAndException() throws UnsupportedEncodingException { + int a = 42; + int b = 0; + try { + int i = a / b; + } catch (Exception ex) { + Logger.error(ex, "Cannot divide {} by {}", a, b); + } + + String outputLog = consoleOutput.toString("UTF-8"); + assertThat(outputLog).startsWith("Cannot divide 42 by 0: java.lang.ArithmeticException"); + } + + @Test + public void whenLoggingExceptionWithoutMessage_thenOutputExceptionOnly() throws UnsupportedEncodingException { + try { + int i = 42 / 0; + } catch (Exception ex) { + Logger.error(ex); + } + + String outputLog = consoleOutput.toString("UTF-8"); + assertThat(outputLog).startsWith("java.lang.ArithmeticException"); + } + +} diff --git a/logging-modules/tinylog2/src/test/resources/tinylog.properties b/logging-modules/tinylog2/src/test/resources/tinylog.properties new file mode 100644 index 0000000000..4534555b06 --- /dev/null +++ b/logging-modules/tinylog2/src/test/resources/tinylog.properties @@ -0,0 +1,4 @@ +writer = console +writer.level = info +writer.stream = out +writer.format = {message}