Kotlin has some great powerful features packed in it that we should be making use of to improve our code. This involves rethinking on our old best practices of coding. Many of our old coding practices can be replaced by better alternatives from Kotlin. One of them is how we write our logger. Though there are a lot of libraries out there that provide logging functionality, we will try to create our own logger in this recipe, just by using idiomatic Kotlin.
How to write an idiomatic logger in Kotlin
Getting ready
We will be using IntelliJ IDE to write and execute our code.
How to do it...
Let's go through the given steps to create an idiomatic logger in Kotlin:
- First, let's see how it was done in Java. In Java, SLF4J is used and considered de-facto, so much that logging seems like a solved problem in Java language. Here's what a Java implementation would look like:
private static final Logger logger = LoggerFactory.getLogger(CurrentClass.class);
…
logger.info(“Hi, {}”, name);
- It also works fine with Kotlin, obviously with minor modifications:
val logger = LoggerFactory.getLogger(CurrentClass::class)
…
logger.info(“Hi, {}”, name)
However, apart from this, we can utilize the power of Kotlin using Delegates for the logger. In this case, we will be creating the logger using the lazy keyword. This way, we will create the object only when we access it. Delegates are a great way to postpone object creation until we use it. This improves startup time (which is much needed and appreciated in Android). So let us explore a method using lazy delegates in Kotlin:
- We'll use java.util.Logging internally, but this works for any Logging library of your choice. So let’s use the Kotlin’s lazy delegate to get our logger:
public fun <R : Any> R.logger(): Lazy<Logger> {
return lazy { Logger.getLogger(this.javaClass.name) }
}
- Now in our class, we can simply call the method to get our logger and use it:
class SomeClass {
companion object { val log by logger() }
fun do_something() {
log.info("Did Something")
}
}
When you run the code, you can see the following output:
Sep 25, 2017 10:49:00 PM packageA.SomeClass do_something
INFO: Did Something
So, as we can see in the output, we get the class name and method name too (if you are accessing logger inside a method).
How it works...
Here, one thing to note is that we have put our logger inside a companion object. The reason for this is quite straightforward because we want to have only one instance of logger per class.
Also, logger() returns a delegate object, which means that the object will be created on its first access and will return the same value (object) on subsequent accesses.
There's more...
Anko is an Android library that uses Kotlin and makes Android development easier with the help of extension functions. It provides Anko-logger, which you can use if you don’t want to write your own logger. It is included in anko-commons, which also has a lot of interesting things to make it worthwhile to include it in your Android projects that use Kotlin.
In Anko, a standard implementation of logger will look something like this:
class SomeActivity : Activity(), AnkoLogger {
private fun someMethod() {
info("London is the capital of Great Britain")
debug(5) // .toString() method will be executed
warn(null) // "null" will be printed
}
}
As you can see, you just need to implement AnkoLogger and you are done.
Each method has two versions: plain and lazy (inlined):
info("String " + "concatenation")
info { "String " + "concatenation" }
The lambda result will be calculated only if Log.isLoggable(tag, Log.INFO) is true.
See also
To know more about delegated properties, refer to the Working with delegated Properties recipe in Chapter 3, Classes and Objects.