Skip to content

Instantly share code, notes, and snippets.

@serioga
Created February 23, 2019 19:18
Show Gist options
  • Save serioga/6398de402ced7b541daede422f61f23e to your computer and use it in GitHub Desktop.
Save serioga/6398de402ced7b541daede422f61f23e to your computer and use it in GitHub Desktop.
Simple logging to console in ClojureScript
(ns app.lib.util.cljs-logging
#?(:cljs
(:require-macros app.lib.util.cljs-logging)))
#?(:clj (set! *warn-on-reflection* true) :cljs (set! *warn-on-infer* true))
#?(:cljs
(do
(def console-fns
{:log js/console.log
:debug js/console.debug
:error js/console.error
:info js/console.info
:warn js/console.warn})
(defn format-arg
[arg]
(cond
(var? arg) (str arg)
:default (clj->js arg)))
(defn console-log
[level prefix & args]
(when-let [f (or (console-fns level)
js/console.log
println)]
(apply f (map format-arg (conj args prefix)))))))
#?(:clj
(do
(defmacro logp
[level & more]
(let [{:keys [ns line]} &env
prefix (str "[" (:name ns) ":" line "] -")]
`(~'app.lib.util.cljs-logging/console-log ~level ~prefix ~@more)))
(defmacro log
[& more]
`(logp :log ~@more))
(defmacro debug
[& more]
`(logp :debug ~@more))
(defmacro error
[& more]
`(logp :error ~@more))
(defmacro info
[& more]
`(logp :info ~@more))
(defmacro warn
[& more]
`(logp :warn ~@more))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment