diff --git a/logging-modules/logback/README.md b/logging-modules/logback/README.md index 9a9ffc6e70..1bebcd20d3 100644 --- a/logging-modules/logback/README.md +++ b/logging-modules/logback/README.md @@ -6,3 +6,4 @@ - [Mask Sensitive Data in Logs With Logback](https://www.baeldung.com/logback-mask-sensitive-data) - [Creating a Custom Logback Appender](https://www.baeldung.com/custom-logback-appender) - [A Guide To Logback](https://www.baeldung.com/logback) +- [Parameterized Logging with SLF4J](TODO) \ No newline at end of file diff --git a/logging-modules/logback/pom.xml b/logging-modules/logback/pom.xml index e7313d902a..cddc2e72ea 100644 --- a/logging-modules/logback/pom.xml +++ b/logging-modules/logback/pom.xml @@ -24,11 +24,6 @@ logback-classic ${logback.version} - - ch.qos.logback - logback-core - ${logback.version} - ch.qos.logback.contrib logback-json-classic @@ -75,6 +70,12 @@ ${angus.activation.version} runtime + + org.projectlombok + lombok + provided + ${lombok.version} + @@ -118,8 +119,9 @@ 3.3.5 2.0.1 2.0.0 - 1.3.5 + 1.4.8 2.0.4 + 1.18.22 \ No newline at end of file diff --git a/logging-modules/logback/src/main/java/com/baeldung/parameterized/logging/FluentLoggingPlayground.java b/logging-modules/logback/src/main/java/com/baeldung/parameterized/logging/FluentLoggingPlayground.java new file mode 100644 index 0000000000..cc27d70c33 --- /dev/null +++ b/logging-modules/logback/src/main/java/com/baeldung/parameterized/logging/FluentLoggingPlayground.java @@ -0,0 +1,36 @@ +package com.baeldung.parameterized.logging; + +import java.time.LocalDateTime; +import java.time.ZonedDateTime; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class FluentLoggingPlayground { + + public static void main(String[] args) { + + Exception exceptionCause = new Exception(new IllegalArgumentException("Something unprocessable")); + + log.atInfo() + .setMessage("App is running at {}, zone = {}") + .addArgument(LocalDateTime.now()) + .addArgument(ZonedDateTime.now().getZone()) + .log(); + + log.atInfo() + .setMessage("App is running at {}, zone = {}") + .addArgument(LocalDateTime.now()) + .addArgument(ZonedDateTime.now().getZone()) + .setCause(exceptionCause) + .log(); + + log.atInfo() + .setMessage("App is running at") + .addKeyValue("time", LocalDateTime.now()) + .addKeyValue("zone", ZonedDateTime.now().getZone()) + .setCause(exceptionCause) + .log(); + + } +} diff --git a/logging-modules/logback/src/main/java/com/baeldung/parameterized/logging/LoggingPlayground.java b/logging-modules/logback/src/main/java/com/baeldung/parameterized/logging/LoggingPlayground.java new file mode 100644 index 0000000000..32a937fcf7 --- /dev/null +++ b/logging-modules/logback/src/main/java/com/baeldung/parameterized/logging/LoggingPlayground.java @@ -0,0 +1,40 @@ +package com.baeldung.parameterized.logging; + +import java.time.LocalDateTime; +import java.time.ZonedDateTime; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LoggingPlayground { + + public static final Logger log = LoggerFactory.getLogger(LoggingPlayground.class); + + public static void main(String[] args) { + log.info("App is running at {}", LocalDateTime.now()); + + log.info("App is running at {}, zone = {}", LocalDateTime.now(), ZonedDateTime.now() + .getZone()); + + log.info("App is running at {}, zone = {}, java version = {}", LocalDateTime.now(), ZonedDateTime.now() + .getZone(), System.getProperty("java.version")); + + log.info("App is running at {}, zone = {}, java version = {}, java vm = {}", LocalDateTime.now(), ZonedDateTime.now() + .getZone(), System.getProperty("java.version"), System.getProperty("java.vm.name")); + + //old approach to print multiple parameters + log.info("App is running at {}, zone = {}, java version = {}, java vm = {}", + new Object[] { ZonedDateTime.now(), ZonedDateTime.now().getZone(), System.getProperty("java.version"), System.getProperty("java.vm.name") }); + + Exception exceptionCause = new Exception(new IllegalArgumentException("Something unprocessable")); + + //exception as last parameters is considered as exception and printed with trace + log.info("App is running at {}, zone = {}, java version = {}, java vm = {}", LocalDateTime.now(), ZonedDateTime.now() + .getZone(), System.getProperty("java.version"), System.getProperty("java.vm.name"), exceptionCause); + + //exception in between parameters is considered as pure parameter and printed without trace + log.info("App is running at {}, zone = {}, java version = {}, java vm = {}", LocalDateTime.now(), ZonedDateTime.now() + .getZone(), System.getProperty("java.version"), exceptionCause, System.getProperty("java.vm.name")); + + } +} diff --git a/logging-modules/logback/src/main/resources/logback.xml b/logging-modules/logback/src/main/resources/logback.xml index 2d56c110e0..07d5a6bee7 100644 --- a/logging-modules/logback/src/main/resources/logback.xml +++ b/logging-modules/logback/src/main/resources/logback.xml @@ -6,7 +6,7 @@ - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %kvp%n @@ -66,4 +66,7 @@ + + + \ No newline at end of file