Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
18 changes: 18 additions & 0 deletions .nb-gradle/profiles/private/aux-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<gradle-project-properties>
<!--DO NOT EDIT THIS FILE! - Used by the Gradle plugin of NetBeans.-->
<auxiliary>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/>
</open-files>
<editor-bookmarks lastBookmarkId="0" xmlns="http://www.netbeans.org/ns/editor-bookmarks/2"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/Users/DCHILDERS/NetBeansProjects/logback-gelf/src/main/java/com/github/pukkaone/gelf/logback/GelfMessageFactory.java</file>
<file>file:/Users/DCHILDERS/NetBeansProjects/logback-gelf/src/main/java/com/github/pukkaone/gelf/protocol/GelfTCPSender.java</file>
<file>file:/Users/DCHILDERS/NetBeansProjects/logback-gelf/README.adoc</file>
</group>
</open-files>
</auxiliary>
</gradle-project-properties>
4 changes: 4 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Configure a logback appender to send by UDP (XML configuration format):
<threadIncluded>false</threadIncluded>
<additionalField>application=MyApplication</additionalField>
<additionalField>environment=development</additionalField>
<numericField>ResponseTimeMs</numericField>
</appender>
--

Expand Down Expand Up @@ -86,6 +87,9 @@ To send by AMQP:
Include an additional field with literal value in the GELF message.
Give the field name and value in the format `key=value`. (*optional*)

`numericField`::
Convert field names to a numeric type.

`amqpURI`::
AMQP URI (*required when using AMQP integration*)

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
}

dependencies {
compile "ch.qos.logback:logback-classic:0.9.17"
compile "ch.qos.logback:logback-classic:1.1.2"
compile "com.fasterxml.jackson.core:jackson-databind:2.3.0"
compile "com.rabbitmq:amqp-client:3.0.4"
testCompile 'junit:junit:4.11'
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/github/pukkaone/gelf/logback/GelfAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -33,6 +35,7 @@ public class GelfAppender extends AppenderBase<ILoggingEvent> {
private boolean mdcIncluded;
private boolean threadIncluded;
private Map<String, String> additionalFields = new HashMap<>();
private List<String> numericFields = new ArrayList<>();
private String amqpURI;
private String amqpExchange;
private String amqpRoutingKey;
Expand Down Expand Up @@ -142,6 +145,14 @@ public void addAdditionalField(String keyValue) {
additionalFields.put(parts[0], parts[1]);
}

public List<String> getNumericFields() {
return numericFields;
}

public void addNumericField(String value) {
numericFields.add(value);
}

public String getAmqpURI() {
return amqpURI;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.util.LevelToSyslogSeverity;
import com.github.pukkaone.gelf.protocol.GelfMessage;
import java.lang.reflect.Method;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Marker;

Expand Down Expand Up @@ -57,12 +61,11 @@ public GelfMessage createMessage(GelfAppender appender, ILoggingEvent event) {
}
}

List<String> numericFields = appender.getNumericFields();
if (appender.isMdcIncluded()) {
Map<String, String> mdc = event.getMDCPropertyMap();
if (mdc != null) {
for (Map.Entry<String, String> entry : mdc.entrySet()) {
message.addField(entry.getKey(), entry.getValue());
}
addFields(message, mdc, numericFields);
}
}

Expand All @@ -78,10 +81,28 @@ public GelfMessage createMessage(GelfAppender appender, ILoggingEvent event) {
}

Map<String, String> fields = appender.getAdditionalFields();
for (Map.Entry<String, String> entry : fields.entrySet()) {
message.addField(entry.getKey(), entry.getValue());
}
addFields(message, fields, numericFields);

return message;
}

private void addFields(GelfMessage message, Map<String, String> fields, List<String> numericFields)
{
for (Map.Entry<String, String> field : fields.entrySet()) {
String key = field.getKey();
Object value = convertNumericField(numericFields, key, field.getValue());
message.addField(key, value);
}
}

private Object convertNumericField(List<String> numericFields, String key, Object value) {
if(numericFields.contains(key) && value != null) {
try {
value = NumberFormat.getInstance().parse(value.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
}
return value;
}
}