Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https://jira.baeldung.com/browse/BAEL-8843 #18104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spring-boot-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<module>spring-boot-3-test-pitfalls</module>
<module>spring-boot-3-testcontainers</module>
<module>spring-boot-3-2</module>
<module>spring-boot-3-4</module>
<module>spring-boot-resilience4j</module>
<module>spring-boot-properties</module>
<module>spring-boot-properties-2</module>
Expand Down
1 change: 1 addition & 0 deletions spring-boot-modules/spring-boot-3-4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Relevant Articles
69 changes: 69 additions & 0 deletions spring-boot-modules/spring-boot-3-4/log.json
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"
}
27 changes: 27 additions & 0 deletions spring-boot-modules/spring-boot-3-4/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a new module, let's add it to the parent pom as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright @Maiklins
Thanks!

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-3-4</groupId>
<artifactId>spring-boot-3-4</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>spring-boot-3-4</name>
<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>
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();
}
}
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);
}
}
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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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