From 669fda33b4894bc7fac08f4e81081434d758185f Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Thu, 19 Sep 2019 16:15:46 +0200 Subject: [PATCH] Use error.type for exception class instead of error.code (#32) --- README.md | 4 ++-- .../src/main/java/co/elastic/logging/EcsJsonSerializer.java | 4 ++-- .../test/java/co/elastic/logging/AbstractEcsLoggingTest.java | 2 +- .../test/java/co/elastic/logging/EcsJsonSerializerTest.java | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cf6a841e..2218e435 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Example: {"@timestamp":"2019-08-06T12:09:12.375Z", "log.level": "INFO", "message":"Tomcat started on port(s): 8080 (http) with context path ''", "service.name":"spring-petclinic","process.thread.name":"restartedMain","log.logger":"org.springframework.boot.web.embedded.tomcat.TomcatWebServer"} {"@timestamp":"2019-08-06T12:09:12.379Z", "log.level": "INFO", "message":"Started PetClinicApplication in 7.095 seconds (JVM running for 9.082)", "service.name":"spring-petclinic","process.thread.name":"restartedMain","log.logger":"org.springframework.samples.petclinic.PetClinicApplication"} {"@timestamp":"2019-08-06T14:08:40.199Z", "log.level":"DEBUG", "message":"init find form", "service.name":"spring-petclinic","process.thread.name":"http-nio-8080-exec-8","log.logger":"org.springframework.samples.petclinic.owner.OwnerController","transaction.id":"28b7fb8d5aba51f1","trace.id":"2869b25b5469590610fea49ac04af7da"} -{"@timestamp":"2019-09-17T13:16:48.038Z", "log.level":"ERROR", "message":"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Expected: controller used to showcase what happens when an exception is thrown] with root cause", "process.thread.name":"http-nio-8080-exec-1","log.logger":"org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]","log.origin":{"file.name":"DirectJDKLog.java","function":"log","file.line":175},"error.code":"java.lang.RuntimeException","error.message":"Expected: controller used to showcase what happens when an exception is thrown","error.stack_trace":[ +{"@timestamp":"2019-09-17T13:16:48.038Z", "log.level":"ERROR", "message":"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Expected: controller used to showcase what happens when an exception is thrown] with root cause", "process.thread.name":"http-nio-8080-exec-1","log.logger":"org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]","log.origin":{"file.name":"DirectJDKLog.java","function":"log","file.line":175},"error.type":"java.lang.RuntimeException","error.message":"Expected: controller used to showcase what happens when an exception is thrown","error.stack_trace":[ "java.lang.RuntimeException: Expected: controller used to showcase what happens when an exception is thrown", "\tat org.springframework.samples.petclinic.system.CrashController.triggerException(CrashController.java:33)", "\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)", @@ -84,7 +84,7 @@ We recommend using this library to log into a JSON log file and let Filebeat sen |[`log.origin.file.line`](https://www.elastic.co/guide/en/ecs/current/ecs-log.html)|[`StackTraceElement#getLineNumber()`](https://docs.oracle.com/javase/6/docs/api/java/lang/StackTraceElement.html#getLineNumber())| |[`log.origin.function`](https://www.elastic.co/guide/en/ecs/current/ecs-log.html)|[`StackTraceElement#getMethodName()`](https://docs.oracle.com/javase/6/docs/api/java/lang/StackTraceElement.html#getMethodName())| |[`message`](https://www.elastic.co/guide/en/ecs/current/ecs-base.html)|[`LogEvent#getMessage()`](https://logging.apache.org/log4j/log4j-2.3/log4j-core/apidocs/org/apache/logging/log4j/core/LogEvent.html#getMessage())| -|[`error.code`](https://www.elastic.co/guide/en/ecs/current/ecs-error.html)|[`Throwable#getClass()`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#getClass())| +|[`error.type`](https://www.elastic.co/guide/en/ecs/current/ecs-error.html)|[`Throwable#getClass()`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#getClass())| |[`error.message`](https://www.elastic.co/guide/en/ecs/current/ecs-error.html)|[`Throwable#getStackTrace()`](https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getMessage())| |[`error.stack_trace`](https://www.elastic.co/guide/en/ecs/current/ecs-error.html)|[`Throwable#getStackTrace()`](https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getStackTrace())| |[`process.thread.name`](https://www.elastic.co/guide/en/ecs/current/ecs-process.html)|[`LogEvent#getThreadName()`](https://logging.apache.org/log4j/log4j-2.3/log4j-core/apidocs/org/apache/logging/log4j/core/LogEvent.html#getThreadName()) | 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 a49b4ef7..35e90bf6 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 @@ -156,7 +156,7 @@ public static void serializeLabels(StringBuilder builder, Map labels, public static void serializeException(StringBuilder builder, Throwable thrown, boolean stackTraceAsArray) { if (thrown != null) { - builder.append("\"error.code\":\""); + builder.append("\"error.type\":\""); JsonUtils.quoteAsString(thrown.getClass().getName(), builder); builder.append("\","); builder.append("\"error.message\":\""); @@ -175,7 +175,7 @@ public static void serializeException(StringBuilder builder, Throwable thrown, b } public static void serializeException(StringBuilder builder, String exceptionClassName, String exceptionMessage, String stackTrace, boolean stackTraceAsArray) { - builder.append("\"error.code\":\""); + builder.append("\"error.type\":\""); JsonUtils.quoteAsString(exceptionClassName, builder); builder.append("\","); builder.append("\"error.message\":\""); 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 285f24ca..66dfb6fc 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 @@ -88,7 +88,7 @@ void testLogException() throws Exception { error("test", new RuntimeException("test")); assertThat(getLastLogLine().get("log.level").textValue()).isEqualTo("ERROR"); assertThat(getLastLogLine().get("error.message").textValue()).isEqualTo("test"); - assertThat(getLastLogLine().get("error.code").textValue()).isEqualTo(RuntimeException.class.getName()); + assertThat(getLastLogLine().get("error.type").textValue()).isEqualTo(RuntimeException.class.getName()); String stackTrace = StreamSupport.stream(getLastLogLine().get("error.stack_trace").spliterator(), false) .map(JsonNode::textValue) .collect(Collectors.joining("\n", "", "\n")); diff --git a/ecs-logging-core/src/test/java/co/elastic/logging/EcsJsonSerializerTest.java b/ecs-logging-core/src/test/java/co/elastic/logging/EcsJsonSerializerTest.java index 48e20f0f..bffeb551 100644 --- a/ecs-logging-core/src/test/java/co/elastic/logging/EcsJsonSerializerTest.java +++ b/ecs-logging-core/src/test/java/co/elastic/logging/EcsJsonSerializerTest.java @@ -47,7 +47,7 @@ void serializeExceptionAsString() throws IOException { jsonBuilder.append('}'); JsonNode jsonNode = new ObjectMapper().readTree(jsonBuilder.toString()); - assertThat(jsonNode.get("error.code").textValue()).isEqualTo(exception.getClass().getName()); + assertThat(jsonNode.get("error.type").textValue()).isEqualTo(exception.getClass().getName()); assertThat(jsonNode.get("error.message").textValue()).isEqualTo("foo"); StringWriter stringWriter = new StringWriter(); exception.printStackTrace(new PrintWriter(stringWriter)); @@ -64,7 +64,7 @@ void serializeExceptionAsArray() throws IOException { System.out.println(jsonBuilder); JsonNode jsonNode = new ObjectMapper().readTree(jsonBuilder.toString()); - assertThat(jsonNode.get("error.code").textValue()).isEqualTo(exception.getClass().getName()); + assertThat(jsonNode.get("error.type").textValue()).isEqualTo(exception.getClass().getName()); assertThat(jsonNode.get("error.message").textValue()).isEqualTo("foo"); StringWriter stringWriter = new StringWriter(); exception.printStackTrace(new PrintWriter(stringWriter));