Featured Image
Software Development

Extension function to log messages in Kotlin

It is very common that you log messages while developing an Android app. And you must be writing below code all the time in each of your activity.

private static final String TAG = "MyActivity";
// Or
private static final String TAG = YouActivity.class.getName();
Log.d(TAG, "Your message goes here");

Right? So now in Kotlin, We can take advantage of extension function to remove TAG from each activity and have it once in extension function.

But, What is an Extension function?

Extension functions allow us to extend the functionality of classes without having to touch its code. So now we can extend Activity to create our own function. In this case, We will extend it to log messages.

Okay, Now tell me how to do it!

Step 1

Create Extension.kt file under util package and write the below code in it.

fun Activity.logd(message: String){
  if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, message) }

Extension function logd in Extensions.kt

To create an extension function, put the name of the class that you want to extend then put a dot (.) and then name of your function. In the above code, we are creating the extension function for Activity that will log the messages. And, that’s it! We created an extension function.

Step 2

Just call our very own created extension function from any activity. No need to create and pass TAG like before. Just write this.

logd("Fab button clicked")

Calling extension function from MainActivity

And you are done!

Read further for more info on extension function-

With an extension function, you can extend and create your own function even if you don’t have access to the code. There are lots of possible uses of the extension function. Consider a case where you want a little modification in the library that you are using.

You can also create extension function to display toast in activity instead of writing in each of your activity like this

Toast.makeText(this, "Display toast", Toast.LENGTH_SHORT).show();

You can just create extension function such as

fun Activity.toast(message: String){
  Toast.makeText(this, message , Toast.LENGTH_SHORT).show();
}

Extensions.kt

And call it from any activity as

toast("Display toast")

MainActivity

Awesome, right? Seems like giving super powers to existing functions in the class.

And that is all! Feedback appreciated. Thanks!

Learn more about extension function here.

Also read: Dart or Kotlin: Selecting the Perfect Language for Your App Development

author
Pinkesh Darji
I love to solve problems using technology that improves user’s life on a major scale. Over the last several years, I have been developing and leading various mobile apps in different areas. More than just programming, I love to write technical articles. I have written many high-quality technical articles. I have worked with various startups to build their dream app. I have been involved in product development since my early days and know insights into it. I have provided my valuable input while taking some crucial decisions of the app by brainstorming with a design, QA team. Over the last 3 years, I have been developing mobile apps and libraries using Google’s Flutter framework. I mentor junior Flutter developers and review their code. You will also find me talking on various Flutter topics in local meetups.