diff --git a/ecs-logging-core/src/main/java/co/elastic/logging/EcsJsonSerializer.java b/ecs-logging-core/src/main/java/co/elastic/logging/EcsJsonSerializer.java index 35e90bf6..ea0d7f80 100644 --- a/ecs-logging-core/src/main/java/co/elastic/logging/EcsJsonSerializer.java +++ b/ecs-logging-core/src/main/java/co/elastic/logging/EcsJsonSerializer.java @@ -159,9 +159,13 @@ public static void serializeException(StringBuilder builder, Throwable thrown, b builder.append("\"error.type\":\""); JsonUtils.quoteAsString(thrown.getClass().getName(), builder); builder.append("\","); - builder.append("\"error.message\":\""); - JsonUtils.quoteAsString(thrown.getMessage(), builder); - builder.append("\","); + + String message = thrown.getMessage(); + if (message != null) { + builder.append("\"error.message\":\""); + JsonUtils.quoteAsString(message, builder); + builder.append("\","); + } if (stackTraceAsArray) { builder.append("\"error.stack_trace\":[").append(NEW_LINE); formatThrowableAsArray(builder, thrown); diff --git a/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java b/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java index 53bc1d2f..df67149a 100644 --- a/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java +++ b/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java @@ -103,6 +103,13 @@ void testLogException() throws Exception { assertThat(stackTrace).contains("at co.elastic.logging.AbstractEcsLoggingTest.testLogException"); } + @Test + void testLogExceptionNullMessage() throws Exception { + error("test", new RuntimeException()); + assertThat(getLastLogLine().get("error.message")).isNull(); + assertThat(getLastLogLine().get("error.type").textValue()).isEqualTo(RuntimeException.class.getName()); + } + @Test void testLogOrigin() throws Exception { debug("test");