BAEL-6754 Parameterized Logging with Slf4j (#14447)
- updated logback-classic version - updated logback.xml to have only one appender for new classes - added sample parameterized logging and fluent logging
This commit is contained in:
parent
0da3664191
commit
f78b9f0b9b
|
@ -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)
|
|
@ -24,11 +24,6 @@
|
|||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback.contrib</groupId>
|
||||
<artifactId>logback-json-classic</artifactId>
|
||||
|
@ -75,6 +70,12 @@
|
|||
<version>${angus.activation.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
|
@ -118,8 +119,9 @@
|
|||
<docx4j.version>3.3.5</docx4j.version>
|
||||
<angus.mail.version>2.0.1</angus.mail.version>
|
||||
<angus.activation.version>2.0.0</angus.activation.version>
|
||||
<logback.version>1.3.5</logback.version>
|
||||
<logback.version>1.4.8</logback.version>
|
||||
<slf4j.version>2.0.4</slf4j.version>
|
||||
<lombok.version>1.18.22</lombok.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
<appender name="out" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %kvp%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
|
@ -66,4 +66,7 @@
|
|||
<appender-ref ref="mask" />
|
||||
</root>
|
||||
|
||||
<logger name="com.baeldung.parameterized.logging" additivity="false" level="INFO">
|
||||
<appender-ref ref="out"/>
|
||||
</logger>
|
||||
</configuration>
|
Loading…
Reference in New Issue