https://kotlinlang.org logo
#android
Title
# android
e

erikmedina

10/24/2018, 11:45 AM
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

tirgei

10/24/2018, 11:47 AM
You can still use Timber with Kotlin
3
s

Sergio Casero

10/24/2018, 11:48 AM
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

erikmedina

10/24/2018, 11:51 AM
Extensions sounds good...
s

Sergio Casero

10/24/2018, 11:52 AM
But you can't use this extensions in domain modules...
e

erikmedina

10/24/2018, 11:56 AM
mm I have to take a look why I can't use these extensions in domain modules...
a

arekolek

10/24/2018, 12:13 PM
I think he means it will crash if you try to run this code on JVM (unit tests)
👍🏼 1
s

Sergio Casero

10/24/2018, 12:41 PM
jjmmm, in that, case, you can do the same thing by using println()
✔️ 1
a

Allan Wang

10/24/2018, 3:49 PM
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

egorand

10/24/2018, 3:58 PM
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

rkeazor

10/24/2018, 4:25 PM
Why not just use Log or Timber. How much more idiomatic could they really be?
2 Views