Skip to content

Commit

Permalink
Merge pull request #1752 from embrace-io/fix-internal-error-logging
Browse files Browse the repository at this point in the history
Set internal error logger correctly
  • Loading branch information
fractalwrench authored Dec 10, 2024
2 parents ac235c4 + bd9deea commit 56eb868
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.embrace.android.embracesdk.internal.logging

import android.util.Log
import io.embrace.android.embracesdk.internal.utils.Provider
import java.util.concurrent.atomic.AtomicBoolean

internal const val EMBRACE_TAG = "[Embrace]"
Expand All @@ -12,7 +13,7 @@ internal const val EMBRACE_TAG = "[Embrace]"
class EmbLoggerImpl : EmbLogger {

private val loggedSdkNotStarted = AtomicBoolean(false)
var errorHandler: InternalErrorHandler? = null
override var errorHandlerProvider: Provider<InternalErrorHandler?> = { null }

override fun logInfo(msg: String, throwable: Throwable?) {
log(msg, EmbLogger.Severity.INFO, throwable)
Expand All @@ -31,7 +32,7 @@ class EmbLoggerImpl : EmbLogger {

override fun trackInternalError(type: InternalErrorType, throwable: Throwable) {
try {
errorHandler?.trackInternalError(type, throwable)
errorHandlerProvider()?.trackInternalError(type, throwable)
} catch (exc: Throwable) {
// don't cause a crash loop!
Log.w(EMBRACE_TAG, "Failed to track internal error", exc)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.embrace.android.embracesdk.internal.logging

import io.embrace.android.embracesdk.internal.utils.Provider

/**
* A simple interface that is used within the Embrace SDK for logging.
*/
Expand All @@ -9,6 +11,11 @@ interface EmbLogger : InternalErrorHandler {
DEBUG, INFO, WARNING, ERROR
}

/**
* The implementation of the internal error handler. This is set after the logger is initialized.
*/
var errorHandlerProvider: Provider<InternalErrorHandler?>

/**
* Logs an informational message.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.embrace.android.embracesdk.testcases.features

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.embrace.android.embracesdk.fakes.FakeEmbLogger
import io.embrace.android.embracesdk.internal.spans.findAttributeValue
import io.embrace.android.embracesdk.testframework.IntegrationTestRule
import io.embrace.android.embracesdk.testframework.assertions.getLastLog
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
internal class InternalErrorLogTest {

@Rule
@JvmField
val testRule: IntegrationTestRule = IntegrationTestRule()

@Test
fun `internal error log delivered`() {
testRule.runTest(
setupAction = {
(overriddenInitModule.logger as FakeEmbLogger).throwOnInternalError = false
},
testCaseAction = {
recordSession {
embrace.impl.internalInterface.logInternalError("Some error message", null)
}
},
assertAction = {
with(getSingleLogEnvelope().getLastLog()) {
assertEquals("ERROR", severityText)
assertEquals("", body)

val attrs = checkNotNull(attributes)
assertEquals("sys.internal", attrs.findAttributeValue("emb.type"))
assertEquals(
"Some error message",
attrs.findAttributeValue("exception.message")
)
assertEquals(
"java.lang.RuntimeException",
attrs.findAttributeValue("exception.type")
)
assertNotNull(attrs.findAttributeValue("log.record.uid"))
assertNotNull(attrs.findAttributeValue("session.id"))
checkNotNull(attrs.findAttributeValue("exception.stacktrace"))
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ internal class ModuleInitBootstrapper(
}
postInit(FeatureModule::class) {
featureModule.registerFeatures()
(initModule.logger as? EmbLoggerImpl)?.let {
it.errorHandler = featureModule.internalErrorDataSource.dataSource
}
initModule.logger.errorHandlerProvider = { featureModule.internalErrorDataSource.dataSource }
}

dataCaptureServiceModule = init(DataCaptureServiceModule::class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.embrace.android.embracesdk.fakes

import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.embrace.android.embracesdk.internal.logging.InternalErrorHandler
import io.embrace.android.embracesdk.internal.logging.InternalErrorType
import io.embrace.android.embracesdk.internal.utils.Provider

class FakeEmbLogger(
var throwOnInternalError: Boolean = true,
override var errorHandlerProvider: Provider<InternalErrorHandler?> = { null },
) : EmbLogger {

data class LogMessage(
Expand Down Expand Up @@ -37,5 +40,6 @@ class FakeEmbLogger(
throw IllegalStateException("Internal error: $type", throwable)
}
internalErrorMessages.add(LogMessage(type.toString(), throwable))
errorHandlerProvider()?.trackInternalError(type, throwable)
}
}

0 comments on commit 56eb868

Please sign in to comment.