Skip to content

Commit

Permalink
Merge pull request #679 from linean/feature/allow-user-to-provide-cus…
Browse files Browse the repository at this point in the history
…tom-logger

Allow users to provide custom logging logic
  • Loading branch information
jsligh authored Nov 16, 2023
2 parents d1af151 + e29f40e commit f6f56d6
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.prebid.mobile;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Size;

public class LogUtil {
Expand All @@ -32,6 +34,9 @@ public class LogUtil {

private static int logLevel;

@NonNull
private static PrebidLogger logger = new LogCatLogger();

private LogUtil() {
}

Expand All @@ -40,6 +45,10 @@ public static void setLogLevel(int level) {
logLevel = level;
}

public static void setLogger(@NonNull PrebidLogger newLogger) {
logger = newLogger;
}

public static int getLogLevel() {
return logLevel;
}
Expand Down Expand Up @@ -130,7 +139,7 @@ public static void error(final String tag, String message, Throwable throwable)
}

if (ERROR >= getLogLevel()) {
Log.e(getTagWithBase(tag), message, throwable);
logger.e(getTagWithBase(tag), message, throwable);
}
}

Expand All @@ -143,7 +152,7 @@ private static void print(int messagePriority, String tag, String message) {
}

if (messagePriority >= getLogLevel()) {
Log.println(messagePriority, getTagWithBase(tag), message);
logger.println(messagePriority, getTagWithBase(tag), message);
}
}

Expand All @@ -167,4 +176,23 @@ private static String getTagWithBase(String tag) {
}
}

public interface PrebidLogger {

void println(int messagePriority, String tag, String message);

void e(final String tag, String message, Throwable throwable);
}

private static class LogCatLogger implements PrebidLogger {

@Override
public void println(int messagePriority, String tag, String message) {
Log.println(messagePriority, tag, message);
}

@Override
public void e(String tag, String message, Throwable throwable) {
Log.e(tag, message, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.LogUtil.PrebidLogger;
import org.prebid.mobile.api.data.InitializationStatus;
import org.prebid.mobile.api.rendering.pluginrenderer.PrebidMobilePluginRegister;
import org.prebid.mobile.api.rendering.pluginrenderer.PrebidMobilePluginRenderer;
Expand Down Expand Up @@ -99,7 +100,8 @@ public class PrebidMobile {
*/
@Deprecated
public static LogLevel logLevel = LogLevel.NONE;

@Nullable
private static PrebidLogger customLogger = null;

private static boolean pbsDebug = false;
private static boolean shareGeoLocation = false;
Expand Down Expand Up @@ -314,6 +316,15 @@ public static void setLogLevel(LogLevel logLevel) {
PrebidMobile.logLevel = logLevel;
}

@Nullable
public static PrebidLogger getCustomLogger() {
return PrebidMobile.customLogger;
}

public static void setCustomLogger(@NonNull PrebidLogger logger) {
PrebidMobile.customLogger = logger;
}

/**
* Check Google Mobile Ads compatibility for original API.
* Show logs if version is not compatible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.annotation.VisibleForTesting;

import org.prebid.mobile.LogUtil;
import org.prebid.mobile.LogUtil.PrebidLogger;
import org.prebid.mobile.PrebidMobile;
import org.prebid.mobile.api.rendering.PrebidRenderer;
import org.prebid.mobile.rendering.listeners.SdkInitializationListener;
Expand Down Expand Up @@ -46,6 +47,11 @@ public static void init(
LogUtil.setLogLevel(PrebidMobile.getLogLevel().getValue());
}

PrebidLogger customLogger = PrebidMobile.getCustomLogger();
if (customLogger != null) {
LogUtil.setLogger(customLogger);
}

try {
PrebidMobile.registerPluginRenderer(new PrebidRenderer());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.prebid.mobile;

import static org.prebid.mobile.LogUtil.DEBUG;
import static org.prebid.mobile.LogUtil.ERROR;
import static org.prebid.mobile.LogUtil.INFO;
import static org.prebid.mobile.LogUtil.VERBOSE;

import org.junit.Before;
import org.junit.Test;
import org.prebid.mobile.testutils.TestLogger;

public class LogUtilTest {

private final String DEFAULT_TAG = "PrebidMobile";
private final TestLogger testLogger = new TestLogger();

@Before
public void setup() {
LogUtil.setLogLevel(VERBOSE);
LogUtil.setLogger(testLogger);
}

@Test
public void testErrorLogWithMessage() {
String testMessage = "Test error message";

LogUtil.error(testMessage);

testLogger.assertLogPrinted(ERROR, DEFAULT_TAG, testMessage);
}

@Test
public void testErrorLogWithMessageAndTag() {
String testTag = "Test tag";
String testMessage = "Test error message";

LogUtil.error(testTag, testMessage);

testLogger.assertLogPrinted(ERROR, testTag, testMessage);
}

@Test
public void testErrorLogWithMessageTagAndThrowable() {
String testTag = "Test tag";
String testMessage = "Test error message";
Throwable testThrowable = new Throwable("Test throwable");

LogUtil.error(testTag, testMessage, testThrowable);

testLogger.assertErrorLogged(testTag, testMessage, testThrowable);
}

@Test
public void testVerboseLogWithMessage() {
String testMessage = "Test error message";

LogUtil.verbose(testMessage);

testLogger.assertLogPrinted(VERBOSE, DEFAULT_TAG, testMessage);
}

@Test
public void testVerboseLogWithMessageAndTag() {
String testTag = "Test tag";
String testMessage = "Test error message";

LogUtil.verbose(testTag, testMessage);

testLogger.assertLogPrinted(VERBOSE, testTag, testMessage);
}

@Test
public void testInfoLogWithMessage() {
String testMessage = "Test error message";

LogUtil.info(testMessage);

testLogger.assertLogPrinted(INFO, DEFAULT_TAG, testMessage);
}

@Test
public void testInfoLogWithMessageAndTag() {
String testTag = "Test tag";
String testMessage = "Test error message";

LogUtil.info(testTag, testMessage);

testLogger.assertLogPrinted(INFO, testTag, testMessage);
}

@Test
public void testWarningLogWithMessage() {
String testMessage = "Test error message";

LogUtil.warning(testMessage);

testLogger.assertLogPrinted(INFO, DEFAULT_TAG, testMessage);
}

@Test
public void testWarningLogWithMessageAndTag() {
String testTag = "Test tag";
String testMessage = "Test error message";

LogUtil.warning(testTag, testMessage);

testLogger.assertLogPrinted(INFO, testTag, testMessage);
}

@Test
public void testDebugLogWithMessage() {
String testMessage = "Test error message";

LogUtil.warning(testMessage);

testLogger.assertLogPrinted(DEBUG, DEFAULT_TAG, testMessage);
}

@Test
public void testDebugLogWithMessageAndTag() {
String testTag = "Test tag";
String testMessage = "Test error message";

LogUtil.warning(testTag, testMessage);

testLogger.assertLogPrinted(DEBUG, testTag, testMessage);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.prebid.mobile.testutils;

import static org.junit.Assert.assertNotNull;

import org.jetbrains.annotations.NotNull;
import org.prebid.mobile.LogUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import kotlin.jvm.internal.Intrinsics;


public final class TestLogger implements LogUtil.PrebidLogger {

private final List<PrintLog> printLogs = new ArrayList<>();
private final List<ErrorLog> errorLogs = new ArrayList<>();

public void println(int messagePriority, @NotNull String tag, @NotNull String message) {
printLogs.add(new PrintLog(messagePriority, tag, message));
}

public void e(@NotNull String tag, @NotNull String message, @NotNull Throwable throwable) {
errorLogs.add(new ErrorLog(tag, message, throwable));
}

public void assertErrorLogged(@NotNull String tag, @NotNull String message, @NotNull Throwable throwable) {
ErrorLog expectedLog = new ErrorLog(tag, message, throwable);
Iterator<ErrorLog> iterator = errorLogs.iterator();

ErrorLog actualLog = null;
while (iterator.hasNext()) {
actualLog = iterator.next();
if (!Intrinsics.areEqual(expectedLog, actualLog)) {
continue;
}
break;
}

assertNotNull("Expected error log not captured. Recorded logs: " + this.errorLogs, actualLog);
}

public void assertLogPrinted(int messagePriority, @NotNull String tag, @NotNull String message) {
PrintLog expectedLog = new PrintLog(messagePriority, tag, message);
Iterator<PrintLog> iterator = printLogs.iterator();

PrintLog actualLog = null;
while (iterator.hasNext()) {
actualLog = iterator.next();
if (!Intrinsics.areEqual(expectedLog, actualLog)) {
continue;
}
break;
}

assertNotNull("Expected log not captured. Recorded logs: " + this.printLogs, actualLog);
}

private static final class ErrorLog {
@NotNull
private final String tag;
@NotNull
private final String message;
@NotNull
private final Throwable throwable;

public ErrorLog(@NotNull String tag, @NotNull String message, @NotNull Throwable throwable) {
this.tag = tag;
this.message = message;
this.throwable = throwable;
}

@NotNull
public String toString() {
return "ErrorLog(tag=" + this.tag + ", message=" + this.message + ", throwable=" + this.throwable + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ErrorLog errorLog = (ErrorLog) o;
return tag.equals(errorLog.tag) && message.equals(errorLog.message) && throwable.equals(errorLog.throwable);
}

@Override
public int hashCode() {
return Objects.hash(tag, message, throwable);
}
}

private static final class PrintLog {
private final int messagePriority;
@NotNull
private final String tag;
@NotNull
private final String message;


public PrintLog(int messagePriority, @NotNull String tag, @NotNull String message) {
this.messagePriority = messagePriority;
this.tag = tag;
this.message = message;
}

@NotNull
public String toString() {
return "PrintLog(messagePriority=" + this.messagePriority + ", tag=" + this.tag + ", message=" + this.message + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PrintLog printLog = (PrintLog) o;
return messagePriority == printLog.messagePriority && tag.equals(printLog.tag) && message.equals(printLog.message);
}

@Override
public int hashCode() {
return Objects.hash(messagePriority, tag, message);
}
}
}

0 comments on commit f6f56d6

Please sign in to comment.