[#233] [3rd-party appender] Gelf: ensure `short_message` is not empty + add extra fields (@vise890)

Gelf really dislikes empty `short_message`s:
http://docs.graylog.org/en/2.2/pages/gelf.html#gelf-payload-specification

If `short_message` is not set (or is an empty string), the message will not
appear in the logs altogether.
This commit is contained in:
Martino Visintin 2017-06-07 18:57:42 +01:00 committed by Peter Taoussanis
parent 68673fccd6
commit bf02914ebf
1 changed files with 27 additions and 6 deletions

View File

@ -33,6 +33,31 @@
(.transport transport))] (.transport transport))]
(GelfTransports/create config))) (GelfTransports/create config)))
(defn data->gelf-message
[data]
(let [{:keys [msg_ hostname_
level instant
context ?err ?ns-str
?file ?line]}
data
log-level (timbre-to-gelf-level level)
msg (or (and (not-empty (force msg_))
(force msg_))
(and ?err
(.getMessage ?err))
"EMPTY MSG")
msg-builder (-> (GelfMessageBuilder. msg (force hostname_))
(.level log-level)
(.timestamp (.getTime instant)))]
(cond-> msg-builder
?err (.fullMessage (timbre/stacktrace ?err {:stacktrace-fonts {}}))
context (.additionalField "context" context)
?ns-str (.additionalField "namespace" ?ns-str)
?file (.additionalField "file" ?file)
?line (.additionalField "line" ?line))
(.build msg-builder)))
(defn gelf-appender (defn gelf-appender
"Returns a Timbre appender that sends gelf messages to a remote host. Params: "Returns a Timbre appender that sends gelf messages to a remote host. Params:
`gelf-server` - IP address or hostname string of the remote logging server `gelf-server` - IP address or hostname string of the remote logging server
@ -49,9 +74,5 @@
:gelf-transport tranport :gelf-transport tranport
:fn :fn
(fn [data] (fn [data]
(let [{:keys [appender msg_ level hostname_]} data (.send (get-in data [:appender :gelf-transport])
gelf-transport (:gelf-transport appender) (data->gelf-message data)))})))
log-level (timbre-to-gelf-level level)
gelf-message (-> (GelfMessageBuilder. (force msg_) (force hostname_))
(.level log-level) .build)]
(.send gelf-transport gelf-message)))})))