Skip to content

Commit

Permalink
Merge pull request #18104 from Michaelin007/structuredlogging
Browse files Browse the repository at this point in the history
  • Loading branch information
Maiklins authored Dec 25, 2024
2 parents 45365e0 + cbbe0a0 commit fa9b892
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions spring-boot-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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"
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

0 comments on commit fa9b892

Please sign in to comment.