Hello there! do you know if there are specific Ko...
# android
e
Hello there! do you know if there are specific Kotlin options to show logs in Android? With Java, I used to use Timber but I'd like to know if there are more "Kotlin" options. Thank you.
t
You can still use Timber with Kotlin
3
s
I have extensions functions for logger
Copy code
/**
 * Any
 * */
fun <http://Any.info|Any.info>(text: String) {
    Log.i(this::class.java.simpleName, text)
}

fun Any.error(text: String) {
    Log.e(this::class.java.simpleName, text)
}

fun Any.error(text: String, exception: Exception) {
    Log.e(this::class.java.simpleName, text, exception)
}

fun Any.warn(text: String) {
    Log.w(this::class.java.simpleName, text)
}

fun Any.warn(text: String, exception: Exception) {
    Log.w(this::class.java.simpleName, text, exception)
}

fun Any.debug(text: String) {
    Log.d(this::class.java.simpleName, text)
}

fun Any.debug(text: String, exception: Exception) {
    Log.d(this::class.java.simpleName, text, exception)
}
e
Extensions sounds good...
s
But you can't use this extensions in domain modules...
e
mm I have to take a look why I can't use these extensions in domain modules...
a
I think he means it will crash if you try to run this code on JVM (unit tests)
👍🏼 1
s
jjmmm, in that, case, you can do the same thing by using println()
✔️ 1
a
In android, I like to add logging through an object and have all messages have the same tag: https://github.com/AllanWang/KAU/blob/dev/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt On server side, I like to use delegations of
WithLogging
so that all logs in a specific class are tagged as so: https://stackoverflow.com/questions/34416869/idiomatic-way-of-logging-in-kotlin I’m not sure if you want to extend
Any
, as your logs are going to be affected if you ever use
apply
or other lambdas of type
T.() -> ...
e
the downside of creating extension functions on
Any
is that it messes up code completion, you'll be seeing those functions in the dropdown everywhere, which you probably don't want
r
Why not just use Log or Timber. How much more idiomatic could they really be?