From aeeca9ffbe0b2ce7f11fd4324348818c0b17b285 Mon Sep 17 00:00:00 2001 From: Daniel Flassak Date: Fri, 16 Apr 2021 16:51:58 +0200 Subject: [PATCH 1/3] add Duration serialization to tests --- src/test/java/de/dm/prom/structuredlogging/ExampleBean.java | 3 +++ .../java/de/dm/prom/structuredlogging/MdcContextUnitTest.java | 3 ++- .../structuredlogging/StructuredMdcJsonProviderUnitTest.java | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java b/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java index 7eafe25..da06c2a 100644 --- a/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java +++ b/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java @@ -3,6 +3,7 @@ import lombok.Builder; import lombok.Data; +import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; @@ -28,6 +29,7 @@ class ExampleBean { private Period period; private ZonedDateTime zonedDateTime; private LocalTime localTime; + private Duration duration; static ExampleBean getExample() { LocalDateTime importantTime = LocalDateTime.of(2019, Month.JANUARY, 1, 13, 37); @@ -43,6 +45,7 @@ static ExampleBean getExample() { .period(Period.ofDays(42)) .zonedDateTime(ZonedDateTime.of(importantTime, ZoneId.of("UTC"))) .localTime(LocalTime.of(13, 37)) + .duration(Duration.ofMinutes(42)) .build(); } } diff --git a/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java b/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java index af460c5..75a5c29 100644 --- a/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java +++ b/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java @@ -27,7 +27,8 @@ class MdcContextUnitTest { "\"offsetTime\":\"13:37+01:00\"," + "\"period\":\"P42D\"," + "\"zonedDateTime\":\"2019-01-01T13:37Z[UTC]\"," + - "\"localTime\":\"13:37\"" + + "\"localTime\":\"13:37\"," + + "\"duration\":\"PT42M\"" + "}"; @RegisterExtension diff --git a/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java b/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java index 238d2c4..f84d0c3 100644 --- a/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java +++ b/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java @@ -43,6 +43,7 @@ class StructuredMdcJsonProviderUnitTest { "\"offsetTime\":\"13:37+01:00\"," + "\"period\":\"P42D\"," + "\"localTime\":\"13:37\"," + + "\"duration\":\"PT42M\"," + "\"zonedDateTime\":\"2019-01-01T13:37Z[UTC]\"}}"; private Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); From a794dcdb2428931f86debae11bd5c236f6a130de Mon Sep 17 00:00:00 2001 From: Daniel Flassak Date: Fri, 16 Apr 2021 17:22:07 +0200 Subject: [PATCH 2/3] add proper serialization for remaining java.time types (MonthDay, YearMonth and Year) --- .../dm/prom/structuredlogging/MdcContext.java | 6 ++++++ .../prom/structuredlogging/ExampleBean.java | 19 ++++++++++++++++++- .../structuredlogging/MdcContextUnitTest.java | 16 ++++++++++++---- .../StructuredMdcJsonProviderUnitTest.java | 5 +++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/dm/prom/structuredlogging/MdcContext.java b/src/main/java/de/dm/prom/structuredlogging/MdcContext.java index 489784a..d41b83f 100644 --- a/src/main/java/de/dm/prom/structuredlogging/MdcContext.java +++ b/src/main/java/de/dm/prom/structuredlogging/MdcContext.java @@ -13,9 +13,12 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.MonthDay; import java.time.OffsetDateTime; import java.time.OffsetTime; import java.time.Period; +import java.time.Year; +import java.time.YearMonth; import java.time.ZonedDateTime; /** @@ -42,6 +45,9 @@ public final class MdcContext implements java.io.Closeable { 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()); OBJECT_MAPPER.registerModule(module); } diff --git a/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java b/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java index da06c2a..646a3a6 100644 --- a/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java +++ b/src/test/java/de/dm/prom/structuredlogging/ExampleBean.java @@ -3,19 +3,26 @@ import lombok.Builder; import lombok.Data; +import java.time.DayOfWeek; import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; +import java.time.MonthDay; import java.time.OffsetDateTime; import java.time.OffsetTime; import java.time.Period; +import java.time.Year; +import java.time.YearMonth; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import static java.time.DayOfWeek.MONDAY; +import static java.time.Month.JANUARY; + @Data @Builder class ExampleBean { @@ -30,9 +37,14 @@ class ExampleBean { private ZonedDateTime zonedDateTime; private LocalTime localTime; private Duration duration; + private DayOfWeek dayOfWeek; + private Month month; + private MonthDay monthDay; + private Year year; + private YearMonth yearMonth; static ExampleBean getExample() { - LocalDateTime importantTime = LocalDateTime.of(2019, Month.JANUARY, 1, 13, 37); + LocalDateTime importantTime = LocalDateTime.of(2019, JANUARY, 1, 13, 37); return ExampleBean.builder() .name("John Doe") @@ -46,6 +58,11 @@ static ExampleBean getExample() { .zonedDateTime(ZonedDateTime.of(importantTime, ZoneId.of("UTC"))) .localTime(LocalTime.of(13, 37)) .duration(Duration.ofMinutes(42)) + .dayOfWeek(MONDAY) + .month(JANUARY) + .monthDay(MonthDay.of(12, 24)) + .year(Year.of(1984)) + .yearMonth(YearMonth.of(2000, 8)) .build(); } } diff --git a/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java b/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java index 75a5c29..69b600e 100644 --- a/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java +++ b/src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java @@ -28,7 +28,12 @@ class MdcContextUnitTest { "\"period\":\"P42D\"," + "\"zonedDateTime\":\"2019-01-01T13:37Z[UTC]\"," + "\"localTime\":\"13:37\"," + - "\"duration\":\"PT42M\"" + + "\"duration\":\"PT42M\"," + + "\"dayOfWeek\":\"MONDAY\"," + + "\"month\":\"JANUARY\"," + + "\"monthDay\":\"--12-24\"," + + "\"year\":\"1984\"," + + "\"yearMonth\":\"2000-08\"" + "}"; @RegisterExtension @@ -62,9 +67,12 @@ private void assertMdcFieldContentIsCorrect(String mdcFieldName, String expected JsonNode sampleBeanTree = objectMapper.readTree(expectedJson); assertThat(jsonStringFromMdc).startsWith(JSON_PREFIX); - JsonNode treeFromMDC = objectMapper.readTree(jsonStringFromMdc.replaceFirst(JSON_PREFIX, "")); + String actualJson = jsonStringFromMdc.replaceFirst(JSON_PREFIX, ""); + JsonNode treeFromMDC = objectMapper.readTree(actualJson); - assertThat(treeFromMDC).isEqualTo(sampleBeanTree); + assertThat(treeFromMDC).as("Expecting:\n<%s>\nto be equal to:\n<%s>\nbut was not.\n\n\n", + treeFromMDC.toPrettyString(), sampleBeanTree.toPrettyString()) + .isEqualTo(sampleBeanTree); } @Test @@ -111,7 +119,7 @@ void withSimpleNameAsKey() throws Exception { } @Test - void failedUpdate() throws Exception { + void failedUpdate() { MdcContext.update(ExampleBean.getExample()); logCapture.assertLogged(WARN, "^Cannot update content of MDC key ExampleBean in .*\\.failedUpdate\\(MdcContextUnitTest.java:[0-9]+\\) because it does not exist.$"); diff --git a/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java b/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java index f84d0c3..6d15ade 100644 --- a/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java +++ b/src/test/java/de/dm/prom/structuredlogging/StructuredMdcJsonProviderUnitTest.java @@ -44,6 +44,11 @@ class StructuredMdcJsonProviderUnitTest { "\"period\":\"P42D\"," + "\"localTime\":\"13:37\"," + "\"duration\":\"PT42M\"," + + "\"dayOfWeek\":\"MONDAY\"," + + "\"month\":\"JANUARY\"," + + "\"monthDay\":\"--12-24\"," + + "\"year\":\"1984\"," + + "\"yearMonth\":\"2000-08\"," + "\"zonedDateTime\":\"2019-01-01T13:37Z[UTC]\"}}"; private Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); From 6ff50d9a5f927b414ff69c806578aa647d86d60c Mon Sep 17 00:00:00 2001 From: Daniel Flassak Date: Fri, 16 Apr 2021 17:24:51 +0200 Subject: [PATCH 3/3] update README for 2.0.4 and 2.0.5 --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 40c2f66..8fbc2f3 100644 --- a/README.md +++ b/README.md @@ -112,11 +112,11 @@ For consuming logs: If you use maven, add this to your pom.xml: -```pom.xml +```xml de.dm.infrastructure structured-logging - 2.0.3 + 2.0.5 test ``` @@ -289,6 +289,14 @@ public class TimeMachine { ## Changes +### 2.0.5 + +* **New Feature**: Added correct serialization of remaining java.time types (Year, YearMonth and MonthDay) + +### 2.0.4 + +* **New Feature**: Added correct serialization of Duration + ### 2.0.3 * **Bugfix**: The Task Decorator threw a NullPointerException when resetting MDC contents if the target thread's MDC was empty