Last active
February 6, 2022 09:18
-
-
Save risalfajar/b6bcf606e600c940ab4be208f2aeede2 to your computer and use it in GitHub Desktop.
Integrate Crashlytics into Timber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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