From 2825f090b7d09cddf851ac838f7193835d758da6 Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 7 Jul 2015 21:34:34 -0700 Subject: [PATCH 1/6] typo --- sample.logback.xml | 2 +- src/main/java/me/moocar/logbackgelf/Field.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/moocar/logbackgelf/Field.java diff --git a/sample.logback.xml b/sample.logback.xml index 988eafa..a7e9999 100644 --- a/sample.logback.xml +++ b/sample.logback.xml @@ -21,7 +21,7 @@ requestId:_request_id true _request_id:long - + _facility:GELF diff --git a/src/main/java/me/moocar/logbackgelf/Field.java b/src/main/java/me/moocar/logbackgelf/Field.java new file mode 100644 index 0000000..0a179f9 --- /dev/null +++ b/src/main/java/me/moocar/logbackgelf/Field.java @@ -0,0 +1,7 @@ +package me.moocar.logbackgelf; + +/** + * Created by anthony on 7/7/15. + */ +public class Field { +} From 14c504431faaa225eb451230730c49411be4a048 Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 7 Jul 2015 21:53:05 -0700 Subject: [PATCH 2/6] added staticField functionality to GelfLayout --- .../java/me/moocar/logbackgelf/Field.java | 22 ++++++++-- .../me/moocar/logbackgelf/GelfLayout.java | 42 +++++++++++-------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/main/java/me/moocar/logbackgelf/Field.java b/src/main/java/me/moocar/logbackgelf/Field.java index 0a179f9..559e002 100644 --- a/src/main/java/me/moocar/logbackgelf/Field.java +++ b/src/main/java/me/moocar/logbackgelf/Field.java @@ -1,7 +1,23 @@ package me.moocar.logbackgelf; -/** - * Created by anthony on 7/7/15. - */ 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; + } } diff --git a/src/main/java/me/moocar/logbackgelf/GelfLayout.java b/src/main/java/me/moocar/logbackgelf/GelfLayout.java index 5b015d6..f4aac39 100644 --- a/src/main/java/me/moocar/logbackgelf/GelfLayout.java +++ b/src/main/java/me/moocar/logbackgelf/GelfLayout.java @@ -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 */ @@ -33,7 +31,7 @@ public class GelfLayout extends LayoutBase { private boolean useMarker = false; private Map additionalFields = new HashMap(); private Map fieldTypes = new HashMap(); - private Map staticAdditionalFields = new HashMap(); + private Map staticFields = new HashMap(); private String host = getLocalHostName(); private final Gson gson; private Layout fullMessageLayout; @@ -198,8 +196,8 @@ private boolean eventHasMarker(ILoggingEvent eventObject) { private void staticAdditionalFields(Map map) { - for (String key : staticAdditionalFields.keySet()) { - map.put(key, (staticAdditionalFields.get(key))); + for (String key : staticFields.keySet()) { + map.put(key, (staticFields.get(key))); } } @@ -268,12 +266,12 @@ public void setAdditionalFields(Map 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 getStaticAdditionalFields() { - return staticAdditionalFields; + public Map getStaticFields() { + return staticFields; } - public void setStaticAdditionalFields(Map staticAdditionalFields) { - this.staticAdditionalFields = staticAdditionalFields; + public void setStaticFields(Map staticFields) { + this.staticFields = staticFields; } /** @@ -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(":"); @@ -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) { From 7fe50788612b3b4f4a0d8834672f1e7af6ac0e77 Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 7 Jul 2015 21:53:19 -0700 Subject: [PATCH 3/6] added test for staticField --- .../me/moocar/logbackgelf/end_to_end_test.clj | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj b/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj index abd54fb..f2e6fd0 100644 --- a/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj +++ b/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj @@ -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"} @@ -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 @@ -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))))))) @@ -285,4 +301,7 @@ (let [logger (LoggerFactory/getLogger "this_logger")] (dotimes [_ 10] (future (.debug logger (string/join (repeatedly (* 512 5) #(rand-nth "abcdefghijklmnopqrstuvwxyz"))) #_(ex-info "ERROR ME TIMBER" {})))) - #_(.debug logger (string/join (repeatedly 30 #(rand-nth "abcdefghijklmnopqrstuvwxyz"))) #_(ex-info "ERROR ME TIMBER" {}))))) + (.debug logger (string/join (repeatedly 30 #(rand-nth "abcdefghijklmnopqrstuvwxyz"))) #_(ex-info "ERROR ME TIMBER" {}))))) + +(send-request) +;; (require 'me.moocar.logbackgelf.end-to-end-test) From fe7bb55d3617c4a01e8f8e53ab45d1d5c21ab7ad Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 7 Jul 2015 21:54:07 -0700 Subject: [PATCH 4/6] added docs for static fields --- README.md | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5c24d66..a867d18 100644 --- a/README.md +++ b/README.md @@ -81,8 +81,11 @@ default values: requestId:_request_id true _request_id:long - - _facility:GELF + + + _facility + GELF + @@ -92,6 +95,7 @@ default values: ``` + ## GelfLayout `me.moocar.logbackgelf.GelfLayout` @@ -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 @@ -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 - _node_name:www013 - _facility:GELF + + _facility + GELF + + + _node_name + www013 + ``` +### 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. From ae663d1b5e4156e65af57149b2647c2e37f8307d Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 7 Jul 2015 21:55:34 -0700 Subject: [PATCH 5/6] updated sample logback.xml --- sample.logback.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sample.logback.xml b/sample.logback.xml index a7e9999..d80316c 100644 --- a/sample.logback.xml +++ b/sample.logback.xml @@ -22,7 +22,10 @@ true _request_id:long - _facility:GELF + + _facility + GELF + From 2e6949276a2cdcc31f6e1b2d5fbc61e2d6c9c84e Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 7 Jul 2015 22:03:31 -0700 Subject: [PATCH 6/6] recomment out send request --- src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj b/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj index f2e6fd0..fc6824c 100644 --- a/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj +++ b/src/test/clojure/me/moocar/logbackgelf/end_to_end_test.clj @@ -301,7 +301,4 @@ (let [logger (LoggerFactory/getLogger "this_logger")] (dotimes [_ 10] (future (.debug logger (string/join (repeatedly (* 512 5) #(rand-nth "abcdefghijklmnopqrstuvwxyz"))) #_(ex-info "ERROR ME TIMBER" {})))) - (.debug logger (string/join (repeatedly 30 #(rand-nth "abcdefghijklmnopqrstuvwxyz"))) #_(ex-info "ERROR ME TIMBER" {}))))) - -(send-request) -;; (require 'me.moocar.logbackgelf.end-to-end-test) + #_(.debug logger (string/join (repeatedly 30 #(rand-nth "abcdefghijklmnopqrstuvwxyz"))) #_(ex-info "ERROR ME TIMBER" {})))))