Debugging live Kotlin applications with Lightrun
  • 01-Nov-2021
Author Lightrun Marketing
Debugging live Kotlin applications with Lightrun

Debugging Live Kotlin Applications with Lightrun

Lightrun Marketing

Kotlin is a rising star, it’s got the power of Java coupled with cool new syntax. It’s very easy to debug under normal conditions but when it’s deployed in a remote environment this might be more challenging. Especially at scale.

This is true for a simple Kotlin application or for a server application running on Tomcat, Spring, JavaEE or pretty much anything else out there. We need a way to observe live applications and debug issues without interrupting user flows.

This is where Lightrun steps in to make this process trivial and secure without risking your uptime. Notice that this short tutorial uses a simple prime main calculation app for demonstration purposes. But you can use pretty much any Kotlin application with Lightrun. The one constraint is that it’s “long running”, so a hello world application will end too quickly and we won’t have time to attach to it.

Here is the code of the prime main application we’ll use in this tutorial.

Step 1 – Build the Test Application

Download the file, open the directory where it resides and compile the project in IntelliJ IDEA.

Kotlin application in IntelliJ IDEA

Step 2 – Install Lightrun & Run Prime Main

If you didn’t do this yet, create a Lightrun account. Download the IDE plugin and set up the agent on your server. I won’t replicate the steps here as they are pretty clear on the website.

You can download the agent into the project directory then run the app using:

java -agentpath:PATH_TO_AGENT_DIRECTORY/ -classpath lib/kotlin-stdlib.jar:out/production/PrimeKotlin PrimeMainComplex

Notice you need to replace PATH_TO_AGENT_DIRECTORY with the right path. Try to avoid shortcuts like ~ which might cause issues. Also you might need to fix the classpath to match the name you gave to the project.

Notice that if this works you should see no output, the process will just keep running and calculating.

You can now install the plugin and login via the IDE.

Step 3 – Inject a Log

In the IDE open the PrimeMainComplex.kt file. Go to line 47 and right click on it. Select Lightrun and Log:

Setting up a Lightrun log in the Kotlin application

In the Log dialog, type the following code into the Format field: Prime number {cnt} is {i}

Configuring formatting for a log

This injects a new log into the application. Notice the variables in the curly braces. We can use any valid read only Java expression, including method calls in those braces. Notice that this uses Java syntax and not Kotlin syntax!

Now if you look at the app you’ll notice printouts like this:

Oct 24, 2021 3:03:52 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 27 is 107

Oct 24, 2021 3:03:54 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 28 is 109

Oct 24, 2021 3:03:58 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 29 is 113

Oct 24, 2021 3:04:12 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 30 is 127

Oct 24, 2021 3:04:16 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 31 is 131

Oct 24, 2021 3:04:22 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 32 is 137

Oct 24, 2021 3:04:24 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 33 is 139

Oct 24, 2021 3:04:34 PM PrimeMainComplex main
INFO: LOGPOINT: Prime number 34 is 149

Once you’re done with the log you can delete it from the right click menu or the tree on the right.

Step 4 – Add a Snapshot

In PrimeMainComplex.kt, go to line 27 and right-click on it. Select Lightrun and Snapshot (Virtual Breakpoint). Notice that a snapshot doesn’t have a format entry.

Setting up a snapshot in a Kotlin application

The snapshot is like a debugging breakpoint, but it doesn’t stop the execution of the application. As such it can’t break your live application flow.

You can see the stack trace on the left which you can navigate through. You can see variable values on the right matching the current stack frame and you can use those to understand what went on in a particular phase.

Notice that all Lightrun actions (snapshots, logs and metrics) support conditional execution. That means you can use an expression to limit them to a specific case. E.g. userName.equalsIgnoreCase(“Shai”) which can limit logs/snapshots only to the user Shai.

There’s So Much More

I barely scratched the surface here. I didn’t touch metrics, tags or complex usages. Installation and setup are also pretty elaborate in some cases, so if you run into trouble don’t hesitate to contact our support team via the chat widget on the website.

Lightrun can give your application a level of observability that hasn’t been seen before.

See Also

See more posts in this series:


It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.