-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://jira.baeldung.com/browse/BAEL-8843
- Loading branch information
1 parent
6328fb1
commit 787ac92
Showing
8 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## Relevant Articles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ | ||
"@timestamp": "2024-12-19T10:34:02.220152180Z", | ||
"log.level": "INFO", | ||
"process.pid": 34696, | ||
"process.thread.name": "main", | ||
"service.name": "BaeldungService", | ||
"service.version": "1", | ||
"service.environment": "Production", | ||
"service.node.name": "Primary", | ||
"log.logger": "com.baeldung.springstructuredlogging.StructuredLoggingApp", | ||
"message": "Starting StructuredLoggingApp using Java 23 with PID 34696 (\/home\/michael\/tutorials\/spring-boot-modules\/spring-boot-3-4\/target\/classes started by michael in \/home\/michael\/tutorials\/spring-boot-modules\/spring-boot-3-4)", | ||
"ecs.version": "8.11" | ||
} | ||
{ | ||
"@timestamp": "2024-12-19T10:34:02.240898973Z", | ||
"log.level": "INFO", | ||
"process.pid": 34696, | ||
"process.thread.name": "main", | ||
"service.name": "BaeldungService", | ||
"service.version": "1", | ||
"service.environment": "Production", | ||
"service.node.name": "Primary", | ||
"log.logger": "com.baeldung.springstructuredlogging.StructuredLoggingApp", | ||
"message": "No active profile set, falling back to 1 default profile: \"default\"", | ||
"ecs.version": "8.11" | ||
} | ||
{ | ||
"@timestamp": "2024-12-19T10:34:03.558243665Z", | ||
"log.level": "INFO", | ||
"process.pid": 34696, | ||
"process.thread.name": "main", | ||
"service.name": "BaeldungService", | ||
"service.version": "1", | ||
"service.environment": "Production", | ||
"service.node.name": "Primary", | ||
"log.logger": "com.baeldung.springstructuredlogging.StructuredLoggingApp", | ||
"message": "Started StructuredLoggingApp in 2.777 seconds (process running for 4.356)", | ||
"ecs.version": "8.11" | ||
} | ||
{ | ||
"@timestamp": "2024-12-19T10:34:03.564752429Z", | ||
"log.level": "INFO", | ||
"process.pid": 34696, | ||
"process.thread.name": "main", | ||
"service.name": "BaeldungService", | ||
"service.version": "1", | ||
"service.environment": "Production", | ||
"service.node.name": "Primary", | ||
"log.logger": "com.baeldung.springstructuredlogging.CustomLog", | ||
"message": "Hello structured logging!", | ||
"userName": "Baeldung", | ||
"userId": "1", | ||
"ecs.version": "8.11" | ||
} | ||
{ | ||
"@timestamp": "2024-12-19T10:34:03.568047793Z", | ||
"log.level": "INFO", | ||
"process.pid": 34696, | ||
"process.thread.name": "main", | ||
"service.name": "BaeldungService", | ||
"service.version": "1", | ||
"service.environment": "Production", | ||
"service.node.name": "Primary", | ||
"log.logger": "com.baeldung.springstructuredlogging.CustomLog", | ||
"message": "Hello Structure logging!", | ||
"userId": "1", | ||
"userName": "Baeldung", | ||
"ecs.version": "8.11" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<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> | ||
<groupId>com.baeldung.spring-boot-simple</groupId> | ||
<artifactId>spring-boot-simple</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<name>spring-boot-3-4</name> | ||
<packaging>war</packaging> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
<version>${spring-boot.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<spring-boot.version>3.4.0</spring-boot.version> | ||
</properties> | ||
</project> |
34 changes: 34 additions & 0 deletions
34
...modules/spring-boot-3-4/src/main/java/com/baeldung/springstructuredlogging/CustomLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.baeldung.springstructuredlogging; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CustomLog implements CommandLineRunner { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomLog.class); | ||
|
||
public void additionalDetailsWithMdc() { | ||
MDC.put("userId", "1"); | ||
MDC.put("userName", "Baeldung"); | ||
LOGGER.info("Hello structured logging!"); | ||
MDC.remove("userId"); | ||
MDC.remove("userName"); | ||
} | ||
|
||
public void additionalDetailsUsingFluentApi() { | ||
LOGGER.atInfo() | ||
.setMessage("Hello Structure logging!") | ||
.addKeyValue("userId", "1") | ||
.addKeyValue("userName", "Baeldung") | ||
.log(); | ||
} | ||
|
||
@Override | ||
public void run(String... args) { | ||
additionalDetailsWithMdc(); | ||
additionalDetailsUsingFluentApi(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...-3-4/src/main/java/com/baeldung/springstructuredlogging/MyStructuredLoggingFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.baeldung.springstructuredlogging; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import org.springframework.boot.json.JsonWriter; | ||
import org.springframework.boot.logging.structured.StructuredLogFormatter; | ||
|
||
public class MyStructuredLoggingFormatter implements StructuredLogFormatter<ILoggingEvent> { | ||
|
||
private final JsonWriter<ILoggingEvent> writer = JsonWriter.<ILoggingEvent>of((members) -> { | ||
members.add("time", ILoggingEvent::getInstant); | ||
members.add("level", ILoggingEvent::getLevel); | ||
members.add("thread", ILoggingEvent::getThreadName); | ||
members.add("message", ILoggingEvent::getFormattedMessage); | ||
members.add("application").usingMembers((application) -> { | ||
application.add("name", "StructuredLoggingDemo"); | ||
application.add("version", "1.0.0-SNAPSHOT"); | ||
}); | ||
members.add("node").usingMembers((node) -> { | ||
node.add("hostname", "node-1"); | ||
node.add("ip", "10.0.0.7"); | ||
}); | ||
}).withNewLineAtEnd(); | ||
|
||
@Override | ||
public String format(ILoggingEvent event) { | ||
return this.writer.writeToString(event); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ing-boot-3-4/src/main/java/com/baeldung/springstructuredlogging/StructuredLoggingApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.springstructuredlogging; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class StructuredLoggingApp { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(StructuredLoggingApp.class, args); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
spring-boot-modules/spring-boot-3-4/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
logging.structured.format.file=ecs | ||
logging.file.name=log.json | ||
logging.structured.ecs.service.name=BaeldungService | ||
logging.structured.ecs.service.version=1 | ||
logging.structured.ecs.service.environment=Production | ||
logging.structured.ecs.service.node-name=Primary | ||
|