diff --git a/README.md b/README.md
index 9ccd9bd..4ba0c53 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
+* **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;
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/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);
}
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