EcsJsonSerializer { public static void serializeException(StringBuilder builder, Throwable thrown, boolean stackTraceAsArray) { if (thrown != null) { builder.append("\"error.type\":\""); JsonUtils.quoteAsString(thrown.getClass().getName(), 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); builder.append("]"); } else { builder.append("\"error.stack_trace\":\""); JsonUtils.quoteAsString(formatThrowable(thrown), builder); builder.append("\""); } } }  static CharSequence toNullSafeString(final CharSequence s); static void serializeObjectStart(StringBuilder builder, long timeMillis); static void serializeObjectEnd(StringBuilder builder); static void serializeLoggerName(StringBuilder builder, String loggerName); static void serializeThreadName(StringBuilder builder, String threadName); static void serializeThreadId(StringBuilder builder, long threadId); static void serializeFormattedMessage(StringBuilder builder, String message); static void serializeServiceName(StringBuilder builder, String serviceName); static void serializeEventDataset(StringBuilder builder, String eventDataset); static void serializeLogLevel(StringBuilder builder, String level); static void serializeTag(StringBuilder builder, String tag); static void serializeTagStart(StringBuilder builder); static void serializeSingleTag(StringBuilder builder, String tag); static void serializeTagEnd(StringBuilder builder); static void serializeOrigin(StringBuilder builder, StackTraceElement stackTraceElement); static void serializeOrigin(StringBuilder builder, String fileName, String methodName, int lineNumber); static void serializeMDC(StringBuilder builder, Map<String, ?> properties); static void serializeException(StringBuilder builder, Throwable thrown, boolean stackTraceAsArray); static void serializeException(StringBuilder builder, String exceptionClassName, String exceptionMessage, String stackTrace, boolean stackTraceAsArray); static void removeIfEndsWith(StringBuilder sb, String ending); static boolean endsWith(StringBuilder sb, String ending); static StringBuilder getMessageStringBuilder(); static String computeEventDataset(String eventDataset, String serviceName);  }
@Test void serializeExceptionAsString() throws IOException { Exception exception = new Exception("foo"); StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.append('{'); EcsJsonSerializer.serializeException(jsonBuilder, exception, false); jsonBuilder.append('}'); JsonNode jsonNode = new ObjectMapper().readTree(jsonBuilder.toString()); 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)); assertThat(jsonNode.get("error.stack_trace").textValue()).isEqualTo(stringWriter.toString()); }
@Test void serializeExceptionAsArray() throws IOException { Exception exception = new Exception("foo"); StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.append('{'); EcsJsonSerializer.serializeException(jsonBuilder, exception, true); jsonBuilder.append('}'); System.out.println(jsonBuilder); JsonNode jsonNode = new ObjectMapper().readTree(jsonBuilder.toString()); 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)); assertThat(StreamSupport.stream(jsonNode.get("error.stack_trace").spliterator(), false) .map(JsonNode::textValue) .collect(Collectors.joining(System.lineSeparator(), "", System.lineSeparator()))) .isEqualTo(stringWriter.toString()); }