taoensso / timbre

Pure Clojure/Script logging library
https://www.taoensso.com/timbre
Eclipse Public License 1.0
1.44k stars 171 forks source link

custom appender no longer printing stacktraces #291

Closed torkus closed 3 years ago

torkus commented 5 years ago

I'm trying to create a more-anonymous log line that drops the hostname information but have now lost the ability to print stacktraces when an exception object is the first argument. I think I may be over-complicating things. This is what I have:

(defn anon-println-appender
  "removes the hostname from the output format string"
  [data]
  (let [{:keys [timestamp_ msg_ level ?ns-str ?line]} data]
    ;; looks like: "2019-03-10 02:17:22.372 :info [foo.bar:89] downloading summary data for ..." 
    (println (format "%s %s [%s:%s] %s"
                     (force timestamp_) level (force ?ns-str) (force ?line) (force msg_)))))

(def default-logging-config
  {:level :info
   :timestamp-opts {:pattern "yyyy-MM-dd HH:mm:ss.SSS"}
   ;;:output-fn (anon-println-appender) ;; doesn't work
   :appenders {:println {:enabled? true
                         :async? false
                         :output-fn :inherit
                         :fn anon-println-appender}}})

(merge-config! default-logging-config)

=> (error (RuntimeException. "kaboom") "error message")
2019-06-24 14:10:49.281 :error [foo.bar:1] error message
nil

All I want to do is drop the hostname field and give it a sane timestamp but it's getting crazy.

Any insight on how I can do this properly? Thank you

xificurC commented 4 years ago

Using a middleware should work. This is untested:

{:level :info 
 :middleware [(fn [d] (assoc d :hostname_ (delay "")))]
}
ptaoussanis commented 3 years ago

@xificurC Thanks Peter!

torkus commented 3 years ago

All I want to do is drop the hostname field and give it a sane timestamp but it's getting crazy.

It's been a little while but I've returned to this problem and have figured out how to print the stacktrace like the native println-appender does.

(defn anon-println-appender
  "removes the hostname from the output format string"
  [data]
  (let [{:keys [?err timestamp_ msg_ level ?ns-str ?line]} data]
    (when ?err
      (println (timbre/stacktrace ?err)))
    (println (format "%s %s [%s:%s] %s"
                     (force timestamp_) level (force ?ns-str) (force ?line) (force msg_)))))