Skip to content

Instantly share code, notes, and snippets.

@johanatan
Created March 15, 2016 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanatan/2e6540e9e3e198bac875 to your computer and use it in GitHub Desktop.
Save johanatan/2e6540e9e3e198bac875 to your computer and use it in GitHub Desktop.
log-opts (bunyan helper)
(defn log-opts [logger level msg & params]
(let [amap (apply array-map params)
expected (count (re-seq #"%s|%d|%j" msg))
args (flatten [amap msg (if (not (empty? amap)) (take expected (vals amap)))])]
(.apply (aget logger level) logger (clj->js (remove nil? args)))))
@johanatan
Copy link
Author

It can be used like so:

(log-opts logger "info" "A static message." :some-key some-val :another-one another-val)
(log-opts logger "info" "Formatted message: %s" :a-key a-val)

There can be more key/value pairs provided than placeholders in the formatted message (extras are just dropped as format params but still provided as part of options object for Bunyan to produce JSON from).

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