Skip to content

Instantly share code, notes, and snippets.

@malcolmsparks
Created November 17, 2022 15:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcolmsparks/19310dba9293715856700cea79ebd2c8 to your computer and use it in GitHub Desktop.
Save malcolmsparks/19310dba9293715856700cea79ebd2c8 to your computer and use it in GitHub Desktop.

If you're using clojure.tools.logging with SLF4J.

It's often useful to 'switch on' logging for a given web request, or during the evaluation of a given form, etc.

Add this to your logback.xml configuration:

  <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
    <MDCKey>logging</MDCKey>
    <Value>on</Value>
    <OnMatch>ACCEPT</OnMatch>
  </turboFilter>

Add the following Clojure macro:

(defmacro with-logging [& body]
  `(try
    (org.slf4j.MDC/put "logging" "on")
    ~@body
    (finally
      (org.slf4j.MDC/remove "logging"))))

Now you can do this:

(with-logging
  (log/trace "hello"))

This allows you to turn down logging (e.g. to INFO), but be able to log tracing messages for a particular context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment