Skip to content

Commit

Permalink
[SDK-2384] Add BranchLogLevel for setting minimum logging level (#1189)
Browse files Browse the repository at this point in the history
* Add BranchLogLevel

* Added verbose check in printQueue

* Updated naming and public methods

* Updated logLevel to loggingLevel
  • Loading branch information
nsingh-branch authored Apr 24, 2024
1 parent 2f842ac commit 49772ca
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.branch.interfaces.IBranchLoggingCallbacks;
import io.branch.referral.Branch;
import io.branch.referral.BranchLogger;

public final class CustomBranchApp extends Application {
@Override
Expand All @@ -17,7 +18,7 @@ public void onBranchLog(String logMessage, String severityConstantName) {
Log.v( "CustomTag", logMessage);
}
};
Branch.enableLogging(); // Pass in iBranchLoggingCallbacks to enable logging redirects
Branch.enableLogging(BranchLogger.BranchLogLevel.VERBOSE); // Pass in iBranchLoggingCallbacks to enable logging redirects
Branch.getAutoInstance(this);
}
}
39 changes: 31 additions & 8 deletions Branch-SDK/src/main/java/io/branch/referral/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -1900,21 +1900,44 @@ private boolean pathMatch(String templatePath, String path) {
}

/**
* Enable Logging, independent of Debug Mode.
* Enable logging with a specific log level, independent of Debug Mode.
*
* @param iBranchLogging Optional interface to receive logging from the SDK.
* @param level The minimum log level for logging output.
*/
private static void enableLogging(IBranchLoggingCallbacks iBranchLogging, BranchLogger.BranchLogLevel level) {
BranchLogger.setLoggerCallback(iBranchLogging);
BranchLogger.setLoggingLevel(level);
BranchLogger.setLoggingEnabled(true);
BranchLogger.logAlways(GOOGLE_VERSION_TAG);
}

/**
* Enable Logging, independent of Debug Mode. Defaults to DEBUG level.
*/
public static void enableLogging() {
enableLogging(null);
enableLogging(null, BranchLogger.BranchLogLevel.DEBUG);
}

/**
* Optional interface. Implement a callback to receive logging from the SDK directly to your
* Enable Logging, independent of Debug Mode. Set to VERBOSE level.
* Implement a callback to receive logging from the SDK directly to your
* own logging solution. If null, and enabled, the default android.util.Log is used.
* @param iBranchLogging
*
* @param iBranchLogging Optional interface to receive logging from the SDK.
*/
public static void enableLogging(IBranchLoggingCallbacks iBranchLogging){
BranchLogger.setLoggerCallback(iBranchLogging);
BranchLogger.logAlways(GOOGLE_VERSION_TAG);
BranchLogger.setLoggingEnabled(true);
public static void enableLogging(IBranchLoggingCallbacks iBranchLogging) {
enableLogging(iBranchLogging, BranchLogger.BranchLogLevel.VERBOSE);
}

/**
* Enable logging with a specific log level.
*
* @param level The minimum log level for logging output.
*/
public static void enableLogging(BranchLogger.BranchLogLevel level) {
enableLogging(null, level);

}

/**
Expand Down
37 changes: 20 additions & 17 deletions Branch-SDK/src/main/java/io/branch/referral/BranchLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ object BranchLogger {

private const val TAG = "BranchSDK"

enum class BranchLogLevel(val level: Int) {
ERROR(1), WARN(2), INFO(3), DEBUG(4), VERBOSE(5)
}

@JvmStatic
var loggingLevel: BranchLogLevel = BranchLogLevel.DEBUG

@JvmStatic
var loggingEnabled = false

@JvmStatic
var loggerCallback: IBranchLoggingCallbacks? = null

private fun shouldLog(level: BranchLogLevel): Boolean = level.level <= loggingLevel.level

/**
* <p>Creates a <b>Error</b> message in the debugger. If debugging is disabled, this will fail silently.</p>
*
* @param message A {@link String} value containing the debug message to record.
*/
@JvmStatic
fun e(message: String) {
if (loggingEnabled && message.isNotEmpty()) {
if (loggingEnabled && shouldLog(BranchLogLevel.ERROR) && message.isNotEmpty()) {
if (useCustomLogger()) {
loggerCallback?.onBranchLog(message, "ERROR")
}
else {
} else {
Log.e(TAG, message)
}
}
Expand All @@ -40,11 +48,10 @@ object BranchLogger {
*/
@JvmStatic
fun w(message: String) {
if (loggingEnabled && message.isNotEmpty()) {
if (loggingEnabled && shouldLog(BranchLogLevel.WARN) && message.isNotEmpty()) {
if (useCustomLogger()) {
loggerCallback?.onBranchLog(message, "WARN")
}
else {
} else {
Log.w(TAG, message)
}
}
Expand All @@ -57,11 +64,10 @@ object BranchLogger {
*/
@JvmStatic
fun i(message: String) {
if (loggingEnabled && message.isNotEmpty()) {
if (loggingEnabled && shouldLog(BranchLogLevel.INFO) && message.isNotEmpty()) {
if(useCustomLogger()) {
loggerCallback?.onBranchLog(message, "INFO")
}
else {
} else {
Log.i(TAG, message)
}
}
Expand All @@ -74,11 +80,10 @@ object BranchLogger {
*/
@JvmStatic
fun d(message: String?) {
if (loggingEnabled && message?.isNotEmpty() == true) {
if (loggingEnabled && shouldLog(BranchLogLevel.DEBUG) && message?.isNotEmpty() == true) {
if (useCustomLogger()) {
loggerCallback?.onBranchLog(message, "DEBUG")
}
else {
} else {
Log.d(TAG, message)
}
}
Expand All @@ -91,11 +96,10 @@ object BranchLogger {
*/
@JvmStatic
fun v(message: String) {
if (loggingEnabled && message.isNotEmpty()) {
if (loggingEnabled && shouldLog(BranchLogLevel.VERBOSE) && message.isNotEmpty()) {
if (useCustomLogger()) {
loggerCallback?.onBranchLog(message, "VERBOSE")
}
else {
} else {
Log.v(TAG, message)
}
}
Expand All @@ -106,8 +110,7 @@ object BranchLogger {
if (message.isNotEmpty()) {
if (useCustomLogger()) {
loggerCallback?.onBranchLog(message, "INFO")
}
else {
} else {
Log.i(TAG, message)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -183,12 +178,15 @@ ServerRequest peek() {
}

public void printQueue(){
synchronized (reqQueueLockObject){
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < queue.size(); i++){
stringBuilder.append(queue.get(i)).append(" with locks ").append(queue.get(i).printWaitLocks()).append("\n");
// Only print the queue if the log level is verbose
if (BranchLogger.getLoggingLevel().getLevel() == BranchLogger.BranchLogLevel.VERBOSE.getLevel()) {
synchronized (reqQueueLockObject) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < queue.size(); i++) {
stringBuilder.append(queue.get(i)).append(" with locks ").append(queue.get(i).printWaitLocks()).append("\n");
}
BranchLogger.v("Queue is: " + stringBuilder);
}
BranchLogger.v("Queue is: " + stringBuilder);
}
}

Expand Down

0 comments on commit 49772ca

Please sign in to comment.