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