Skip to content

Instantly share code, notes, and snippets.

@fluffywaffles
Forked from martinklepsch/logging.cljc
Created June 28, 2017 21:30
Show Gist options
  • Save fluffywaffles/35d67a07965fdabe9bf8c9fa3e740984 to your computer and use it in GitHub Desktop.
Save fluffywaffles/35d67a07965fdabe9bf8c9fa3e740984 to your computer and use it in GitHub Desktop.
simple Clojurescript logging using Google Closure logging tools
;; This previously was CLJX but has now been updated to use cljc. Thanks @caskolkm
;; https://gist.github.com/caskolkm/39d823f5bac7051d3062
(ns app.logging
(:refer-clojure :exclude [time])
(:require #?(:clj [clojure.tools.logging :as log]
:cljs [goog.log :as glog]))
#?(:cljs (:import goog.debug.Console)))
#?(:cljs
(def logger
(glog/getLogger "app")))
#?(:cljs
(def levels {:severe goog.debug.Logger.Level.SEVERE
:warning goog.debug.Logger.Level.WARNING
:info goog.debug.Logger.Level.INFO
:config goog.debug.Logger.Level.CONFIG
:fine goog.debug.Logger.Level.FINE
:finer goog.debug.Logger.Level.FINER
:finest goog.debug.Logger.Level.FINEST}))
#?(:cljs
(defn log-to-console! []
(.setCapturing (goog.debug.Console.) true)))
#?(:cljs
(defn set-level! [level]
(.setLevel logger (get levels level (:info levels)))))
(defn fmt [msgs]
(apply str (interpose " " (map pr-str msgs))))
(defn info [& s]
(let [msg (fmt s)]
#?(:clj (log/info msg)
:cljs (glog/info logger msg))))
(defn debug [& s]
(let [msg (fmt s)]
#?(:clj (log/debug msg)
:cljs (glog/fine logger msg))))
(defn error [& s]
(let [msg (fmt s)]
#?(:clj (log/error msg)
:cljs (glog/error logger msg))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment