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

Commit

Permalink
Merge pull request #28 from danieljamesscott/master
Browse files Browse the repository at this point in the history
Add option to configure sending hostname
  • Loading branch information
Moocar committed Dec 17, 2013
2 parents fe584cb + 21b64fe commit c56e8e0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Add the following to your logback.xml configuration file.
<graylog2ServerHost>localhost</graylog2ServerHost>
<graylog2ServerPort>12201</graylog2ServerPort>
<useLoggerName>true</useLoggerName>
<hostName>sendinghost</hostName>
<useThreadName>true</useThreadName>
<useMarker>true</useMarker>
<graylog2ServerVersion>0.9.6</graylog2ServerVersion>
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/me/moocar/logbackgelf/GelfAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class GelfAppender extends AppenderBase<ILoggingEvent> {
private Map<String, String> additionalFields = new HashMap<String, String>();
private Map<String, String> staticAdditionalFields = new HashMap<String, String>();
private boolean includeFullMDC;
private String hostName;

// The following are hidden (not configurable)
private int shortMessageLength = 255;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
*
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/me/moocar/logbackgelf/GelfLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class GelfLayout extends LayoutBase<ILoggingEvent> {
// 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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/me/moocar/logbackgelf/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static String createLongMessage() {
public void setup() throws SocketException, UnknownHostException {
server = TestServer.build();
server.start();
host = getLocalHostName();
host = "Test";
}

@Test
Expand All @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<graylog2ServerHost>localhost</graylog2ServerHost>
<graylog2ServerPort>12201</graylog2ServerPort>
<useLoggerName>true</useLoggerName>
<hostName>Test</hostName>
<graylog2ServerVersion>0.9.6</graylog2ServerVersion>
<chunkThreshold>1000</chunkThreshold>
<messagePattern>%m%rEx</messagePattern>
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/staticAdditionalFields.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<graylog2ServerHost>localhost</graylog2ServerHost>
<graylog2ServerPort>12201</graylog2ServerPort>
<useLoggerName>true</useLoggerName>
<hostName>Test</hostName>
<graylog2ServerVersion>0.9.6</graylog2ServerVersion>
<chunkThreshold>1000</chunkThreshold>
<messagePattern>%m%rEx</messagePattern>
Expand Down

0 comments on commit c56e8e0

Please sign in to comment.