From 2e231c7b692d11eea542acb9e5d703ee25bbb02c Mon Sep 17 00:00:00 2001 From: normakm Date: Mon, 1 Mar 2021 09:57:43 +0200 Subject: [PATCH] Add version field to GelfMessage #260 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add version field Add version field for GELF versions 1.1 and up as specified here: https://docs.graylog.org/en/latest/pages/gelf.html > version string (UTF-8) > GELF spec version – “1.1”; MUST be set by client library. * Unit test added unit test for version field --- .../java/biz/paluch/logging/gelf/intern/GelfMessage.java | 5 +++++ .../biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java | 1 + 2 files changed, 6 insertions(+) diff --git a/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java b/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java index a78a6c447..817e8959c 100644 --- a/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java +++ b/src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java @@ -37,6 +37,7 @@ public class GelfMessage { public static final String FIELD_TIMESTAMP = "timestamp"; public static final String FIELD_LEVEL = "level"; public static final String FIELD_FACILITY = "facility"; + public static final String FIELD_VERSION = "version"; public static final String ID_NAME = "id"; /** @@ -154,6 +155,10 @@ protected void toJson(OutputAccessor out, String additionalFieldPrefix) { boolean hasFields = writeIfNotEmpty(out, false, FIELD_HOST, getHost()); + if (!isEmpty(version) && !GELF_VERSION_1_0.equals(version)) { + hasFields = writeIfNotEmpty(out, hasFields, FIELD_VERSION, version); + } + if (!isEmpty(shortMessage)) { hasFields = writeIfNotEmpty(out, hasFields, FIELD_SHORT_MESSAGE, getShortMessage()); } diff --git a/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java b/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java index ae0a44e94..8a1316277 100644 --- a/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java +++ b/src/test/java/biz/paluch/logging/gelf/intern/GelfMessageUnitTests.java @@ -236,6 +236,7 @@ void testGelf_v1_1() { assertThat(gelfMessage.getVersion()).isEqualTo(GelfMessage.GELF_VERSION_1_1); assertThat(gelfMessage.toJson()).contains("\"level\":6"); assertThat(gelfMessage.toJson()).contains("\"timestamp\":123.456"); + assertThat(gelfMessage.toJson()).contains("\"version\":\"1.1\""); }