Skip to content

Commit

Permalink
update dependencies and serialize Optional properly
Browse files Browse the repository at this point in the history
  • Loading branch information
cleaning-agent committed Nov 29, 2024
1 parent 6f5ded1 commit fbd7aa9
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 240 deletions.
52 changes: 32 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,39 @@
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.4</version>
<version>8.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<version>1.18.36</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.14</version>
<version>1.5.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.18.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.1</version>
<version>6.1.14</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>3.2.0</version>
<version>3.4.0</version>
<optional>true</optional>
<scope>compile</scope>
</dependency>
Expand All @@ -77,31 +82,31 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.2.0</version>
<version>3.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.1</version>
<version>5.10.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<version>5.10.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<version>3.25.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.8.0</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -118,7 +123,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand All @@ -130,7 +135,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.1</version>
<version>3.6.0</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
Expand All @@ -141,7 +146,7 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.12.5</version><!-- use a current version of Checkstyle -->
<version>10.20.1</version><!-- use a current version of Checkstyle -->
</dependency>
</dependencies>
<executions>
Expand All @@ -157,20 +162,20 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<version>3.5.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.1</version>
<version>3.1.1</version>
<configuration>
<tagNameFormat>@{project.version}</tagNameFormat>
</configuration>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<version>1.7.0</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -192,12 +197,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.3</version>
<version>3.11.1</version>
<configuration>
<source>${java.version}</source>
<encoding>${encoding}</encoding>
Expand All @@ -222,7 +227,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.1.0</version>
<version>3.2.7</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down Expand Up @@ -296,5 +301,12 @@
<developerConnection>scm:git:ssh://[email protected]/dm-drogeriemarkt/structured-logging.git</developerConnection>
<url>http://github.com/dm-drogeriemarkt/structured-logging/tree/master</url>
</scm>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>

</project>
60 changes: 31 additions & 29 deletions src/main/java/de/dm/prom/structuredlogging/MdcContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

Expand All @@ -29,28 +30,34 @@
*/
@Slf4j
public final class MdcContext implements java.io.Closeable {
private final String oldValue; //MDC value outside this context
private final String key;

private static final ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper();
private static Optional<ObjectMapper> customObjectMapper = Optional.empty();

static {
SimpleModule module = new SimpleModule();
module.addSerializer(Instant.class, new ToStringSerializer());
module.addSerializer(LocalDate.class, new ToStringSerializer());
module.addSerializer(LocalDateTime.class, new ToStringSerializer());
module.addSerializer(OffsetDateTime.class, new ToStringSerializer());
module.addSerializer(OffsetTime.class, new ToStringSerializer());
module.addSerializer(Period.class, new ToStringSerializer());
module.addSerializer(ZonedDateTime.class, new ToStringSerializer());
module.addSerializer(LocalTime.class, new ToStringSerializer());
module.addSerializer(Duration.class, new ToStringSerializer());
module.addSerializer(MonthDay.class, new ToStringSerializer());
module.addSerializer(Year.class, new ToStringSerializer());
module.addSerializer(YearMonth.class, new ToStringSerializer());
module.addSerializer(Instant.class, ToStringSerializer.instance);
module.addSerializer(LocalDate.class, ToStringSerializer.instance);
module.addSerializer(LocalDateTime.class, ToStringSerializer.instance);
module.addSerializer(OffsetDateTime.class, ToStringSerializer.instance);
module.addSerializer(OffsetTime.class, ToStringSerializer.instance);
module.addSerializer(Period.class, ToStringSerializer.instance);
module.addSerializer(ZonedDateTime.class, ToStringSerializer.instance);
module.addSerializer(LocalTime.class, ToStringSerializer.instance);
module.addSerializer(Duration.class, ToStringSerializer.instance);
module.addSerializer(MonthDay.class, ToStringSerializer.instance);
module.addSerializer(Year.class, ToStringSerializer.instance);
module.addSerializer(YearMonth.class, ToStringSerializer.instance);

DEFAULT_OBJECT_MAPPER.registerModule(module);
DEFAULT_OBJECT_MAPPER.registerModule(new Jdk8Module());
}

private final String oldValue; //MDC value outside this context
private final String key;

private MdcContext(String key, Object value) {
this.key = key;
oldValue = putToMDCwithOverwriteWarning(key, toJson(value));
}

/**
Expand Down Expand Up @@ -288,20 +295,6 @@ public static void update(Object mdcValue) {
updateMdcContent(mdcValue.getClass().getSimpleName(), toJson(mdcValue));
}

private MdcContext(String key, Object value) {
this.key = key;
oldValue = putToMDCwithOverwriteWarning(key, toJson(value));
}

@Override
public void close() {
if (oldValue == null) {
MDC.remove(key);
} else {
MDC.put(key, oldValue);
}
}

private static String toJson(Object object) {
String objectToJson = "{\"json_error\":\"Unserializable Object.\"}";
//needs to be an object, not a string, for Kibana. Otherwise, Kibana will throw away the log entry because the field has the wrong type.
Expand Down Expand Up @@ -355,4 +348,13 @@ private static void logFailedUpdate(String key) {
log.warn("Cannot update content of MDC key {} in {}.{}({}:{}) because it does not exist.",
key, caller.getClassName(), caller.getMethodName(), caller.getFileName(), caller.getLineNumber());
}

@Override
public void close() {
if (oldValue == null) {
MDC.remove(key);
} else {
MDC.put(key, oldValue);
}
}
}
5 changes: 5 additions & 0 deletions src/test/java/de/dm/prom/structuredlogging/ExampleBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Optional;

import static java.time.DayOfWeek.MONDAY;
import static java.time.Month.JANUARY;
Expand All @@ -42,6 +43,8 @@ class ExampleBean {
private MonthDay monthDay;
private Year year;
private YearMonth yearMonth;
private Optional<String> emptyOptional;
private Optional<String> nonEmptyOptional;

static ExampleBean getExample() {
LocalDateTime importantTime = LocalDateTime.of(2019, JANUARY, 1, 13, 37);
Expand All @@ -63,6 +66,8 @@ static ExampleBean getExample() {
.monthDay(MonthDay.of(12, 24))
.year(Year.of(1984))
.yearMonth(YearMonth.of(2000, 8))
.emptyOptional(Optional.empty())
.nonEmptyOptional(Optional.of("Hello"))
.build();
}
}
Loading

0 comments on commit fbd7aa9

Please sign in to comment.