Skip to content

Commit

Permalink
Delete the static logger
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Apr 3, 2024
1 parent d25f1e0 commit 67efcdb
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.embrace.android.embracesdk.fcm.swazzle.callback.com.android.fcm;

import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.firebase.messaging.RemoteMessage;

Expand All @@ -18,10 +17,8 @@ private FirebaseSwazzledHooks() {
@SuppressWarnings("MethodNameCheck")
@InternalApi
public static void _onMessageReceived(@NonNull RemoteMessage message) {
logger.logDebug("Embrace received push notification message");

if (!Embrace.getInstance().isStarted()) {
logger.logError("Embrace received push notification data before the SDK was started");
logError("Embrace received push notification data before the SDK was started", null);
return;
}

Expand All @@ -37,29 +34,29 @@ private static void handleRemoteMessage(@NonNull RemoteMessage message) {
try {
messageId = message.getMessageId();
} catch (Exception e) {
logger.logError("Failed to capture FCM messageId", e);
logError("Failed to capture FCM messageId", e);
}

String topic = null;
try {
topic = message.getFrom();
} catch (Exception e) {
logger.logError("Failed to capture FCM topic", e);
logError("Failed to capture FCM topic", e);
}

Integer messagePriority = null;
try {
messagePriority = message.getPriority();
} catch (Exception e) {
logger.logError("Failed to capture FCM message priority", e);
logError("Failed to capture FCM message priority", e);
}

RemoteMessage.Notification notification = null;

try {
notification = message.getNotification();
} catch (Exception e) {
logger.logError("Failed to capture FCM RemoteMessage Notification", e);
logError("Failed to capture FCM RemoteMessage Notification", e);
}

String title = null;
Expand All @@ -69,19 +66,19 @@ private static void handleRemoteMessage(@NonNull RemoteMessage message) {
try {
title = notification.getTitle();
} catch (Exception e) {
logger.logError("Failed to capture FCM title", e);
logError("Failed to capture FCM title", e);
}

try {
body = notification.getBody();
} catch (Exception e) {
logger.logError("Failed to capture FCM body", e);
logError("Failed to capture FCM body", e);
}

try {
notificationPriority = notification.getNotificationPriority();
} catch (Exception e) {
logger.logError("Failed to capture FCM notificationPriority", e);
logError("Failed to capture FCM notificationPriority", e);
}
}

Expand All @@ -100,10 +97,17 @@ private static void handleRemoteMessage(@NonNull RemoteMessage message) {
hasData
);
} catch (Exception e) {
logger.logError("Failed to log push Notification", e);
logError("Failed to log push Notification", e);
}
} catch (Exception e) {
logger.logError("Push Notification Error", e);
logError("Push Notification Error", e);
}
}

private static void logError(@NonNull String message, @Nullable Exception e) {
Embrace.getInstance().getInternalInterface().logError(message, null, null, false);
if (e != null) {
Embrace.getInstance().getInternalInterface().logInternalError(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.embrace.android.embracesdk.okhttp3.swazzle.callback.okhttp3;

import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import java.util.List;

import io.embrace.android.embracesdk.Embrace;
import io.embrace.android.embracesdk.annotation.InternalApi;
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3ApplicationInterceptor;
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3NetworkInterceptor;
Expand Down Expand Up @@ -35,13 +34,13 @@ private Builder() {
*/
@SuppressWarnings("MethodNameCheck")
public static void _preBuild(okhttp3.OkHttpClient.Builder thiz) {
logger.logDebug("Embrace OkHTTP Wrapper; onPrebuild");
logInfo("Embrace OkHTTP Wrapper; onPrebuild");
addEmbraceInterceptors(thiz);
}

@SuppressWarnings("MethodNameCheck")
public static void _constructorOnPostBody(okhttp3.OkHttpClient.Builder thiz) {
logger.logDebug("Embrace OkHTTP Wrapper; onPostBody");
logInfo("Embrace OkHTTP Wrapper; onPostBody");
addEmbraceInterceptors(thiz);
}

Expand All @@ -52,16 +51,15 @@ public static void _constructorOnPostBody(okhttp3.OkHttpClient.Builder thiz) {
*/
private static void addEmbraceInterceptors(okhttp3.OkHttpClient.Builder thiz) {
try {
logger.logDebug("Embrace OkHTTP Wrapper; Adding interceptors");
logInfo("Embrace OkHTTP Wrapper; Adding interceptors");
addInterceptor(thiz.interceptors(), new EmbraceOkHttp3ApplicationInterceptor());
addInterceptor(thiz.networkInterceptors(), new EmbraceOkHttp3NetworkInterceptor());
} catch (NoSuchMethodError exception) {
// The customer may be overwriting OkHttpClient with their own implementation, and some of the
// methods we use are missing.
logger.logError("Altered OkHttpClient implementation, could not add OkHttp interceptor. ",
exception);
logError("Altered OkHttpClient implementation, could not add OkHttp interceptor. ", exception);
} catch (Exception exception) {
logger.logError("Could not add OkHttp interceptor. ", exception);
logError("Could not add OkHttp interceptor. ", exception);
}
}

Expand All @@ -76,8 +74,8 @@ private static void addInterceptor(List<Interceptor> interceptors,
if (interceptors != null && !containsInstance(interceptors, interceptor.getClass())) {
interceptors.add(0, interceptor);
} else {
logger.logDebug(
"Not adding interceptor [" + interceptor.getClass().getSimpleName() + "]"
logInfo(
"Not adding interceptor [" + interceptor.getClass().getSimpleName() + "]"
);
}
}
Expand All @@ -94,13 +92,22 @@ private static <T> boolean containsInstance(List<T> elementsList,
Class<? extends T> clazz) {
for (T classInstance : elementsList) {
if (clazz.isInstance(classInstance)) {
logger.logDebug(
"[" + clazz.getSimpleName() + "] already present in list"
logInfo(
"[" + clazz.getSimpleName() + "] already present in list"
);
return true;
}
}
return false;
}

private static void logInfo(String message) {
Embrace.getInstance().getInternalInterface().logInfo(message, null);
}

private static void logError(String message, Throwable throwable) {
Embrace.getInstance().getInternalInterface().logError(message, null, null, false);
Embrace.getInstance().getInternalInterface().logInternalError(throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import android.os.Build.VERSION_CODES.TIRAMISU
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.embrace.android.embracesdk.Embrace.AppFramework
import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.assertions.assertInternalErrorLogged
import io.embrace.android.embracesdk.internal.ApkToolsConfig
import io.embrace.android.embracesdk.internal.TraceparentGeneratorTest.Companion.validPattern
import io.embrace.android.embracesdk.internalErrorService
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger
import io.embrace.android.embracesdk.recordSession
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand Down Expand Up @@ -139,23 +136,4 @@ internal class PublicApiTest {
}
}
}

@Test
fun `static logger and SDK logger instance log to the same internal error service`() {
with(testRule) {
embrace.start(harness.fakeCoreModule.context)
InternalStaticEmbraceLogger.logger.logError("not-used", RuntimeException("static"), true)
assertInternalErrorLogged(
checkNotNull(internalErrorService()).currentExceptionError,
checkNotNull(RuntimeException::class.qualifiedName),
"static"
)
harness.initModule.logger.logError("not-used", RuntimeException("non-static"), true)
assertInternalErrorLogged(
checkNotNull(internalErrorService()).currentExceptionError,
checkNotNull(RuntimeException::class.qualifiedName),
"non-static"
)
}
}
}

0 comments on commit 67efcdb

Please sign in to comment.