Skip to content

Commit

Permalink
Refactor console logger, incl. Ref from #132
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Feb 25, 2016
1 parent 79c9586 commit 4a2f1dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/taoensso/timbre.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@
You can modify default options with `(partial default-output-fn <opts-map>)`."
([data] (default-output-fn nil data))
([{:keys [no-stacktrace? stacktrace-fonts] :as opts} data]
(let [{:keys [level ?err_ vargs_ msg_ ?ns-str hostname_ timestamp_]} data]
(let [{:keys [level ?err_ vargs_ msg_ ?ns-str hostname_
timestamp_ ?line]} data]
(str
#+clj @timestamp_ #+clj " "
#+clj @hostname_ #+clj " "
(str/upper-case (name level)) " "
"[" (or ?ns-str "?ns") "] - "
@msg_
"[" (or ?ns-str "?") ":" (or ?line "?") "] - "
(force msg_)
(when-not no-stacktrace?
(when-let [err @?err_]
(when-let [err (force ?err_)]
(str "\n" (stacktrace err opts))))))))

;;; Alias core appenders here for user convenience
Expand Down
55 changes: 32 additions & 23 deletions src/taoensso/timbre/appenders/core.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,41 @@

#+cljs
(defn console-?appender
"Returns a simple js/console appender for ClojureScript, or nil if no
js/console exists."
[]
(when-let [have-logger? (and (exists? js/console) (.-log js/console))]
(let [have-warn-logger? (.-warn js/console)
have-error-logger? (.-error js/console)
level->logger {:fatal (if have-error-logger? :error :info)
:error (if have-error-logger? :error :info)
:warn (if have-warn-logger? :warn :info)}]
{:enabled? true
:async? false
:min-level nil
:rate-limit nil
:output-fn :inherit
:fn
"Returns a simple js/console appender for ClojureScript. For accurate
line numbers in Chrome, Ref. https://goo.gl/ZejSvR"
[& [{:keys [raw-output?]} ; Undocumented (experimental)
]]
{:enabled? true
:async? false
:min-level nil
:rate-limit nil
:output-fn :inherit
:fn
(if (and (exists? js/console) js/console.log)
(let [level->logger
{:trace (or js/console.trace js/console.log)
:debug (or js/console.debug js/console.log)
:info (or js/console.info js/console.log)
:warn (or js/console.warn js/console.log)
:error (or js/console.error js/console.log)
:fatal (or js/console.error js/console.log)
:report (or js/console.info js/console.log)}]

(fn [data]
(let [{:keys [level output-fn vargs_]} data
vargs @vargs_
[v1 vnext] (enc/vsplit-first vargs)
output (if (= v1 :timbre/raw)
(into-array vnext)
(output-fn data))]

(case (level->logger level)
:error (.error js/console output)
:warn (.warn js/console output)
(.log js/console output))))})))
logger (level->logger level js/console.log)]

(if (or raw-output? (= v1 :timbre/raw)) ; Undocumented
(let [output (output-fn (merge data {:msg_ (delay "")
:?err_ (delay nil)}))
;; [<output> <raw-error> <raw-arg1> <raw-arg2> ...]:
args (->> vnext (cons @(:?err_ data)) (cons output))]

(.apply logger js/console (into-array args)))
(.call logger js/console (output-fn data))))))

(fn [data] nil))})

(comment (console-?appender))

0 comments on commit 4a2f1dc

Please sign in to comment.