Clojure Power Tools Part 2

Recording with Clojure.


This is the second part of my Clojure Power Tools series (I’m a bit interested myself how many blog posts I will write to this series). If you haven’t read the first part I recommend you to read it first: Clojure Power Tools Part 1. In this second blog article, I list a couple of new power tool tricks for debugging. I’ll first introduce a poor man’s debug repl, and then the real debug repl.

Poor Man’s Debug Repl

I was refactoring some Clojure tests for the second version of a complicated application. There were quite a few structural changes in the application logic, and the domain was rather complicated comprising a lot of various recursive data structures. In some tests, I was quite puzzled about what kind of state there was in several places of the application. The tests were also rather complicated and I wanted a simple way to record certain bindings in several places of the application when I was running a certain area of the test — and only that area. I quickly implemented a poor man’s debug recorder:

The idea is to provide a way to add stuff to the value map with unique keys — but only when I’m recording.

(I’m using my own exercises as an example here, the data here is ridiculously simple — no need for a recorder here, but I guess you get my point if you change the data to several hundreds of lines of recursive maps and vectors and complex business logic…)

So, let’s add the start/stop recording to the test we are interested:

See the (re/start!) and (re/stop!) commands in the test - we are recording only during that time.

Then I can add stuff to my recorder where-ever I want:

I.e. (re/add-value-if-recording :valid-token-basic basic) .

Run the tests.

After the tests I can examine the recorder in my scratch file:

You can also use the excellent portal visualization tool to examine your complicated data you recorded (well, in this example, not so complicated):

Real Debug-Repl

The example above was a simple trick but a real Clojurian stops the world if he is interested to see what is happening in a particular moment of time instead of just recording it. We are going to use Gary Fredericks’s debug-repl library for it. You need the dependency — I have these tool dependencies in my ~/.clojure/deps.edn file:

Then we need to be able to start the backend REPL with this debug repl middleware, I have this in my Justfile as a Just recipe which I use to start my backend repl with various options:

Then you can add a debug-repl breakpoint in your code, run your code, stop the world and examine the snapshot context of your world just before the breakpoint. Let’s see this in action:

I have added _ (break! "Yihaa!") breakpoint in one of the tests. Then I run the test and the world stops at the breakpoint (Hijacking repl for breakpoint: Yihaa! output in the REPL output window). Then I have moved the cursor in various let-bindings: json-web-token, params and get-ret and evaluated the forms: you can see the evaluated values in the REPL output window on the right. If I try to evaluate status I get an error “Unable to resolve symbol: status in this context” - of course, because it’s outside the context.


There are a lot of other Clojure tricks and tools — maybe I’ll write “Clojure Power Tools Part 3” blog post in the future.

The writer is working at Metosin using Clojure in cloud projects. If you are interested to start a Clojure project in Finland or you are interested to get Clojure training in Finland you can contact me by sending email to my Metosin email address or contact me via LinkedIn.

Kari Marttila

The article was first published in

I’m a Software architect and developer. Currently implementing systems on AWS / GCP / Azure / Docker / Kubernetes using Java, Python, Go and Clojure.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store