Skip to content

Instantly share code, notes, and snippets.

@ptaoussanis
Last active March 11, 2025 06:47
Show Gist options
  • Save ptaoussanis/f8a80f85d3e0f89b307a470ce6e044b5 to your computer and use it in GitHub Desktop.
Save ptaoussanis/f8a80f85d3e0f89b307a470ce6e044b5 to your computer and use it in GitHub Desktop.
Example of using Telemere with Bling (rich text console printing lib for Clj/s+)
;; Example created: 2024-12-23
;; Last updated: 2025-03-11 by @paintparty
;; Dependencies:
;; Telemere - Ref. https://github.com/taoensso/telemere v1.0.0-RC5 (2025-03-10)
;; Bling - Ref. https://github.com/paintparty/bling v0.5.2 (2025-03-11)
;; Platform: Clj only for now (haven't tried yet with Cljs)
;; Improvements very welcome!
;; Original context: https://github.com/taoensso/telemere/issues/33
(require '[taoensso.telemere :as tel])
(require '[taoensso.telemere.utils :as tel-utils])
(require '[bling.core :as bling])
(defn- signal->callout-opts
"Returns {:keys [type label data?]} for use with `bling/callout`, etc."
[{:keys [level]}]
(let [label (tel-utils/format-level level)
type
(case level
(:report :info) :info
(:fatal :error) :error
(:warn) :warning
(do nil))
colorway
(case level
(:trace :debug) :subtle
(do :neutral))]
{:type type,
:colorway colorway
:label label,
:data? true,
:margin-top 0,
:margin-bottom 1}))
(tel/add-handler! :my-bling-handler
(let [;; Set your preferred formatting options below as usual:
format-signal-fn (tel/format-signal-fn {:incl-newline? false})
my-output-fn
(fn [signal]
(let [norm-output (format-signal-fn signal)] ; Normal Telemere output
(bling.core/callout ; Add Bling "callout" formatting
(signal->callout-opts signal)
norm-output)))]
(tel/handler:console {:output-fn my-output-fn})))
@ptaoussanis
Copy link
Author

Note also https://github.com/paintparty/fireworks from the same author as Bling ๐Ÿ‘

@paintparty
Copy link

paintparty commented Mar 10, 2025

Thanks for the fireworks shoutout!

I just released a new version of Bling with some minor breaking changes. I realized the interface to bling.core/callout around the :type option needed some decomplecting and there is now a distinct :colorway option that can be used if you want a colored callout that is not of type :error :warning or :info(which all add labels to the top of the callout by default).

I think the signal->callout-opts function in the above gist should be changed to:

(defn- signal->callout-opts
  "Returns {:keys [type label data?]} for use with `bling/callout`, etc."
  [{:keys [level]}]
  (let [label (tel-utils/format-level level)
        type
        (case level
          (:report :info) :info
          (:fatal :error) :error
          (:warn)         :warning
          (do             nil))
        colorway
        (case level
          (:trace :debug) :subtle
          (do             :neutral))]

    {:type type,
     :colorway colorway
     :label label,
     :data? true,
     :margin-top 0,
     :margin-bottom 1}))

I just tested this on my side with Telemere in a .clj file and it seemed to work. You might want to check it on your side as this test was my first time using Telemere, so I'm not super familiar with it yet.

I did notice that I got no printed ouput when I set the :level to :debug like so:
(tel/log! {:level :debug, :data {:foo :bar}} "Hello again!")

Also tried it in a repl, just telemere without the handler or anything:

foo.core=> (require '[taoensso.telemere :as tel])
nil
foo.core=> (tel/log! {:level :info, :data {:a :foo}} "Hello again!")
2025-03-10T22:35:52.337197Z INFO LOG mycomputer.local foo.core[1,1] Hello again!
   data: {:a :foo}
nil
foo.core=> (tel/log! {:level :debug, :data {:a :foo}} "Hello again!")
nil
foo.core=>

Is that the expected behavior?

Thank you!

@ptaoussanis
Copy link
Author

I just released a new version of Bling with some minor breaking changes. [...]

Sounds good ๐Ÿ‘

I think the signal->callout-opts function in the above gist should be changed to:

Updated, thanks!

I did notice that I got no printed ouput when I set the :level to :debug like so [...] Is that the expected behavior?

Yes, that's expected since the default minimum level is :info. One can decrease the minimum level with set-min-level!, etc.

Thanks for checking on this, and for the updated gist - cheers! :-)

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