Logging in Kotlin— the right way


Let’s see how we typically log something in Android using Java before Kotlin.

But this is not how we actually do it, right? We don’t want to leak the logs in release build. So we use a util class which actually calls the methods, guarded by a check.

As you can image, we would have other methods for , , etc. This works pretty well, but can we do something better than this with the power of Kotlin?

First let’s see some problems with this typical approach.

  • TAG variable needs to be in every class we wish to add log.
  • The message is eagerly evaluated which is a problem if you do extensive logging, since on release builds even though the message is not used, it would still be evaluated.
Here the method call is evaluated on release build too which is unnecessary.

Let’s see an upgraded version of this using Kotlin.

Ah! This is much better.

  • We are adding the method as an extension of type here. is the equivalent of from Java and hence the method is an extension of it, we can access this method using all instances of all objects.
  • We are also setting the class name as . This removes the need to have a separate variable for this.
  • Now the interesting part is, we are not taking the message directly, but as a function which returns the message. This makes the message lazily evaluated and in release build, the lambda inside method calls won’t ever be executed.
  • Another interesting part is, we made the method as an inline function. It basically means, the method body would be copy-pasted at the call site and method and method call would not be present after compilation. So this eliminates the overhead of creating separate function (which is actually an instance of type at bytecode) for each method call.

Again, we can have separate functions for all variants of Log like for , for etc.

Live Templates

One cool thing about the traditional Java logging is that Android Studio has some nice live templates to add logs.

Java Log live templates

It would be nice to have these kind of live templates to add our own log methods, right? Let’s do it.

If you go to File -> Settings/Preferences -> Editor -> Live Templates, under AndroidLog you can see the live templates shown in the above gif.

We can start adding by

  1. Clicking on the plus icon on top right
  2. Select Template Group
  3. Give a name, ex: AndroidLogKotlin
  4. Click the plus icon at top right again
  5. Select Live Template
  6. Now we can just copy paste Abbreviation, Description and Template text from logd from AndroidLog.
  7. Change the template text to use our logging method. We can just add like and import the log method after usage or add with full package name to auto import on usage
  8. Select Reformat according to style option (this is optional)
  9. Now click Define at the bottom near the error No applicable contexts and select Kotlin -> Statement.
  10. Now repeat the steps from 4 to 9 for all other Log variants like , , etc.

This is how it looks like after adding these live templates.



Android Developer at Zoho Corp

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