Skip to content

Instantly share code, notes, and snippets.

@eval
Forked from caskolkm/logging.cljc
Created October 2, 2018 11:20
Show Gist options
  • Save eval/f1aaab653927d39ec549f14defb21785 to your computer and use it in GitHub Desktop.
Save eval/f1aaab653927d39ec549f14defb21785 to your computer and use it in GitHub Desktop.
Simple Clojurescript logging using Google Closure logging tools, now in cljc :)

Usage

Log shows with optimization-level :none...

# optimization none
$ clj -m cljs.main -v -O none -t node -c logging
$ node out/main.js
-main: via println
 [  0.001s] [app] "some" "bad" "stuff"

...but not with level :simple

# optimization simple
$ clj -m cljs.main -v -O simple -t node -c logging
$ node out/main.js
-main: via println
{:paths
["."]
:deps
{org.clojure/clojurescript {:mvn/version "1.10.339"}}}
(ns logging
(:refer-clojure :exclude [time])
(:require [goog.log :as glog])
(:import goog.debug.Console))
(def logger
(glog/getLogger "app"))
(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})
(defn log-to-console! []
(.setCapturing (goog.debug.Console.) true))
(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)]
(glog/info logger msg)))
(defn debug [& s]
(let [msg (fmt s)]
(glog/fine logger msg)))
(defn error [& s]
(let [msg (fmt s)]
(glog/error logger msg)))
(defn -main []
(println "-main: via println")
(log-to-console!)
(set-level! :finest)
(error "some" "bad" "stuff"))
(set! *main-cli-fn* -main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment