BAEL-6033: Add tinylog 2 examples (#13232)

* BAEL-6033: Add tinylog 2 examples

* BAEL-6033: Rename test methods of tinylog 2 in when-then style
This commit is contained in:
Martin Winandy 2023-01-11 02:53:43 +01:00 committed by GitHub
parent a23ccae5ac
commit 5825cf4775
6 changed files with 166 additions and 0 deletions

View File

@ -19,6 +19,7 @@
<module>log4j2</module>
<module>logback</module>
<module>log-mdc</module>
<module>tinylog2</module>
</modules>
</project>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>tinylog2</artifactId>
<version>0.1-SNAPSHOT</version>
<name>tinylog2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>logging-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-api</artifactId>
<version>${tinylog.version}</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>${tinylog.version}</version>
</dependency>
</dependencies>
<properties>
<tinylog.version>2.5.0</tinylog.version>
</properties>
</project>

View File

@ -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
}
}

View File

@ -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

View File

@ -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");
}
}

View File

@ -0,0 +1,4 @@
writer = console
writer.level = info
writer.stream = out
writer.format = {message}