From 0cd90c4fef6fdc23ef5a1a4441bda59ef7789bda Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Wed, 17 Mar 2021 21:17:20 +0100 Subject: [PATCH] Improve Loki message line. --- gradle.properties | 2 +- .../biz/nellemann/syslogd/SyslogPrinter.java | 16 +++++++++++++--- .../biz/nellemann/syslogd/net/LokiClient.java | 1 + .../nellemann/syslogd/SyslogPrinterTest.groovy | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index fe73369..1b16f7f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ id = syslogd group = biz.nellemann.syslogd -version = 1.2.1 +version = 1.2.2 diff --git a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java index 0fdb83d..2f1ad34 100644 --- a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java +++ b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java @@ -112,17 +112,27 @@ public class SyslogPrinter { */ public static String toLoki(SyslogMessage msg) { StringBuilder sb = new StringBuilder("{ \"streams\": [ { \"stream\": {"); - sb.append(String.format(" \"host\": \"%s\",", msg.hostname)); + sb.append(String.format(" \"hostname\": \"%s\",", msg.hostname)); sb.append(String.format(" \"facility\": \"%s\",", msg.facility)); - sb.append(String.format(" \"severity\": \"%s\",", msg.severity)); + sb.append(String.format(" \"level\": \"%s\",", msg.severity)); sb.append(String.format(" \"application\": \"%s\"", msg.application)); sb.append("}, \"values\": [ "); - sb.append(String.format("[ \"%d\", \"%s\" ]", msg.timestamp.getEpochSecond() * 1000000000l, msg.message)); + sb.append(String.format("[ \"%d\", \"%s\" ]", msg.timestamp.getEpochSecond() * 1000000000l, getMessageLine(msg))); sb.append(" ] } ] }"); return sb.toString(); } + private static String getMessageLine(SyslogMessage msg) { + StringBuilder sb = new StringBuilder(); + sb.append(String.format("[%s.%s] ", msg.facility, msg.severity)); + sb.append(String.format("%s ", msg.hostname)); + sb.append(String.format("%s ", msg.application)); + sb.append(msg.message); + return sb.toString(); + } + + static private String getPri(Facility facility, Severity severity) { int pri = (facility.toNumber() * 8) + severity.toNumber(); return String.format("%c%d%c", '<', pri, '>'); diff --git a/src/main/java/biz/nellemann/syslogd/net/LokiClient.java b/src/main/java/biz/nellemann/syslogd/net/LokiClient.java index d500fb1..95c3d99 100644 --- a/src/main/java/biz/nellemann/syslogd/net/LokiClient.java +++ b/src/main/java/biz/nellemann/syslogd/net/LokiClient.java @@ -42,6 +42,7 @@ public class LokiClient { int responseCode = con.getResponseCode(); if(responseCode != 204) { log.warn("send() - response: " + responseCode); + log.debug("send() - msg: " + msg); } } catch (IOException e) { diff --git a/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy b/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy index 44f9694..00c17c4 100644 --- a/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy +++ b/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy @@ -34,7 +34,7 @@ class SyslogPrinterTest extends Specification { String output = SyslogPrinter.toLoki(msg) then: - output == '{ "streams": [ { "stream": { "host": "xps13", "facility": "user", "severity": "notice", "application": "mark"}, "values": [ [ "1600845200000000000", "adfdfdf3432434565656" ] ] } ] }' + output == '{ "streams": [ { "stream": { "hostname": "xps13", "facility": "user", "level": "notice", "application": "mark"}, "values": [ [ "1600845200000000000", "[user.notice] xps13 mark adfdfdf3432434565656" ] ] } ] }' } }