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

Add static fields #57

Merged
merged 6 commits into from
Sep 19, 2015
Merged
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
36 changes: 27 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ default values:
<additionalField>requestId:_request_id</additionalField>
<includeFullMDC>true</includeFullMDC>
<fieldType>_request_id:long</fieldType>
<!--Facility is not officially supported in GELF anymore, but you can use staticAdditionalFields to do the same thing-->
<staticAdditionalField>_facility:GELF</staticAdditionalField>
<!--Facility is not officially supported in GELF anymore, but you can use staticFields to do the same thing-->
<staticField class="me.moocar.logbackgelf.Field">
<key>_facility</key>
<value>GELF</value>
</staticField>
</layout>
</encoder>
</appender>
Expand All @@ -92,6 +95,7 @@ default values:
</root>
</configuration>
```

## GelfLayout

`me.moocar.logbackgelf.GelfLayout`
Expand Down Expand Up @@ -126,9 +130,10 @@ actually converts a log event into a GELF compatible JSON string.
* **additionalFields**: See additional fields below. Default: empty
* **fieldType**: See field type conversion below. Default: empty
(fields sent as string)
* **staticAdditionalFields**: See static additional fields below.
Note, now that facility is deprecated, use this to set a facility
Default: empty
* **staticFields**: See static fields below. Note, now that facility
is deprecated, use this to set a facility Default: empty
* **staticAdditionalFields**: _deprecated_. Use staticFields. Default:
empty
* **includeFullMDC**: See additional fields below. Default: `false`

## Transports
Expand Down Expand Up @@ -247,24 +252,37 @@ If the property `includeFullMDC` is set to false (default value) then
only the keys listed as `additionalField` will be added to a gelf
message.

### Static Additional Fields
### Static Fields

Use static additional fields when you want to add a static key value
pair to every GELF message. Key is the additional field key (and
should thus begin with an underscore). The value is a static string.

Now that the GELF `facility` is deprecated, this is how you add a
static facility.
static facility. StaticFields replace staticAdditionalFields

E.g in the appender configuration:

```xml
<layout class="me.moocar.logbackgelf.GelfLayout">
<staticAdditionalField>_node_name:www013</staticAdditionalField>
<staticAdditionalField>_facility:GELF</staticAdditionalField>
<staticField class="me.moocar.logbackgelf.Field">
<key>_facility</key>
<value>GELF</value>
</staticField>
<staticField class="me.moocar.logbackgelf.Field">
<key>_node_name</key>
<value>www013</value>
</staticField>
</layout>
```

### Static Additional Fields (deprecated)

Static Additional fields have been deprecated and superceded by
staticFields. While they offered a more concise way of expressing the
key/value pair, it was impossible to include a colon in the value.
staticFields are fully structured and don't have this problem.

### Field type conversion

You can configure a specific field to be converted to a numeric type.
Expand Down
7 changes: 5 additions & 2 deletions sample.logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
<additionalField>requestId:_request_id</additionalField>
<includeFullMDC>true</includeFullMDC>
<fieldType>_request_id:long</fieldType>
<!--Facility is not officially supporte in GELF anymore, but you can use staticAdditionalFields to do the same thing-->
<staticAdditionalField>_facility:GELF</staticAdditionalField>
<!--Facility is not officially supported in GELF anymore, but you can use staticAdditionalFields to do the same thing-->
<staticField class="me.moocar.logbackgelf.Field">
<key>_facility</key>
<value>GELF</value>
</staticField>
</layout>
</encoder>
</appender>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/me/moocar/logbackgelf/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.moocar.logbackgelf;

public class Field {

private String key;
private String value;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
42 changes: 25 additions & 17 deletions src/main/java/me/moocar/logbackgelf/GelfLayout.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package me.moocar.logbackgelf;

import java.lang.reflect.Method;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.classic.util.LevelToSyslogSeverity;

import ch.qos.logback.core.Layout;
import ch.qos.logback.core.LayoutBase;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Method;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
* Responsible for formatting a log event into a GELF JSON string
*/
Expand All @@ -33,7 +31,7 @@ public class GelfLayout<E extends ILoggingEvent> extends LayoutBase<E> {
private boolean useMarker = false;
private Map<String, String> additionalFields = new HashMap<String, String>();
private Map<String, String> fieldTypes = new HashMap<String, String>();
private Map<String, String> staticAdditionalFields = new HashMap<String, String>();
private Map<String, String> staticFields = new HashMap<String, String>();
private String host = getLocalHostName();
private final Gson gson;
private Layout fullMessageLayout;
Expand Down Expand Up @@ -198,8 +196,8 @@ private boolean eventHasMarker(ILoggingEvent eventObject) {

private void staticAdditionalFields(Map<String,Object> map) {

for (String key : staticAdditionalFields.keySet()) {
map.put(key, (staticAdditionalFields.get(key)));
for (String key : staticFields.keySet()) {
map.put(key, (staticFields.get(key)));
}
}

Expand Down Expand Up @@ -268,12 +266,12 @@ public void setAdditionalFields(Map<String, String> additionalFields) {
* static additional fields to add to every gelf message. Key is the additional field key (and should thus begin
* with an underscore). The value is a static string.
*/
public Map<String, String> getStaticAdditionalFields() {
return staticAdditionalFields;
public Map<String, String> getStaticFields() {
return staticFields;
}

public void setStaticAdditionalFields(Map<String, String> staticAdditionalFields) {
this.staticAdditionalFields = staticAdditionalFields;
public void setStaticFields(Map<String, String> staticFields) {
this.staticFields = staticFields;
}

/**
Expand Down Expand Up @@ -337,6 +335,8 @@ public void addAdditionalField(String keyValue) {
*
* @param keyValue This must be in format key:value where key is the additional field key, and value is a static
* string. e.g "_node_name:www013"
*
* @deprecated Use addStaticField instead
*/
public void addStaticAdditionalField(String keyValue) {
String[] splitted = keyValue.split(":");
Expand All @@ -348,7 +348,15 @@ public void addStaticAdditionalField(String keyValue) {
"e.g. _node_name:www013");
}

staticAdditionalFields.put(splitted[0], splitted[1]);
staticFields.put(splitted[0], splitted[1]);
}

/**
* Add a static field. A static field is a key/value pair that should be sent in each Gelf message. This supercedes
* static additional fields, which can't have colon characters in their value.
*/
public void addStaticField(Field entry) {
staticFields.put(entry.getKey(), entry.getValue());
}

public void addFieldType(String keyValue) {
Expand Down
20 changes: 18 additions & 2 deletions src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@
[:additionalField "ipAddress:_ip_address"]
[:additionalField "requestId:_request_id"]
[:includeFullMDC (:include-full-mdc? config)]]
(map #(vector :staticAdditionalField (string/join ":" %))
(:static-additional-fields config))
(for [field (:static-additional-fields config)]
[:staticAdditionalField (string/join ":" field)])
(for [field (:static-fields config)]
[:staticField {:class "me.moocar.logbackgelf.Field"}
[:key (key field)]
[:value (val field)]])
(map #(vector :fieldType (string/join ":" %))
(:field-types config))))]]
[:root {:level "all"}
Expand Down Expand Up @@ -236,6 +240,17 @@
(let [json (wait msg-ch)]
(is (= "www013" (:_node_name json)))))))

(defn t-static-fields
[{:keys [config server] :as system}]
(let [msg-ch (:msg-ch server)
config (assoc config :static-fields {"foo" "bar"
"moo" "car"})]
(with-logger [logger config]
(.debug logger "my msg")
(let [json (wait msg-ch)]
(is (= "bar" (:foo json)))
(is (= "car" (:moo json)))))))

(defn t-undefined-hostname-string
"Ensure that when a bad remote host is included, that an
appropariate error is reported to the user. Note that I can't think
Expand Down Expand Up @@ -270,6 +285,7 @@
(t-substitute system)
(t-exception system)
(t-static-additional-field system)
(t-static-fields system)
(t-undefined-hostname-string system)
(is (= true (:result (tc/quick-check 100 (t-field-types system)))))
(is (= true (:result (tc/quick-check 100 (t-test system)))))))
Expand Down