Fork me on GitHub
#observability
<
2020-06-17
>
jumar03:06:36

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

seancorfield04:06:33

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!

jumar06:06:35

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

seancorfield06:06:41

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."
  [ctx]
  (if (map? ctx)
    (reduce-kv (fn [m k v]
                 (assoc m
                        (if (keyword? k) (name k) (str k))
                        (pr-str v)))
               {}
               ctx)
    {"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))]
     ~@body))

👍 6
seancorfield06:06:13

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

sparkofreason20:06:04

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.

noisesmith20:06:37

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 https://cambium-clojure.github.io/

noisesmith20:06:55

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

sparkofreason20:06:36

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.

noisesmith20:06:33

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

sparkofreason20:06:41

Thanks, this might do it, especially in conjunction with https://github.com/lambdaisland/regal.