Useful Clojure Asides - Logging and Exceptions

It's always useful to have some nice logging when writing code. Simple logging and exception handling samples to make it a bit easier for the developer.

Logging and exception handling are such core requirements in any application which are rarely, if ever, discussed. At least in the initial phases of any application. But as any developer would vouch (when asked, not voluntarily, I think), logging right is a very crucial aspect of any application. Not only does it help you understand the (then-) current state of the application, they also help folks trace causes of various problems when done right.

Leaving aside the generally broad topic of logging, and exceptions-handling, let’s look at a very simple setup one can use, especially in a new, nascent application written in Clojure.

A couple of small functions, placed in some utils namespace

  (ns msync-blog.utils
    (:require [taoensso.timbre :as timbre :refer [log info debug error warn
                                                  trace fatal report]]
              [clojure.string :as string]))

  ;; Modified slightly since contribs has now changed forms (as of 1.7)
  (defn unmangle
    "Given the name of a class that implements a Clojure function,
        returns the function's name in Clojure.
        Note: If the true Clojure function name contains any underscores
        (a rare occurrence), the unmangled name will contain hyphens
        at those locations instead."
     (string/replace class-name #"^(.+)\$(.+)__\d+$" "$1/$2")
     \_ \-))

  ;; And some minor modifications
  (defmacro try-catch [log-level & body]
       (catch Exception e#
          (timbre/color-str :red (str "Exception thrown trying to execute '"
                                      (-> e#
                                      "' - Message/exception: "