Skip to content

Instantly share code, notes, and snippets.

@pikolinianita
Last active April 10, 2021 22:55
Show Gist options
  • Save pikolinianita/99a62d740a775b5d229fb99acb67fd49 to your computer and use it in GitHub Desktop.
Save pikolinianita/99a62d740a775b5d229fb99acb67fd49 to your computer and use it in GitHub Desktop.
Clojure Logging
(ns log-first.core
(:require [clojure.java.io :as io]
[taoensso.timbre :as timbre]
[taoensso.timbre.appenders.core :as appenders])
(:gen-class))
;And into project.cli
; :dependencies [[org.clojure/clojure "1.10.1"]
; [com.taoensso/timbre "5.1.2"]]
(timbre/refer-timbre) ; set up timbre aliases
;; Disable logging to the console in timbre v4.0.0+:
(timbre/merge-config! {:appenders {:println {:enabled? false}}})
;; Create a "spit to file" appender in timbre v4.0.0+:
(timbre/merge-config! {:appenders {:spit (appenders/spit-appender {:fname "log.txt"})}})
; Set the lowest-level to output as :debug
(timbre/set-level! :debug)
; Demonstrate logging with Timbre
(trace "Hell, Timbre! trace") ; will not be logged, below current log-level
(debug "Hell, Timbre! debug")
(info "Hell, Timbre! info")
(warn "Hell, Timbre! warn")
(error "Hell, Timbre! error")
(fatal "Hell, Timbre! fatal")
; Demonstrate 3 arities of spy
(info "Arg-1")
(info "Arg-1" :Arg-2)
(info "Arg-1" :Arg-2 ["Arg-3"] )
(info "Arg-1" :Arg-2 ["Arg-3"] {:Arg 4} )
; Demonstrate 3 arities of spy
(assert (= {:a 1} (spy :info "Spy returns the last value" {:a 1} )))
(assert (= 42 (spy (* 6 7) ))) ; no level implies :debug
(assert (= 42 (spy :warn (* 6 7))))
(assert (= {:a 1} (spy :error "optional message" {:a 1} )))
; Even exceptions look nice in the logs
(error (Exception. "Doh!") "Any extra" :items {:go "here"} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment