Skip to content

Instantly share code, notes, and snippets.

@risalfajar
Last active February 6, 2022 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save risalfajar/b6bcf606e600c940ab4be208f2aeede2 to your computer and use it in GitHub Desktop.
Save risalfajar/b6bcf606e600c940ab4be208f2aeede2 to your computer and use it in GitHub Desktop.
Integrate Crashlytics into Timber
/**
* Application class integrated with Timber and Crashlytics
*/
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
if(BuildConfig.DEBUG) Timber.plant(Timber.DebugTree())
else Timber.plant(FirebaseCrashlyticsReportingTree())
}
private class FirebaseCrashlyticsReportingTree : Timber.Tree() {
companion object{
private const val CRASHLYTICS_KEY_PRIORITY = "priority"
private const val CRASHLYTICS_KEY_TAG = "tag"
private const val CRASHLYTICS_KEY_MESSAGE = "message"
}
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if(priority == Log.VERBOSE || priority == Log.DEBUG) return
val throwable = t ?: Exception(message)
//Crashlytics error details
FirebaseCrashlytics.getInstance().apply {
setCustomKey(CRASHLYTICS_KEY_PRIORITY, priority)
setCustomKey(CRASHLYTICS_KEY_MESSAGE, message)
setCustomKey(CRASHLYTICS_KEY_TAG, tag ?: "unknown")
}.also {
it.recordException(throwable)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment