mirror of
https://github.com/status-im/timbre.git
synced 2025-02-16 13:46:22 +00:00
Add socket appender
This commit is contained in:
parent
1039912094
commit
6dac0ec03e
16
README.md
16
README.md
@ -154,6 +154,18 @@ Filter logging output by namespaces:
|
||||
:chan "#logs"})
|
||||
```
|
||||
|
||||
#### Socket ([server-socket](https://github.com/technomancy/server-socket)) appender
|
||||
|
||||
```clojure
|
||||
;; [server-socket "1.0.0"] ; Add to project.clj dependencies
|
||||
;; (:require [taoensso.timbre.appenders (socket :as socket-appender)]) ; Add to ns
|
||||
|
||||
(timbre/set-config! [:appenders :socket] socket-appender/socket-appender)
|
||||
(timbre/set-config! [:shared-appender-config :socket]
|
||||
{:list-addr "127.0.0.1"
|
||||
:port 9000})
|
||||
```
|
||||
|
||||
### Custom appenders
|
||||
|
||||
Writing a custom appender is dead-easy:
|
||||
@ -179,7 +191,7 @@ See the `timbre/config` docstring for more information on appenders.
|
||||
|
||||
The usual recommendation for Clojure profiling is: use a good **JVM profiler** like [YourKit](http://www.yourkit.com/), [JProfiler](http://www.ej-technologies.com/products/jprofiler/overview.html), or [VisualVM](http://docs.oracle.com/javase/6/docs/technotes/guides/visualvm/index.html).
|
||||
|
||||
And these certainly do the job. But as with many Java tools, they can be a little hairy and often heavy-handed - especially when applied to Clojure. Timbre includes an alternative.
|
||||
And these certainly do the job. But as with many Java tools, they can be a little hairy and often heavy-handed - especially when applied to Clojure. Timbre includes an alternative.
|
||||
|
||||
Let's add it to our app's `ns` declaration:
|
||||
|
||||
@ -246,4 +258,4 @@ Otherwise reach me (Peter Taoussanis) at [taoensso.com](https://www.taoensso.com
|
||||
|
||||
## License
|
||||
|
||||
Copyright © 2012, 2013 Peter Taoussanis. Distributed under the [Eclipse Public License](http://www.eclipse.org/legal/epl-v10.html), the same as Clojure.
|
||||
Copyright © 2012, 2013 Peter Taoussanis. Distributed under the [Eclipse Public License](http://www.eclipse.org/legal/epl-v10.html), the same as Clojure.
|
||||
|
40
src/taoensso/timbre/appenders/socket.clj
Normal file
40
src/taoensso/timbre/appenders/socket.clj
Normal file
@ -0,0 +1,40 @@
|
||||
(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]])
|
||||
(:import [java.net Socket InetAddress]
|
||||
[java.io BufferedReader InputStreamReader PrintWriter]))
|
||||
|
||||
(def conn (atom nil))
|
||||
|
||||
(defn listener-fun [in out]
|
||||
(loop [lines (-> in
|
||||
(InputStreamReader.)
|
||||
(BufferedReader.)
|
||||
(line-seq))]
|
||||
(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 ensure-conn [socket-config]
|
||||
(swap! conn #(or % (connect socket-config))))
|
||||
|
||||
(defn appender-fn [{:keys [ap-config prefix message] :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)))))))
|
||||
|
||||
(def socket-appender
|
||||
{:doc (str "Logs to a listening socket.\n"
|
||||
"Needs :socket config map in :shared-appender-config, e.g.:
|
||||
{:listen-addr nil
|
||||
:port 9000}")
|
||||
:min-level :trace :enabled? true :async? false
|
||||
:max-message-per-msecs nil ; no rate limit by default
|
||||
:fn appender-fn})
|
Loading…
x
Reference in New Issue
Block a user