Skip to content

Commit 669fda3

Browse files
authored
Use error.type for exception class instead of error.code (#32)
1 parent 645978b commit 669fda3

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Example:
2020
{"@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"}
2121
{"@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"}
2222
{"@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"}
23-
{"@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":[
23+
{"@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":[
2424
"java.lang.RuntimeException: Expected: controller used to showcase what happens when an exception is thrown",
2525
"\tat org.springframework.samples.petclinic.system.CrashController.triggerException(CrashController.java:33)",
2626
"\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
8484
|[`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())|
8585
|[`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())|
8686
|[`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())|
87-
|[`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())|
87+
|[`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())|
8888
|[`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())|
8989
|[`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())|
9090
|[`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()) |

ecs-logging-core/src/main/java/co/elastic/logging/EcsJsonSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static void serializeLabels(StringBuilder builder, Map<String, ?> labels,
156156

157157
public static void serializeException(StringBuilder builder, Throwable thrown, boolean stackTraceAsArray) {
158158
if (thrown != null) {
159-
builder.append("\"error.code\":\"");
159+
builder.append("\"error.type\":\"");
160160
JsonUtils.quoteAsString(thrown.getClass().getName(), builder);
161161
builder.append("\",");
162162
builder.append("\"error.message\":\"");
@@ -175,7 +175,7 @@ public static void serializeException(StringBuilder builder, Throwable thrown, b
175175
}
176176

177177
public static void serializeException(StringBuilder builder, String exceptionClassName, String exceptionMessage, String stackTrace, boolean stackTraceAsArray) {
178-
builder.append("\"error.code\":\"");
178+
builder.append("\"error.type\":\"");
179179
JsonUtils.quoteAsString(exceptionClassName, builder);
180180
builder.append("\",");
181181
builder.append("\"error.message\":\"");

ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void testLogException() throws Exception {
8888
error("test", new RuntimeException("test"));
8989
assertThat(getLastLogLine().get("log.level").textValue()).isEqualTo("ERROR");
9090
assertThat(getLastLogLine().get("error.message").textValue()).isEqualTo("test");
91-
assertThat(getLastLogLine().get("error.code").textValue()).isEqualTo(RuntimeException.class.getName());
91+
assertThat(getLastLogLine().get("error.type").textValue()).isEqualTo(RuntimeException.class.getName());
9292
String stackTrace = StreamSupport.stream(getLastLogLine().get("error.stack_trace").spliterator(), false)
9393
.map(JsonNode::textValue)
9494
.collect(Collectors.joining("\n", "", "\n"));

ecs-logging-core/src/test/java/co/elastic/logging/EcsJsonSerializerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void serializeExceptionAsString() throws IOException {
4747
jsonBuilder.append('}');
4848
JsonNode jsonNode = new ObjectMapper().readTree(jsonBuilder.toString());
4949

50-
assertThat(jsonNode.get("error.code").textValue()).isEqualTo(exception.getClass().getName());
50+
assertThat(jsonNode.get("error.type").textValue()).isEqualTo(exception.getClass().getName());
5151
assertThat(jsonNode.get("error.message").textValue()).isEqualTo("foo");
5252
StringWriter stringWriter = new StringWriter();
5353
exception.printStackTrace(new PrintWriter(stringWriter));
@@ -64,7 +64,7 @@ void serializeExceptionAsArray() throws IOException {
6464
System.out.println(jsonBuilder);
6565
JsonNode jsonNode = new ObjectMapper().readTree(jsonBuilder.toString());
6666

67-
assertThat(jsonNode.get("error.code").textValue()).isEqualTo(exception.getClass().getName());
67+
assertThat(jsonNode.get("error.type").textValue()).isEqualTo(exception.getClass().getName());
6868
assertThat(jsonNode.get("error.message").textValue()).isEqualTo("foo");
6969
StringWriter stringWriter = new StringWriter();
7070
exception.printStackTrace(new PrintWriter(stringWriter));

0 commit comments

Comments
 (0)