Skip to content
This repository was archived by the owner on May 31, 2021. It is now read-only.

Commit c56e8e0

Browse files
committed
Merge pull request #28 from danieljamesscott/master
Add option to configure sending hostname
2 parents fe584cb + 21b64fe commit c56e8e0

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Add the following to your logback.xml configuration file.
3535
<graylog2ServerHost>localhost</graylog2ServerHost>
3636
<graylog2ServerPort>12201</graylog2ServerPort>
3737
<useLoggerName>true</useLoggerName>
38+
<hostName>sendinghost</hostName>
3839
<useThreadName>true</useThreadName>
3940
<useMarker>true</useMarker>
4041
<graylog2ServerVersion>0.9.6</graylog2ServerVersion>
@@ -69,6 +70,7 @@ will be the name of the thread. Defaults to false;
6970
changed from 0.9.5 -> 0.9.6. Allowed values = 0.9.5 and 0.9.6. Defaults to "0.9.6"
7071
* **chunkThreshold**: The maximum number of bytes allowed by the payload before the message should be chunked into
7172
smaller packets. Defaults to 1000
73+
* **hostName** The hostname of the sending host. Defaults to getLocalHostName()
7274
* **useMarker**: If true, and the user has used an slf4j marker (http://slf4j.org/api/org/slf4j/Marker.html) in their
7375
log message by using one of the marker-overloaded log methods (http://slf4j.org/api/org/slf4j/Logger.html), then the
7476
marker.toString() will be added to the gelf message as the field "_marker". Defaults to false;

src/main/java/me/moocar/logbackgelf/GelfAppender.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class GelfAppender extends AppenderBase<ILoggingEvent> {
3333
private Map<String, String> additionalFields = new HashMap<String, String>();
3434
private Map<String, String> staticAdditionalFields = new HashMap<String, String>();
3535
private boolean includeFullMDC;
36+
private String hostName;
3637

3738
// The following are hidden (not configurable)
3839
private int shortMessageLength = 255;
@@ -93,14 +94,16 @@ private void initExecutor() {
9394
padSeq = true;
9495
}
9596

96-
String hostname = getLocalHostName();
97+
if (hostName == null) {
98+
hostName = getLocalHostName();
99+
}
97100

98101
PayloadChunker payloadChunker = new PayloadChunker(chunkThreshold, maxChunks,
99-
new MessageIdProvider(messageIdLength, MessageDigest.getInstance("MD5"), hostname),
102+
new MessageIdProvider(messageIdLength, MessageDigest.getInstance("MD5"), hostName),
100103
new ChunkFactory(chunkedGelfId, padSeq));
101104

102105
GelfConverter converter = new GelfConverter(facility, useLoggerName, useThreadName, useMarker, additionalFields,
103-
staticAdditionalFields, shortMessageLength, hostname, messagePattern, shortMessagePattern,
106+
staticAdditionalFields, shortMessageLength, hostName, messagePattern, shortMessagePattern,
104107
includeFullMDC);
105108

106109
appenderExecutor = new AppenderExecutor(transport, payloadChunker, converter, new Zipper(), chunkThreshold);
@@ -234,6 +237,18 @@ public void setIncludeFullMDC(boolean includeFullMDC) {
234237
this.includeFullMDC = includeFullMDC;
235238
}
236239

240+
/**
241+
* Override the local hostname using a config option
242+
* @return the local hostname (defaults to getLocalHost() if not overridden
243+
* in config
244+
*/
245+
public String getHostName() {
246+
return hostName;
247+
}
248+
249+
public void setHostName(String hostName) {
250+
this.hostName = hostName;
251+
}
237252
/**
238253
* Add an additional field. This is mainly here for compatibility with logback.xml
239254
*

src/main/java/me/moocar/logbackgelf/GelfLayout.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class GelfLayout extends LayoutBase<ILoggingEvent> {
1313
// The following are configurable via logback configuration
1414
private String facility = "GELF";
1515
private boolean useLoggerName = false;
16+
private String hostName;
1617
private boolean useThreadName = false;
1718
private boolean useMarker = false;
1819
private boolean appendLineSeparator = false;
@@ -60,6 +61,16 @@ public void setUseLoggerName(boolean useLoggerName) {
6061
this.useLoggerName = useLoggerName;
6162
}
6263

64+
public String getHostName()
65+
{
66+
return hostName;
67+
}
68+
69+
public void setHostName(final String hostName)
70+
{
71+
this.hostName = hostName;
72+
}
73+
6374
public boolean isUseThreadName() {
6475
return useThreadName;
6576
}
@@ -146,7 +157,11 @@ public void start() {
146157

147158
private void createConverter() {
148159
try {
149-
this.converter = new GelfConverter(facility, useLoggerName, useThreadName, useMarker, additionalFields, staticAdditionalFields, shortMessageLength, getLocalHostName(), messagePattern, shortMessagePattern, includeFullMDC);
160+
if (hostName == null) {
161+
hostName = getLocalHostName();
162+
}
163+
164+
this.converter = new GelfConverter(facility, useLoggerName, useThreadName, useMarker, additionalFields, staticAdditionalFields, shortMessageLength, hostName, messagePattern, shortMessagePattern, includeFullMDC);
150165
} catch (Exception e) {
151166
throw new RuntimeException("Unable to initialize converter", e);
152167
}

src/test/java/me/moocar/logbackgelf/IntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static String createLongMessage() {
5050
public void setup() throws SocketException, UnknownHostException {
5151
server = TestServer.build();
5252
server.start();
53-
host = getLocalHostName();
53+
host = "Test";
5454
}
5555

5656
@Test
@@ -66,6 +66,7 @@ public void test() throws IOException, JoranException {
6666
assertMapEquals(makeMap(message), removeFields(lastRequest));
6767
assertTrue(lastRequest.containsKey("level"));
6868
assertTrue(lastRequest.containsKey("timestamp"));
69+
assertTrue(lastRequest.containsKey("host"));
6970

7071
// Test with IP and requestID in MDC
7172
ipAddress = "87.345.23.55";

src/test/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<graylog2ServerHost>localhost</graylog2ServerHost>
55
<graylog2ServerPort>12201</graylog2ServerPort>
66
<useLoggerName>true</useLoggerName>
7+
<hostName>Test</hostName>
78
<graylog2ServerVersion>0.9.6</graylog2ServerVersion>
89
<chunkThreshold>1000</chunkThreshold>
910
<messagePattern>%m%rEx</messagePattern>

src/test/resources/staticAdditionalFields.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<graylog2ServerHost>localhost</graylog2ServerHost>
55
<graylog2ServerPort>12201</graylog2ServerPort>
66
<useLoggerName>true</useLoggerName>
7+
<hostName>Test</hostName>
78
<graylog2ServerVersion>0.9.6</graylog2ServerVersion>
89
<chunkThreshold>1000</chunkThreshold>
910
<messagePattern>%m%rEx</messagePattern>

0 commit comments

Comments
 (0)