https://kotlinlang.org logo
Title
s

STAYER

02/07/2020, 7:22 PM
Hello guys! Can someone explain me why extension function not working with Log class or Crashlytics?
fun Log.dd(msg: String? = "null", tag: String = "test1") = Log.d(tag, msg)
r

rkeazor

02/07/2020, 7:27 PM
They dont work with statics
👍 1
s

STAYER

02/07/2020, 7:46 PM
Thanks! They really have private constructor with no chance to create instance.
r

Rajkumar Singh

02/08/2020, 2:03 AM
did you mean msg: String? = null
or msg: String? = "null"
s

STAYER

02/10/2020, 9:40 AM
@Rajkumar Singh, it's just example. I realize 2 extensions, this:
fun Any.logE(msg: String, tr: Throwable? = null, tag: String = "test1") = when (tr) {
    null -> Log.e(tag, msg)
    else -> Log.e(tag, msg, tr)
}
And this:
fun Any.crashlyticsLog(tr: Throwable, msg: String): Unit {
    logE(msg, tr)
    Crashlytics.log(msg)
    Crashlytics.logException(tr)
}
So i can log errors to Logcat and report to crashlytics in one line. For example:
try {
    //Do something
} catch(e: Exception) {
    crashlyticsLog(e, "some info")
}
p

pavi2410

03/16/2020, 3:11 AM
@STAYER you need to take a look into Timber library. It's built for that
👍 1
s

STAYER

03/16/2020, 3:18 AM
@pavi2410 yep, i use Timber in new project, and it is nice)
p

pavi2410

03/16/2020, 3:18 AM
Great to know that