[#195] Logstash appender: add `:flush?` option (@tvanhens)

Setting the value to true will eagerly flush the tcp writer. Default
behavior is for this to be false.
This commit is contained in:
Tyler van Hensbergen 2016-09-15 12:40:42 -06:00 committed by Peter Taoussanis
parent ef8592f27d
commit e9c5dc0479
1 changed files with 10 additions and 7 deletions

View File

@ -50,10 +50,12 @@
format to the logstash server at `host:port`. Additionally `opts` format to the logstash server at `host:port`. Additionally `opts`
may be a map with `:pr-stracktrace` mapped to a function taking an may be a map with `:pr-stracktrace` mapped to a function taking an
exception, which should write the stacktrace of that exception to exception, which should write the stacktrace of that exception to
`*out`." `*out`. Set `:flush?` to true to flush the writer after every
event."
[host port & [opts]] [host port & [opts]]
(let [conn (atom nil) (let [conn (atom nil)
nl "\n"] flush? (or (:flush? opts) false)
nl "\n"]
{:enabled? true {:enabled? true
:async? false :async? false
:min-level nil :min-level nil
@ -63,12 +65,13 @@
(fn [data] (fn [data]
(try (try
(let [[sock out] (swap! conn (let [[sock out] (swap! conn
(fn [conn] (fn [conn]
(or (and conn (connection-ok? conn) conn) (or (and conn (connection-ok? conn) conn)
(connect host port))))] (connect host port))))]
(locking sock (locking sock
(data->json-stream data out (select-keys opts [:pr-stacktrace])) (data->json-stream data out (select-keys opts [:pr-stacktrace]))
;; logstash tcp input plugin: "each event is assumed to be one line of text". ;; logstash tcp input plugin: "each event is assumed to be one line of text".
(.write ^java.io.Writer out nl))) (.write ^java.io.Writer out nl)
(when flush? (.flush ^java.io.Writer out))))
(catch java.io.IOException _ (catch java.io.IOException _
nil)))})) nil)))}))