Skip to content

Instantly share code, notes, and snippets.

@fab1an
Created June 28, 2017 13:22
Show Gist options
  • Save fab1an/4a0ec03a097ab88028c16d803f2a4de1 to your computer and use it in GitHub Desktop.
Save fab1an/4a0ec03a097ab88028c16d803f2a4de1 to your computer and use it in GitHub Desktop.
object SentryLog {
// ~ Properties -------------------------------------------------------------------------------
var netInfo: String? = ""
var appState: AppState? = null
private val gson = createAppStateGson()
// ~ Methods ----------------------------------------------------------------------------------
private class MyRavenFactory(ctx: Context) : AndroidSentryClientFactory(ctx) {
override fun createSentryClient(dsn: Dsn): SentryClient {
val def = super.createSentryClient(dsn)
def.addBuilderHelper { builder ->
val stateStr = try {
gson.toJson(appState).take(50000)
} catch (t: Throwable) {
"error serializing: ${t.message}"
}
builder
.withRelease("${BuildConfig.VERSION_CODE} – ${BuildConfig.BUILD_TYPE}")
.withExtra("AppState", stateStr)
.withExtra("netInfo", netInfo)
}
return def
}
}
fun init(ctx: Context, dsn: String) {
Sentry.init(dsn, MyRavenFactory(ctx))
}
fun addBreadcrumb(category: String, message: String, data: Map<String, String> = emptyMap()) {
if (!BuildConfig.DEBUG)
Sentry.record(BreadcrumbBuilder()
.setCategory(category)
.setData(data)
.setMessage(message).build())
}
fun warn(message: String, extra: Map<String, String>) {
if (!BuildConfig.DEBUG)
Sentry.capture(EventBuilder().apply {
withMessage(message)
extra.forEach { (key, value) ->
withExtra(key, value)
}
})
}
fun addBreadcrumb(event: Controller.AppEvent) =
when (event) {
is UIEvent -> addBreadcrumb("UIEvent", "${event.eventName}: ${event.data}")
is NetworkResponse -> addBreadcrumb("NetworkResponse", "$event")
else -> Unit
}
fun addBreadcrumb(from: List<Screen>, to: List<Screen>) {
if (!BuildConfig.DEBUG) {
val fromStr = from.map(Screen::name).joinToString("/")
val toStr = to.map(Screen::name).joinToString("/")
Sentry.record(BreadcrumbBuilder()
.setCategory("screens")
.setMessage("$fromStr - $toStr")
.build())
}
}
fun captureException(t: Throwable) {
if (!BuildConfig.DEBUG) Sentry.capture(t)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment