Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merged changes from ssundell: Additional GELF fields added for logger…

… name and NDC.
  • Loading branch information...
commit 2543070e1d4acb1250330e94361e5ba3f94fb076 1 parent 5fdbf1f
@joschi joschi authored
View
28 README.md
@@ -9,27 +9,7 @@ It's very simple GELF implementation in pure Java with the Log4j appender. It su
How to use GELFJ
----------------
-Grab latest JAR from the [downloads section](https://github.com/t0xa/gelfj/downloads) and drop it into your classpath.
-
-Examples
---------
-
-To send a GELF message:
-
- GelfMessage message = new GelfMessage("Short message", "Long message", new Date(), "1");
- GelfSender gelfSender = new GelfSender("localhost");
- if (message.isValid()) {
- gelfSender.sendMessage(message)
- }
-
-To send a GELF message with additional fields:
-
- GelfMessage message = new GelfMessage("Short message", "Long message", new Date(), "1");
- message.addField("specialVariable", "LOLCAT").addField("anotherValue", "typos in my closet");
- GelfSender gelfSender = new GelfSender("localhost");
- if (message.isValid()) {
- gelfSender.sendMessage(message)
- }
+Drop the latest JAR into your classpath and configure Log4j to use it.
Log4j appender
--------------
@@ -42,7 +22,7 @@ To use GELF Facility as appender in Log4j (XML configuration format):
<param name="graylogHost" value="192.168.0.201"/>
<param name="originHost" value="my.machine.example.com"/>
<param name="extractStacktrace" value="true"/>
- <param name="useDiagnosticContext" value="true"/>
+ <param name="addExtendedInformation" value="true"/>
<param name="facility" value="gelf-java"/>
<param name="Threshold" value="INFO"/>
</appender>
@@ -63,7 +43,7 @@ Or, in the log4j.properties format:
log4j.appender.graylog2.facility=gelf-java
log4j.appender.graylog2.layout=org.apache.log4j.PatternLayout
log4j.appender.graylog2.extractStacktrace=true
- log4j.appender.graylog2.useDiagnosticContext=true
+ log4j.appender.graylog2.addExtendedInformation=true
# Send all INFO logs to graylog2
log4j.rootLogger=INFO, graylog2
@@ -78,7 +58,7 @@ GelfAppender supports the following options:
- **graylogPort**: Port on which the Graylog2 server is listening; default 12201 (*optional*)
- **originHost**: Name of the originating host; defaults to the local hostname (*optional*)
- **extractStacktrace** (true/false): Add stacktraces to the GELF message; default false (*optional*)
-- **useDiagnosticContext** (true/false): Use additional information from Log4j's NDC/MDC; default false (*optional*)
+- **addExtendedInformation** (true/false): Add extended information like Log4j's NDC/MDC; default false (*optional*)
- **facility**: Facility which to use in the GELF message; default "gelf-java"
What is GELF
View
2  examples/log4j.properties
@@ -12,7 +12,7 @@ log4j.appender.graylog2.originHost=my.machine.example.com
log4j.appender.graylog2.layout=org.apache.log4j.PatternLayout
log4j.appender.graylog2.additionalFields={'environment': 'DEV', 'application': 'MyAPP'}
log4j.appender.graylog2.extractStacktrace=true
-log4j.appender.graylog2.useDiagnosticContext=true
+log4j.appender.graylog2.addExtendedInformation=true
# Send all INFO logs to graylog2
log4j.rootLogger=INFO, graylog2
View
2  examples/log4j.xml
@@ -14,7 +14,7 @@
<param name="originHost" value="my.machine.example.com"/>
<param name="additionalFields" value="{'environment': 'DEV', 'application': 'MyAPP'}"/>
<param name="extractStacktrace" value="true"/>
- <param name="useDiagnosticContext" value="true"/>
+ <param name="addExtendedInformation" value="true"/>
<param name="Facility" value="USER"/>
<param name="FacilityPrinting" value="true"/>
<param name="Threshold" value="INFO"/>
View
24 src/main/java/org/graylog2/log/GelfAppender.java
@@ -29,7 +29,7 @@
private String facility;
private GelfSender gelfSender;
private boolean extractStacktrace;
- private boolean useDiagnosticContext;
+ private boolean addExtendedInformation;
private Map<String, String> fields;
private static final int MAX_SHORT_MESSAGE_LENGTH = 250;
@@ -55,7 +55,7 @@ private String getLocalHostName() {
}
public void setAdditionalFields(String additionalFields) {
- fields = (Map<String, String>) JSONValue.parse(additionalFields.replaceAll("'", "\""));
+ fields = (Map<String, String>) JSONValue.parse(additionalFields.replaceAll("'", "\""));
}
public int getGraylogPort() {
@@ -98,12 +98,12 @@ public void setOriginHost(String originHost) {
this.originHost = originHost;
}
- public boolean isUseDiagnosticContext() {
- return useDiagnosticContext;
+ public boolean isAddExtendedInformation() {
+ return addExtendedInformation;
}
- public void setUseDiagnosticContext(boolean useDiagnosticContext) {
- this.useDiagnosticContext = useDiagnosticContext;
+ public void setAddExtendedInformation(boolean addExtendedInformation) {
+ this.addExtendedInformation = addExtendedInformation;
}
@Override
@@ -166,12 +166,15 @@ protected void append(LoggingEvent event) {
fields.remove(ORIGIN_HOST_KEY);
}
- for(String key : fields.keySet()) {
- gelfMessage.addField(key, fields.get(key));
+ for (Map.Entry<String, String> entry : fields.entrySet()) {
+ gelfMessage.addField(entry.getKey(), entry.getValue());
}
}
- if (isUseDiagnosticContext()) {
+ if (isAddExtendedInformation()) {
+
+ gelfMessage.addField(LOGGER_NAME, event.getLoggerName());
+ gelfMessage.addField(JAVA_TIMESTAMP, Long.toString(timeStamp));
// Get MDC and add a GELF field for each key/value pair
Map<String, Object> mdc = MDC.getContext();
@@ -190,7 +193,6 @@ protected void append(LoggingEvent event) {
gelfMessage.addField(LOGGER_NDC, ndc);
}
- gelfMessage.addField(JAVA_TIMESTAMP, Long.toString(timeStamp));
}
if(!getGelfSender().sendMessage(gelfMessage)) {
@@ -202,7 +204,7 @@ public GelfSender getGelfSender() {
return gelfSender;
}
- private long getTimestamp(LoggingEvent event) {
+ private long getTimestamp(LoggingEvent event) {
return Log4jVersionChecker.getTimeStamp(event);
}
View
35 src/test/java/org/graylog2/log/GelfAppenderTest.java
@@ -24,6 +24,7 @@
*/
public class GelfAppenderTest {
+ private static final String CLASS_NAME = GelfAppenderTest.class.getCanonicalName();
private TestGelfSender gelfSender;
private GelfAppender gelfAppender;
@@ -48,7 +49,8 @@ public void append(LoggingEvent event) {
@Test
public void ensureHostnameForMessage() {
- LoggingEvent event = new LoggingEvent("a.b.c.DasClass", Category.getInstance(this.getClass()), 123L, Priority.INFO, "Das Auto", new RuntimeException("LOL"));
+ LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(GelfAppenderTest.class), 123L, Priority.INFO, "Das Auto",
+ new RuntimeException("LOL"));
gelfAppender.append(event);
assertThat("Message hostname", gelfSender.getLastMessage().getHost(), notNullValue());
@@ -61,7 +63,7 @@ public void ensureHostnameForMessage() {
@Test
public void handleNullInAppend() {
- LoggingEvent event = new LoggingEvent("a.b.c.DasClass", Category.getInstance(this.getClass()), 123L, Priority.INFO, null, new RuntimeException("LOL"));
+ LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(this.getClass()), 123L, Priority.INFO, null, new RuntimeException("LOL"));
gelfAppender.append(event);
assertThat("Message short message", gelfSender.getLastMessage().getShortMessage(), notNullValue());
@@ -71,9 +73,9 @@ public void handleNullInAppend() {
@Test
public void handleMDC() {
- gelfAppender.setUseDiagnosticContext(true);
+ gelfAppender.setAddExtendedInformation(true);
- LoggingEvent event = new LoggingEvent("a.b.c.DasClass", Category.getInstance(this.getClass()), 123L, Priority.INFO, "", new RuntimeException("LOL"));
+ LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(this.getClass()), 123L, Priority.INFO, "", new RuntimeException("LOL"));
MDC.put("foo", "bar");
gelfAppender.append(event);
@@ -85,32 +87,43 @@ public void handleMDC() {
@Test
public void handleNDC() {
- gelfAppender.setUseDiagnosticContext(true);
+ gelfAppender.setAddExtendedInformation(true);
- LoggingEvent event = new LoggingEvent("a.b.c.DasClass", Category.getInstance(this.getClass()), 123L, Priority.INFO, "", new RuntimeException("LOL"));
+ LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(this.getClass()), 123L, Priority.INFO, "", new RuntimeException("LOL"));
NDC.push("Foobar");
gelfAppender.append(event);
- assertEquals("Foobar", gelfSender.getLastMessage().getAdditonalFields().get("context"));
+ assertEquals("Foobar", gelfSender.getLastMessage().getAdditonalFields().get("loggerNdc"));
}
@Test
- public void disableDiagnosticContext() {
+ public void disableExtendedInformation() {
- gelfAppender.setUseDiagnosticContext(false);
+ gelfAppender.setAddExtendedInformation(false);
- LoggingEvent event = new LoggingEvent("a.b.c.DasClass", Category.getInstance(this.getClass()), 123L, Priority.INFO, "", new RuntimeException("LOL"));
+ LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(this.getClass()), 123L, Priority.INFO, "", new RuntimeException("LOL"));
MDC.put("foo", "bar");
NDC.push("Foobar");
gelfAppender.append(event);
- assertNull(gelfSender.getLastMessage().getAdditonalFields().get("context"));
+ assertNull(gelfSender.getLastMessage().getAdditonalFields().get("loggerNdc"));
assertNull(gelfSender.getLastMessage().getAdditonalFields().get("foo"));
}
+ public void checkExtendedInformation() throws UnknownHostException, SocketException {
+
+ gelfAppender.setAddExtendedInformation(true);
+
+ LoggingEvent event = new LoggingEvent(CLASS_NAME, Category.getInstance(GelfAppenderTest.class), 123L, Priority.INFO, "Das Auto", new RuntimeException("LOL"));
+
+ gelfAppender.append(event);
+
+ assertEquals(gelfSender.getLastMessage().getAdditonalFields().get("logger"), CLASS_NAME);
+ }
+
private class TestGelfSender extends GelfSender {
private GelfMessage lastMessage;
Please sign in to comment.
Something went wrong with that request. Please try again.