Make threads daemons to not block shutdown, add stacktrace, default to localhost only but allow :all

This commit is contained in:
Emlyn Corrin 2013-06-05 16:45:59 +01:00
parent e5852a6676
commit e7bf5015e9
1 changed files with 16 additions and 7 deletions

View File

@ -1,7 +1,8 @@
(ns taoensso.timbre.appenders.socket
"TCP Socket appender. Depends on https://github.com/technomancy/server-socket."
{:author "Emlyn Corrin"}
(:require [server.socket :refer [create-server]])
(:require [server.socket :refer [create-server]]
[taoensso.timbre :refer [stacktrace]])
(:import [java.net Socket InetAddress]
[java.io BufferedReader InputStreamReader PrintWriter]))
@ -15,25 +16,33 @@
(when-not (re-find #"(?i)^quit" (first lines))
(recur (rest lines)))))
(defn conect [{:keys [port listen-addr]}]
(let [addr (when listen-addr (InetAddress/getByName listen-addr))]
(create-server port listener-fun 0 ^InetAddress addr)))
(defn on-thread-daemon [f]
(doto (Thread. ^Runnable f)
(.setDaemon true)
(.start)))
(defn connect [{:keys [port listen-addr]}]
(let [addr (when (not= :all listen-addr)
(InetAddress/getByName listen-addr))]
(with-redefs [server.socket/on-thread on-thread-daemon]
(create-server port listener-fun 0 ^InetAddress addr))))
(defn ensure-conn [socket-config]
(swap! conn #(or % (connect socket-config))))
(defn appender-fn [{:keys [ap-config prefix message] :as params}]
(defn appender-fn [{:keys [ap-config prefix message throwable] :as params}]
(when-let [socket-config (:socket ap-config)]
(let [c (ensure-conn socket-config)]
(doseq [sock @(:connections c)]
(let [out (PrintWriter. (.getOutputStream ^Socket sock))]
(binding [*out* out]
(println prefix message)))))))
(println prefix message
(stacktrace throwable))))))))
(def socket-appender
{:doc (str "Logs to a listening socket.\n"
"Needs :socket config map in :shared-appender-config, e.g.:
{:listen-addr nil
{:listen-addr :all
:port 9000}")
:min-level :trace :enabled? true :async? false
:max-message-per-msecs nil ; no rate limit by default