Skip to content

Remove trailing space from serialization #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void serializeObjectStart(StringBuilder builder, long timeMillis)
builder.append('{');
builder.append("\"@timestamp\":\"");
TIMESTAMP_SERIALIZER.serializeEpochTimestampAsIsoDateTime(builder, timeMillis);
builder.append("\", ");
builder.append("\",");
}

public static void serializeEcsVersion(StringBuilder builder) {
Expand Down Expand Up @@ -84,7 +84,7 @@ public static void serializeThreadId(StringBuilder builder, long threadId) {
public static void serializeFormattedMessage(StringBuilder builder, String message) {
builder.append("\"message\":\"");
JsonUtils.quoteAsString(message, builder);
builder.append("\", ");
builder.append("\",");
}

public static void serializeServiceName(StringBuilder builder, String serviceName) {
Expand Down Expand Up @@ -121,7 +121,7 @@ public static void serializeLogLevel(StringBuilder builder, String level) {
}
builder.append('\"');
JsonUtils.quoteAsString(level, builder);
builder.append("\", ");
builder.append("\",");
}

public static void serializeTag(StringBuilder builder, String tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,27 @@ final void setUpSpec() throws Exception {
@Test
void testMetadata() throws Exception {
debug("test");
assertThat(getAndValidateLastLogLine().get("process.thread.name").textValue()).isEqualTo(Thread.currentThread().getName());
assertThat(getAndValidateLastLogLine().get("service.name").textValue()).isEqualTo("test");
assertThat(getAndValidateLastLogLine().get("service.node.name").textValue()).isEqualTo("test-node");
assertThat(Instant.parse(getAndValidateLastLogLine().get("@timestamp").textValue())).isCloseTo(Instant.now(), within(1, ChronoUnit.MINUTES));
assertThat(getAndValidateLastLogLine().get("log.level").textValue()).isIn("DEBUG", "FINE");
assertThat(getAndValidateLastLogLine().get("log.logger")).isNotNull();
assertThat(getAndValidateLastLogLine().get("event.dataset").textValue()).isEqualTo("testdataset");
assertThat(getAndValidateLastLogLine().get("ecs.version").textValue()).isEqualTo("1.2.0");
validateLog(getAndValidateLastLogLine());
JsonNode logLine = getAndValidateLastLogLine();
System.out.println(logLine.toPrettyString());
assertThat(logLine.get("process.thread.name").textValue()).isEqualTo(Thread.currentThread().getName());
assertThat(logLine.get("service.name").textValue()).isEqualTo("test");
assertThat(logLine.get("service.node.name").textValue()).isEqualTo("test-node");
assertThat(Instant.parse(logLine.get("@timestamp").textValue())).isCloseTo(Instant.now(), within(1, ChronoUnit.MINUTES));
assertThat(logLine.get("log.level").textValue()).isIn("DEBUG", "FINE");
assertThat(logLine.get("log.logger")).isNotNull();
assertThat(logLine.get("event.dataset").textValue()).isEqualTo("testdataset");
assertThat(logLine.get("ecs.version").textValue()).isEqualTo("1.2.0");
validateLog(logLine);
}

@Test
protected final void testAdditionalFields() throws Exception {
debug("test");
assertThat(getAndValidateLastLogLine().get("key1").textValue()).isEqualTo("value1");
assertThat(getAndValidateLastLogLine().get("key2").textValue()).isEqualTo("value2");
validateLog(getAndValidateLastLogLine());
JsonNode logLine = getAndValidateLastLogLine();
System.out.println(logLine.toPrettyString());
assertThat(logLine.get("key1").textValue()).isEqualTo("value1");
assertThat(logLine.get("key2").textValue()).isEqualTo("value2");
validateLog(logLine);
}

@Test
Expand Down Expand Up @@ -189,6 +193,7 @@ void testMdc() throws Exception {
void testLogException() throws Exception {
error("test", new RuntimeException("test"));
JsonNode log = getAndValidateLastLogLine();
System.out.println(log.toPrettyString());
assertThat(log.get("log.level").textValue()).isIn("ERROR", "SEVERE");
assertThat(log.get("error.message").textValue()).isEqualTo("test");
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName());
Expand All @@ -205,9 +210,11 @@ void testLogExceptionNullMessage() throws Exception {
@Test
void testLogOrigin() throws Exception {
debug("test");
assertThat(getAndValidateLastLogLine().at("/log/origin/file/name").textValue()).endsWith(".java");
assertThat(getAndValidateLastLogLine().at("/log/origin/function").textValue()).isEqualTo("debug");
assertThat(getAndValidateLastLogLine().at("/log/origin/file/line").intValue()).isPositive();
JsonNode logLine = getAndValidateLastLogLine();
System.out.println(logLine.toPrettyString());
assertThat(logLine.at("/log/origin/file/name").textValue()).endsWith(".java");
assertThat(logLine.at("/log/origin/function").textValue()).isEqualTo("debug");
assertThat(logLine.at("/log/origin/file/line").intValue()).isPositive();
}

public boolean putMdc(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void testEscaping() throws IOException {
void serializeNullDoesNotThrowAnException() throws JsonProcessingException {
StringBuilder stringBuilder = new StringBuilder();
EcsJsonSerializer.serializeFormattedMessage(stringBuilder, null);
assertThat(stringBuilder.toString()).isEqualTo("\"message\":\"null\", ");
assertThat(stringBuilder.toString()).isEqualTo("\"message\":\"null\",");
}

@Test
Expand Down