diff --git a/logging-modules/logging-techniques/pom.xml b/logging-modules/logging-techniques/pom.xml new file mode 100644 index 0000000000..f2ea495af1 --- /dev/null +++ b/logging-modules/logging-techniques/pom.xml @@ -0,0 +1,43 @@ + + + + logging-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + logging-techniques + + + + org.slf4j + slf4j-api + 2.0.9 + + + net.logstash.logback + logstash-logback-encoder + 7.4 + + + ch.qos.logback + logback-classic + 1.4.14 + + + ch.qos.logback + logback-core + 1.4.14 + + + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/logging-modules/logging-techniques/src/main/java/com/baeldung/structuredlogging/User.java b/logging-modules/logging-techniques/src/main/java/com/baeldung/structuredlogging/User.java new file mode 100644 index 0000000000..c717f2ac8a --- /dev/null +++ b/logging-modules/logging-techniques/src/main/java/com/baeldung/structuredlogging/User.java @@ -0,0 +1,38 @@ +package com.baeldung.structuredlogging; + +public class User { + + private String id; + private String name; + private String password; + + public User(String id, String name, String password) { + this.id = id; + this.name = name; + this.password = password; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/logging-modules/logging-techniques/src/main/resources/logback.xml b/logging-modules/logging-techniques/src/main/resources/logback.xml new file mode 100644 index 0000000000..bed18774fd --- /dev/null +++ b/logging-modules/logging-techniques/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + true + + + + XXXX + password + + + + + + + + + + \ No newline at end of file diff --git a/logging-modules/logging-techniques/src/test/java/StructuredLog4jExampleUnitTest.java b/logging-modules/logging-techniques/src/test/java/StructuredLog4jExampleUnitTest.java new file mode 100644 index 0000000000..f6256bdda7 --- /dev/null +++ b/logging-modules/logging-techniques/src/test/java/StructuredLog4jExampleUnitTest.java @@ -0,0 +1,42 @@ + +import com.baeldung.structuredlogging.User; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static net.logstash.logback.argument.StructuredArguments.kv; + +public class StructuredLog4jExampleUnitTest { + + Logger logger = LoggerFactory.getLogger("logger_name_example"); + + @Test + void whenInfoLoggingData_thenFormatItCorrectly() { + User user = new User("1", "John Doe", "123456"); + + logger.atInfo().setMessage("Processed user succesfully") + .addKeyValue("user_info", user) + .log(); + } + + @Test + void givenStructuredLog_whenUseLog4j_thenExtractCorrectInformation() { + User user = new User("1", "John Doe", "123456"); + + try { + throwExceptionMethod(); + } catch (RuntimeException ex) { + logger.atError().addKeyValue("user_info", user) + .setMessage("Error processing given user") + .addKeyValue("exception_class", ex.getClass().getSimpleName()) + .addKeyValue("error_message", ex.getMessage()) + .log(); + } + } + + private void throwExceptionMethod() { + throw new RuntimeException("Error saving user data", new AssertionError()); + } +} diff --git a/logging-modules/logging-techniques/src/test/resources/log4j.xml b/logging-modules/logging-techniques/src/test/resources/log4j.xml new file mode 100644 index 0000000000..d24324613d --- /dev/null +++ b/logging-modules/logging-techniques/src/test/resources/log4j.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file