- Kotlin Programming Cookbook
- Aanand Shekhar Roy Rashi Karanpuria
- 157字
- 2025-04-04 17:32:31
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.