Fork me on GitHub

Thanks everyone for your comments! I remember we were using MDC years ago when I work solely with Java.


This discussion encouraged me to add a with-log-context macro to our code base today. It takes a context (usually) a hash map, and a body to be executed and stringizes the context (into a CloseableThreadContext). Very useful! Thank you all again for the nudge!


I guess it's pretty basic but would you mind sharing the macro?


Here's what I added

(defn- stringize-context
  "Given a logging context (that is hopefully a hash map), return a
  hash map with strings for keys and for values. If the context is
  not a hash map, return a hash map with a ctx string key and the
  context value as a string."
  (if (map? ctx)
    (reduce-kv (fn [m k v]
                 (assoc m
                        (if (keyword? k) (name k) (str k))
                        (pr-str v)))
    {"ctx" (pr-str ctx)}))

(defmacro with-log-context
  "Given a hash map and a body, add the hash map to the log4j2 mapped
  diagnostic context, and execute the body."
  [ctx & body]
  `(with-open [_# (CloseableThreadContext/putAll (stringize-context ~ctx))]

👍 6

Assuming :import of (org.apache.logging.log4j CloseableThreadContext)


Any recommendations on parsing log file data? Looking to write a little utility to stream in logs from a kubernetes cluster and performe Clojuresque voodoo upon the data.


I think the best answer is to use logging that's meant to be machine readable, my company has had good luck with cambium json logging


oh - the logs aren't from clojure originally...


Right. In fact, they're from a variety of sources in a variety of formats, now that I look at them, so I don't know if I'll find something out of the box to cover the spectrum. But maybe something as an example to get started would be helpful.


I guess in theory the spectrum goes from regex to instaparse(?) -- google shows me a few libs but I can't tell you if any are better than others


Thanks, this might do it, especially in conjunction with