Improve Loki message line.

This commit is contained in:
Mark Nellemann 2021-03-17 21:17:20 +01:00
parent 5e1481e770
commit 0cd90c4fef
4 changed files with 16 additions and 5 deletions

View file

@ -1,3 +1,3 @@
id = syslogd id = syslogd
group = biz.nellemann.syslogd group = biz.nellemann.syslogd
version = 1.2.1 version = 1.2.2

View file

@ -112,17 +112,27 @@ public class SyslogPrinter {
*/ */
public static String toLoki(SyslogMessage msg) { public static String toLoki(SyslogMessage msg) {
StringBuilder sb = new StringBuilder("{ \"streams\": [ { \"stream\": {"); 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(" \"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(String.format(" \"application\": \"%s\"", msg.application));
sb.append("}, \"values\": [ "); 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(" ] } ] }"); sb.append(" ] } ] }");
return sb.toString(); 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) { static private String getPri(Facility facility, Severity severity) {
int pri = (facility.toNumber() * 8) + severity.toNumber(); int pri = (facility.toNumber() * 8) + severity.toNumber();
return String.format("%c%d%c", '<', pri, '>'); return String.format("%c%d%c", '<', pri, '>');

View file

@ -42,6 +42,7 @@ public class LokiClient {
int responseCode = con.getResponseCode(); int responseCode = con.getResponseCode();
if(responseCode != 204) { if(responseCode != 204) {
log.warn("send() - response: " + responseCode); log.warn("send() - response: " + responseCode);
log.debug("send() - msg: " + msg);
} }
} catch (IOException e) { } catch (IOException e) {

View file

@ -34,7 +34,7 @@ class SyslogPrinterTest extends Specification {
String output = SyslogPrinter.toLoki(msg) String output = SyslogPrinter.toLoki(msg)
then: 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" ] ] } ] }'
} }
} }