Skip to content

Commit

Permalink
Merge pull request #828 from embrace-io/emb-logger-interface
Browse files Browse the repository at this point in the history
Create interface for internal logging
  • Loading branch information
fractalwrench committed May 10, 2024
2 parents 307d284 + 58417d8 commit bb925cc
Show file tree
Hide file tree
Showing 198 changed files with 601 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import io.embrace.android.embracesdk.gating.SessionGatingKeys
import io.embrace.android.embracesdk.getSentSessionMessages
import io.embrace.android.embracesdk.hasEventOfType
import io.embrace.android.embracesdk.hasSpanOfType
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLoggerImpl
import io.embrace.android.embracesdk.payload.SessionMessage
import io.embrace.android.embracesdk.recordSession
import io.embrace.android.embracesdk.session.orchestrator.SessionSnapshotType
Expand Down Expand Up @@ -50,7 +50,7 @@ internal class OtelSessionGatingTest {
RemoteConfig(sessionConfig = gatingConfig)
},
),
InternalEmbraceLogger()
EmbLoggerImpl()
)

@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import io.embrace.android.embracesdk.internal.crash.LastRunCrashVerifier;
import io.embrace.android.embracesdk.internal.spans.EmbraceTracer;
import io.embrace.android.embracesdk.internal.utils.ThrowableUtilsKt;
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger;
import io.embrace.android.embracesdk.logging.EmbLogger;
import io.embrace.android.embracesdk.logging.InternalErrorService;
import io.embrace.android.embracesdk.logging.InternalErrorServiceAction;
import io.embrace.android.embracesdk.ndk.NativeModule;
Expand Down Expand Up @@ -104,7 +104,7 @@ final class EmbraceImpl {
private final Clock sdkClock;

@NonNull
private final InternalEmbraceLogger internalEmbraceLogger;
private final EmbLogger logger;

/**
* Custom app ID that overrides the one specified at build time
Expand Down Expand Up @@ -209,7 +209,7 @@ final class EmbraceImpl {
EmbraceImpl(@NonNull ModuleInitBootstrapper bs) {
moduleInitBootstrapper = bs;
sdkClock = moduleInitBootstrapper.getInitModule().getClock();
internalEmbraceLogger = moduleInitBootstrapper.getInitModule().getLogger();
logger = moduleInitBootstrapper.getInitModule().getLogger();
tracer = moduleInitBootstrapper.getOpenTelemetryModule().getEmbraceTracer();
uninitializedSdkInternalInterface =
LazyKt.lazy(
Expand Down Expand Up @@ -248,7 +248,7 @@ void startInternal(@NonNull Context context,
startImpl(context, isDevMode, appFramework, configServiceProvider);
Systrace.endSynchronous();
} catch (Throwable t) {
internalEmbraceLogger.logError(
logger.logError(
"Error occurred while initializing the Embrace SDK. Instrumentation may be disabled.", t, true);
}
}
Expand All @@ -259,18 +259,18 @@ private void startImpl(@NonNull Context context,
@NonNull Function0<ConfigService> configServiceProvider) {
if (application != null) {
// We don't hard fail if the SDK has been already initialized.
internalEmbraceLogger.logWarning("Embrace SDK has already been initialized");
logger.logWarning("Embrace SDK has already been initialized", null, false);
return;
}

if (ApkToolsConfig.IS_SDK_DISABLED) {
internalEmbraceLogger.logInfo("SDK disabled through ApkToolsConfig");
logger.logInfo("SDK disabled through ApkToolsConfig");
stop();
return;
}

final long startTimeMs = sdkClock.now();
internalEmbraceLogger.logInfo("Starting SDK for framework " + framework.name());
logger.logInfo("Starting SDK for framework " + framework.name());
moduleInitBootstrapper.init(context, isDevMode, framework, startTimeMs, customAppId, configServiceProvider);
Systrace.startSynchronous("post-services-setup");
telemetryService = moduleInitBootstrapper.getInitModule().getTelemetryService();
Expand All @@ -284,7 +284,7 @@ private void startImpl(@NonNull Context context,

final EssentialServiceModule essentialServiceModule = moduleInitBootstrapper.getEssentialServiceModule();
if (essentialServiceModule.getConfigService().isSdkDisabled()) {
internalEmbraceLogger.logInfo("Interrupting SDK start because it is disabled");
logger.logInfo("Interrupting SDK start because it is disabled");
stop();
return;
}
Expand Down Expand Up @@ -372,7 +372,7 @@ private void startImpl(@NonNull Context context,

final String startMsg = "Embrace SDK started. App ID: " +
essentialServiceModule.getConfigService().getSdkModeBehavior().getAppId() + " Version: " + BuildConfig.VERSION_NAME;
internalEmbraceLogger.logInfo(startMsg);
logger.logInfo(startMsg);

final long endTimeMs = sdkClock.now();
started.set(true);
Expand Down Expand Up @@ -418,7 +418,7 @@ private void registerComposeActivityListener(@NonNull CoreModule coreModule) {
composeActivityListenerInstance = composeActivityListener.newInstance();
coreModule.getApplication().registerActivityLifecycleCallbacks((Application.ActivityLifecycleCallbacks) composeActivityListenerInstance);
} catch (Throwable e) {
internalEmbraceLogger.logError("registerComposeActivityListener error", e);
logger.logError("registerComposeActivityListener error", e, false);
}
}

Expand All @@ -431,7 +431,7 @@ private void unregisterComposeActivityListener(@NonNull Application app) {
try {
app.unregisterActivityLifecycleCallbacks((Application.ActivityLifecycleCallbacks) composeActivityListenerInstance);
} catch (Throwable e) {
internalEmbraceLogger.logError("Instantiation error for ComposeActivityListener", e);
logger.logError("Instantiation error for ComposeActivityListener", e, false);
}
}

Expand All @@ -453,16 +453,16 @@ boolean isStarted() {
*/
boolean setAppId(@NonNull String appId) {
if (isStarted()) {
internalEmbraceLogger.logError("You must set the custom app ID before the SDK is started.");
logger.logError("You must set the custom app ID before the SDK is started.", null, false);
return false;
}
if (appId.isEmpty()) {
internalEmbraceLogger.logError("App ID cannot be null or empty.");
logger.logError("App ID cannot be null or empty.", null, false);
return false;
}
if (!appIdPattern.matcher(appId).find()) {
internalEmbraceLogger.logError("Invalid app ID. Must be a 5-character string with " +
"characters from the set [A-Za-z0-9], but it was \"" + appId + "\".");
logger.logError("Invalid app ID. Must be a 5-character string with " +
"characters from the set [A-Za-z0-9], but it was \"" + appId + "\".", null, false);
return false;
}

Expand All @@ -475,7 +475,7 @@ boolean setAppId(@NonNull String appId) {
*/
void stop() {
if (started.compareAndSet(true, false)) {
internalEmbraceLogger.logInfo("Shutting down Embrace SDK.");
logger.logInfo("Shutting down Embrace SDK.");
try {
if (composeActivityListenerInstance != null && application != null) {
unregisterComposeActivityListener(application);
Expand All @@ -484,7 +484,7 @@ void stop() {
application = null;
moduleInitBootstrapper.stopServices();
} catch (Exception ex) {
internalEmbraceLogger.logError("Error while shutting down Embrace SDK", ex);
logger.logError("Error while shutting down Embrace SDK", ex, false);
}
}
}
Expand Down Expand Up @@ -665,7 +665,7 @@ void startMoment(@NonNull String name,
@Nullable String identifier,
@Nullable Map<String, Object> properties) {
if (checkSdkStartedAndLogPublicApiUsage("start_moment")) {
eventService.startEvent(name, identifier, normalizeProperties(properties, internalEmbraceLogger));
eventService.startEvent(name, identifier, normalizeProperties(properties, logger));
onActivityReported();
}
}
Expand All @@ -681,7 +681,7 @@ void startMoment(@NonNull String name,
*/
void endMoment(@NonNull String name, @Nullable String identifier, @Nullable Map<String, Object> properties) {
if (checkSdkStartedAndLogPublicApiUsage("end_moment")) {
eventService.endEvent(name, identifier, normalizeProperties(properties, internalEmbraceLogger));
eventService.endEvent(name, identifier, normalizeProperties(properties, logger));
onActivityReported();
}
}
Expand Down Expand Up @@ -814,7 +814,7 @@ void logMessage(
message,
type,
logExceptionType,
normalizeProperties(properties, internalEmbraceLogger),
normalizeProperties(properties, logger),
stackTraceElements,
customStackTrace,
appFramework,
Expand All @@ -824,7 +824,7 @@ void logMessage(
exceptionMessage);
onActivityReported();
} catch (Exception ex) {
internalEmbraceLogger.logDebug("Failed to log message using Embrace SDK.", ex);
logger.logDebug("Failed to log message using Embrace SDK.", ex);
}
}
}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ String getCurrentSessionId() {
if (sessionId != null) {
return sessionId;
} else {
internalEmbraceLogger.logInfo("Session ID is null");
logger.logInfo("Session ID is null");
}
}
return null;
Expand Down Expand Up @@ -1107,7 +1107,7 @@ void logRnAction(@NonNull String name, long startTime, long endTime,
*/
void logRnView(@NonNull String screen) {
if (appFramework != Embrace.AppFramework.REACT_NATIVE) {
internalEmbraceLogger.logWarning("[Embrace] logRnView is only available on React Native");
logger.logWarning("[Embrace] logRnView is only available on React Native", null, false);
return;
}

Expand Down Expand Up @@ -1158,21 +1158,21 @@ private void sampleCurrentThreadDuringAnrs() {
service
);
} else {
internalEmbraceLogger.logWarning("nativeThreadSamplerInstaller not started, cannot sample current thread");
logger.logWarning("nativeThreadSamplerInstaller not started, cannot sample current thread", null, false);
}
} catch (Exception exc) {
internalEmbraceLogger.logError("Failed to sample current thread during ANRs", exc);
logger.logError("Failed to sample current thread during ANRs", exc, false);
}
}

@Nullable
private Map<String, Object> normalizeProperties(@Nullable Map<String, ?> properties, @Nullable InternalEmbraceLogger logger) {
private Map<String, Object> normalizeProperties(@Nullable Map<String, ?> properties, @Nullable EmbLogger logger) {
Map<String, Object> normalizedProperties = new HashMap<>();
if (properties != null) {
try {
normalizedProperties = PropertyUtils.sanitizeProperties(properties, logger);
} catch (Exception e) {
internalEmbraceLogger.logError("Exception occurred while normalizing the properties.", e);
this.logger.logError("Exception occurred while normalizing the properties.", e, false);
}
return normalizedProperties;
} else {
Expand All @@ -1193,7 +1193,7 @@ private boolean checkSdkStartedAndLogPublicApiUsage(@NonNull String action) {
private boolean checkSdkStarted(@NonNull String action, boolean logPublicApiUsage) {
boolean isStarted = isStarted();
if (!isStarted) {
internalEmbraceLogger.logSdkNotInitialized(action);
logger.logSdkNotInitialized(action);
}
if (telemetryService != null && logPublicApiUsage) {
telemetryService.onPublicApiCalled(action);
Expand All @@ -1203,15 +1203,15 @@ private boolean checkSdkStarted(@NonNull String action, boolean logPublicApiUsag

public void addSpanExporter(@NonNull SpanExporter spanExporter) {
if (isStarted()) {
internalEmbraceLogger.logError("A SpanExporter can only be added before the SDK is started.");
logger.logError("A SpanExporter can only be added before the SDK is started.", null, false);
return;
}
moduleInitBootstrapper.getOpenTelemetryModule().getOpenTelemetryConfiguration().addSpanExporter(spanExporter);
}

public void addLogRecordExporter(@NonNull LogRecordExporter logRecordExporter) {
if (isStarted()) {
internalEmbraceLogger.logError("A LogRecordExporter can only be added before the SDK is started.");
logger.logError("A LogRecordExporter can only be added before the SDK is started.", null, false);
return;
}
moduleInitBootstrapper.getOpenTelemetryModule().getOpenTelemetryConfiguration().addLogExporter(logRecordExporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package io.embrace.android.embracesdk

import io.embrace.android.embracesdk.capture.metadata.HostedSdkVersionInfo
import io.embrace.android.embracesdk.internal.EmbraceInternalInterface
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLogger

internal class FlutterInternalInterfaceImpl(
private val embrace: EmbraceImpl,
private val impl: EmbraceInternalInterface,
private val hostedSdkVersionInfo: HostedSdkVersionInfo,
private val logger: InternalEmbraceLogger
private val logger: EmbLogger
) : EmbraceInternalInterface by impl, FlutterInternalInterface {

override fun setEmbraceFlutterSdkVersion(version: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.embrace.android.embracesdk.capture.crash.CrashService
import io.embrace.android.embracesdk.capture.metadata.HostedSdkVersionInfo
import io.embrace.android.embracesdk.capture.metadata.MetadataService
import io.embrace.android.embracesdk.internal.EmbraceInternalInterface
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.payload.JsException

internal class ReactNativeInternalInterfaceImpl(
Expand All @@ -16,7 +16,7 @@ internal class ReactNativeInternalInterfaceImpl(
private val crashService: CrashService,
private val metadataService: MetadataService,
private val hostedSdkVersionInfo: HostedSdkVersionInfo,
private val logger: InternalEmbraceLogger
private val logger: EmbLogger
) : EmbraceInternalInterface by impl, ReactNativeInternalInterface {

override fun logUnhandledJsException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package io.embrace.android.embracesdk

import io.embrace.android.embracesdk.capture.metadata.HostedSdkVersionInfo
import io.embrace.android.embracesdk.internal.EmbraceInternalInterface
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.embrace.android.embracesdk.network.http.HttpMethod

internal class UnityInternalInterfaceImpl(
private val embrace: EmbraceImpl,
private val impl: EmbraceInternalInterface,
private val hostedSdkVersionInfo: HostedSdkVersionInfo,
private val logger: InternalEmbraceLogger
private val logger: EmbLogger
) : EmbraceInternalInterface by impl, UnityInternalInterface {

override fun setUnityMetaData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.embrace.android.embracesdk.anr.detection.UnbalancedCallDetector
import io.embrace.android.embracesdk.config.ConfigService
import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.enforceThread
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.payload.AnrInterval
import io.embrace.android.embracesdk.session.MemoryCleanerListener
import io.embrace.android.embracesdk.session.lifecycle.ProcessStateListener
Expand All @@ -28,7 +28,7 @@ import java.util.concurrent.atomic.AtomicReference
internal class EmbraceAnrService(
var configService: ConfigService,
looper: Looper,
logger: InternalEmbraceLogger,
logger: EmbLogger,
livenessCheckScheduler: LivenessCheckScheduler,
private val anrMonitorWorker: ScheduledWorker,
state: ThreadMonitoringState,
Expand All @@ -39,7 +39,7 @@ internal class EmbraceAnrService(
private val state: ThreadMonitoringState
private val targetThread: Thread
val stacktraceSampler: AnrStacktraceSampler
private val logger: InternalEmbraceLogger
private val logger: EmbLogger
private val targetThreadHeartbeatScheduler: LivenessCheckScheduler

val listeners = CopyOnWriteArrayList<BlockedThreadListener>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.embrace.android.embracesdk.anr.BlockedThreadListener
import io.embrace.android.embracesdk.config.ConfigService
import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.enforceThread
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.payload.ResponsivenessSnapshot
import java.util.concurrent.atomic.AtomicReference

Expand Down Expand Up @@ -37,7 +37,7 @@ internal class BlockedThreadDetector(
var listener: BlockedThreadListener? = null,
private val state: ThreadMonitoringState,
private val targetThread: Thread,
private val logger: InternalEmbraceLogger,
private val logger: EmbLogger,
private val anrMonitorThread: AtomicReference<Thread>
) {
private val heartbeatResponseMonitor = ResponsivenessMonitor(clock = clock, name = "heartbeatResponse")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.embrace.android.embracesdk.anr.detection.TargetThreadHandler.Companion
import io.embrace.android.embracesdk.config.ConfigService
import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.enforceThread
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.payload.ResponsivenessSnapshot
import io.embrace.android.embracesdk.worker.ScheduledWorker
import java.util.concurrent.ScheduledFuture
Expand All @@ -29,7 +29,7 @@ internal class LivenessCheckScheduler internal constructor(
private val state: ThreadMonitoringState,
private val targetThreadHandler: TargetThreadHandler,
private val blockedThreadDetector: BlockedThreadDetector,
private val logger: InternalEmbraceLogger,
private val logger: EmbLogger,
private val anrMonitorThread: AtomicReference<Thread>
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.os.MessageQueue
import io.embrace.android.embracesdk.config.ConfigService
import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.enforceThread
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.worker.ScheduledWorker
import java.util.concurrent.ExecutorService
import java.util.concurrent.atomic.AtomicReference
Expand All @@ -30,7 +30,7 @@ internal class TargetThreadHandler(
private val anrMonitorThread: AtomicReference<Thread>,
private val configService: ConfigService,
private val messageQueue: MessageQueue? = LooperCompat.getMessageQueue(looper),
private val logger: InternalEmbraceLogger,
private val logger: EmbLogger,
private val clock: Clock,
) : Handler(looper) {

Expand Down
Loading

0 comments on commit bb925cc

Please sign in to comment.