From c57dbec77faba895fb527c39cac7c8563dd773ac Mon Sep 17 00:00:00 2001 From: Daniel Scott Date: Wed, 11 Dec 2013 23:26:57 +0000 Subject: [PATCH 1/3] Add option to configure sending hostname --- .../me/moocar/logbackgelf/GelfAppender.java | 21 ++++++++++++++++--- .../moocar/logbackgelf/IntegrationTest.java | 3 ++- src/test/resources/logback.xml | 1 + src/test/resources/staticAdditionalFields.xml | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/me/moocar/logbackgelf/GelfAppender.java b/src/main/java/me/moocar/logbackgelf/GelfAppender.java index c31e5da..a89865d 100644 --- a/src/main/java/me/moocar/logbackgelf/GelfAppender.java +++ b/src/main/java/me/moocar/logbackgelf/GelfAppender.java @@ -33,6 +33,7 @@ public class GelfAppender extends AppenderBase { private Map additionalFields = new HashMap(); private Map staticAdditionalFields = new HashMap(); private boolean includeFullMDC; + private String hostName; // The following are hidden (not configurable) private int shortMessageLength = 255; @@ -93,14 +94,16 @@ private void initExecutor() { padSeq = true; } - String hostname = getLocalHostName(); + if (hostName == null) { + hostName = getLocalHostName(); + } PayloadChunker payloadChunker = new PayloadChunker(chunkThreshold, maxChunks, - new MessageIdProvider(messageIdLength, MessageDigest.getInstance("MD5"), hostname), + new MessageIdProvider(messageIdLength, MessageDigest.getInstance("MD5"), hostName), new ChunkFactory(chunkedGelfId, padSeq)); GelfConverter converter = new GelfConverter(facility, useLoggerName, useThreadName, useMarker, additionalFields, - staticAdditionalFields, shortMessageLength, hostname, messagePattern, shortMessagePattern, + staticAdditionalFields, shortMessageLength, hostName, messagePattern, shortMessagePattern, includeFullMDC); appenderExecutor = new AppenderExecutor(transport, payloadChunker, converter, new Zipper(), chunkThreshold); @@ -234,6 +237,18 @@ public void setIncludeFullMDC(boolean includeFullMDC) { this.includeFullMDC = includeFullMDC; } + /** + * Override the local hostname using a config option + * @return the local hostname (defaults to getLocalHost() if not overridden + * in config + */ + public String getHostName() { + return hostName; + } + + public void setHostName(String hostName) { + this.hostName = hostName; + } /** * Add an additional field. This is mainly here for compatibility with logback.xml * diff --git a/src/test/java/me/moocar/logbackgelf/IntegrationTest.java b/src/test/java/me/moocar/logbackgelf/IntegrationTest.java index 9b47e29..0c5faa8 100644 --- a/src/test/java/me/moocar/logbackgelf/IntegrationTest.java +++ b/src/test/java/me/moocar/logbackgelf/IntegrationTest.java @@ -50,7 +50,7 @@ private static String createLongMessage() { public void setup() throws SocketException, UnknownHostException { server = TestServer.build(); server.start(); - host = getLocalHostName(); + host = "Test"; } @Test @@ -66,6 +66,7 @@ public void test() throws IOException, JoranException { assertMapEquals(makeMap(message), removeFields(lastRequest)); assertTrue(lastRequest.containsKey("level")); assertTrue(lastRequest.containsKey("timestamp")); + assertTrue(lastRequest.containsKey("host")); // Test with IP and requestID in MDC ipAddress = "87.345.23.55"; diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml index 89af3c8..5adde14 100644 --- a/src/test/resources/logback.xml +++ b/src/test/resources/logback.xml @@ -4,6 +4,7 @@ localhost 12201 true + Test 0.9.6 1000 %m%rEx diff --git a/src/test/resources/staticAdditionalFields.xml b/src/test/resources/staticAdditionalFields.xml index bc7899c..b417b2c 100644 --- a/src/test/resources/staticAdditionalFields.xml +++ b/src/test/resources/staticAdditionalFields.xml @@ -4,6 +4,7 @@ localhost 12201 true + Test 0.9.6 1000 %m%rEx From 3abca0056bb20d2bcd0fe09cb15fc40b56d6c421 Mon Sep 17 00:00:00 2001 From: dscott Date: Thu, 12 Dec 2013 09:24:50 +0000 Subject: [PATCH 2/3] Update documentation; Add hostName member variable to GelfLayout --- README.md | 2 ++ .../java/me/moocar/logbackgelf/GelfLayout.java | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ccd9bd..eb0d5b1 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Add the following to your logback.xml configuration file. localhost 12201 true + sendinghost true true 0.9.6 @@ -69,6 +70,7 @@ will be the name of the thread. Defaults to false; changed from 0.9.5 -> 0.9.6. Allowed values = 0.9.5 and 0.9.6. Defaults to "0.9.6" * **chunkThreshold**: The maximum number of bytes allowed by the payload before the message should be chunked into smaller packets. Defaults to 1000 +* **sendinghost** The hostname of the sending host. Defaults to getLocalHostName() * **useMarker**: If true, and the user has used an slf4j marker (http://slf4j.org/api/org/slf4j/Marker.html) in their log message by using one of the marker-overloaded log methods (http://slf4j.org/api/org/slf4j/Logger.html), then the marker.toString() will be added to the gelf message as the field "_marker". Defaults to false; diff --git a/src/main/java/me/moocar/logbackgelf/GelfLayout.java b/src/main/java/me/moocar/logbackgelf/GelfLayout.java index 3f41c9f..8dcbd03 100644 --- a/src/main/java/me/moocar/logbackgelf/GelfLayout.java +++ b/src/main/java/me/moocar/logbackgelf/GelfLayout.java @@ -13,6 +13,7 @@ public class GelfLayout extends LayoutBase { // The following are configurable via logback configuration private String facility = "GELF"; private boolean useLoggerName = false; + private String hostName; private boolean useThreadName = false; private boolean useMarker = false; private boolean appendLineSeparator = false; @@ -60,6 +61,16 @@ public void setUseLoggerName(boolean useLoggerName) { this.useLoggerName = useLoggerName; } + public String getHostName() + { + return hostName; + } + + public void setHostName(final String hostName) + { + this.hostName = hostName; + } + public boolean isUseThreadName() { return useThreadName; } @@ -146,7 +157,11 @@ public void start() { private void createConverter() { try { - this.converter = new GelfConverter(facility, useLoggerName, useThreadName, useMarker, additionalFields, staticAdditionalFields, shortMessageLength, getLocalHostName(), messagePattern, shortMessagePattern, includeFullMDC); + if (hostName == null) { + hostName = getLocalHostName(); + } + + this.converter = new GelfConverter(facility, useLoggerName, useThreadName, useMarker, additionalFields, staticAdditionalFields, shortMessageLength, hostName, messagePattern, shortMessagePattern, includeFullMDC); } catch (Exception e) { throw new RuntimeException("Unable to initialize converter", e); } From 21b64fe9b33d0a50f9d5a954383416cb1b59efea Mon Sep 17 00:00:00 2001 From: dscott Date: Tue, 17 Dec 2013 09:12:56 +0000 Subject: [PATCH 3/3] Fixed README to correct name of variable --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb0d5b1..4ba0c53 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ will be the name of the thread. Defaults to false; changed from 0.9.5 -> 0.9.6. Allowed values = 0.9.5 and 0.9.6. Defaults to "0.9.6" * **chunkThreshold**: The maximum number of bytes allowed by the payload before the message should be chunked into smaller packets. Defaults to 1000 -* **sendinghost** The hostname of the sending host. Defaults to getLocalHostName() +* **hostName** The hostname of the sending host. Defaults to getLocalHostName() * **useMarker**: If true, and the user has used an slf4j marker (http://slf4j.org/api/org/slf4j/Marker.html) in their log message by using one of the marker-overloaded log methods (http://slf4j.org/api/org/slf4j/Logger.html), then the marker.toString() will be added to the gelf message as the field "_marker". Defaults to false;