Skip to content

Instantly share code, notes, and snippets.

@elroid
Created May 18, 2018 07:49
Show Gist options
  • Save elroid/af628851e085784ffcb84511441fad0b to your computer and use it in GitHub Desktop.
Save elroid/af628851e085784ffcb84511441fad0b to your computer and use it in GitHub Desktop.
Crashlytics debug tree for Timber
import android.util.Log
import com.crashlytics.android.Crashlytics
import timber.log.Timber
import java.util.*
class CrashlyticsTree(val logLevel: Int) : Timber.Tree() {
override fun isLoggable(tag: String?, priority: Int): Boolean {
return priority >= logLevel
}
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
//15:47:28.123 - ClassName: DEBUG/My Message here
var msg = Date().toString()
msg += " - "
if (tag != null) msg += "$tag: "
msg += print(priority) + "/"
msg += message
if (t != null) {
//add \nStack:...
val elements = t.stackTrace
msg += "\nStack:"
for (element in elements) {
msg += "\n\t" + element
}
}
Crashlytics.log(tag + ": " + print(priority) + "/" + msg)
}
private fun print(priority: Int): String {
return when (priority) {
Log.VERBOSE -> "TRACE"
Log.DEBUG -> "DEBUG"
Log.WARN -> "WARN "
Log.ERROR -> "ERROR"
else -> "? ($priority)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment