@@ -2,7 +2,9 @@ package com.steamclock.steamclog
22
33import android.annotation.SuppressLint
44import android.util.Log
5+ import io.sentry.Breadcrumb
56import io.sentry.Sentry
7+ import io.sentry.SentryLevel
68import timber.log.Timber
79import java.io.File
810import java.text.SimpleDateFormat
@@ -41,35 +43,48 @@ internal class SentryDestination : Timber.Tree() {
4143 val originalMessage = wrapper?.originalMessage ? : message
4244 val originalThrowable = wrapper?.originalThrowable
4345
44- // Always add breadcrumb that indicates the log message.
45- Sentry .addBreadcrumb(generateSimpleLogMessage(
46- priority,
47- includeEmoji = false ,
48- wrapper,
49- message
50- ), breadcrumbCategory)
46+ // Create a full log message and include as a breadcrumb to Sentry.
47+ val verboseLogMessage = generateSimpleLogMessage(priority, includeEmoji = false , wrapper, originalMessage)
48+ Sentry .addBreadcrumb(createSentryBreadcrumbFor(priority, verboseLogMessage))
5149
52- when {
53- priority == Log .ERROR && originalThrowable != null -> {
54- // Check to see if we want to allow or block the Throwable from being reported
55- // as an error .
50+ // Log error report if desired.
51+ if ( priority == Log .ERROR ) {
52+ if (originalThrowable != null ) {
53+ // Check to see if we want to block this Throwable from being logged as an Error .
5654 if (SteamcLog .config.filtering.shouldBlock(originalThrowable)) {
57- Sentry .addBreadcrumb(" ${originalThrowable::class .simpleName} on blocked list, and has " +
58- " been blocked from being captured as an exception: " +
59- " ${originalThrowable.message} " , breadcrumbCategory)
60- } else {
61- Sentry .captureException(originalThrowable)
55+ Sentry .addBreadcrumb(
56+ " ${originalThrowable::class .simpleName} on blocked list, and has " +
57+ " been blocked from being captured as an exception: " +
58+ " ${originalThrowable.message} " , breadcrumbCategory
59+ )
60+ return
6261 }
62+ // Log stack trace as a breadcrumb (this logged as an INFO breadcrumb by default)
63+ Sentry .addBreadcrumb(originalThrowable.stackTraceToString(), breadcrumbCategory)
6364 }
64- priority == Log .ERROR -> {
65- // If no throwable given, capture message as the error
66- Sentry .captureMessage(originalMessage)
67- }
68- else -> {
69- // Not an error; breadcrumb should already be added, so we do not
70- // need to do anything more.
71- }
65+
66+ // Always use the message as our error report, as this gives us way more contextual
67+ // info about the problem than the name of the Exception.
68+ Sentry .captureMessage(originalMessage, SentryLevel .ERROR )
69+ }
70+ }
71+
72+ /* *
73+ * Takes the given priority level and creates a breadcrumb at a similar level. Mappings
74+ * aren't 1 to 1, but this should still give us better detail in our breadcrumb log.
75+ */
76+ private fun createSentryBreadcrumbFor (priority : Int , message : String ): Breadcrumb {
77+ val breadcrumb = when (priority) {
78+ Log .ASSERT -> Breadcrumb .error(message)
79+ Log .ERROR -> Breadcrumb .error(message)
80+ Log .WARN -> Breadcrumb .info(message)
81+ // Log.INFO is the default
82+ Log .DEBUG -> Breadcrumb .debug(message)
83+ Log .VERBOSE -> Breadcrumb .debug(message)
84+ else -> Breadcrumb .info(message)
7285 }
86+ breadcrumb.category = breadcrumbCategory
87+ return breadcrumb
7388 }
7489}
7590
0 commit comments