Skip to content

Commit 49772ca

Browse files
[SDK-2384] Add BranchLogLevel for setting minimum logging level (#1189)
* Add BranchLogLevel * Added verbose check in printQueue * Updated naming and public methods * Updated logLevel to loggingLevel
1 parent 2f842ac commit 49772ca

File tree

4 files changed

+61
-36
lines changed

4 files changed

+61
-36
lines changed

Branch-SDK-TestBed/src/main/java/io/branch/branchandroidtestbed/CustomBranchApp.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import io.branch.interfaces.IBranchLoggingCallbacks;
77
import io.branch.referral.Branch;
8+
import io.branch.referral.BranchLogger;
89

910
public final class CustomBranchApp extends Application {
1011
@Override
@@ -17,7 +18,7 @@ public void onBranchLog(String logMessage, String severityConstantName) {
1718
Log.v( "CustomTag", logMessage);
1819
}
1920
};
20-
Branch.enableLogging(); // Pass in iBranchLoggingCallbacks to enable logging redirects
21+
Branch.enableLogging(BranchLogger.BranchLogLevel.VERBOSE); // Pass in iBranchLoggingCallbacks to enable logging redirects
2122
Branch.getAutoInstance(this);
2223
}
2324
}

Branch-SDK/src/main/java/io/branch/referral/Branch.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,21 +1900,44 @@ private boolean pathMatch(String templatePath, String path) {
19001900
}
19011901

19021902
/**
1903-
* Enable Logging, independent of Debug Mode.
1903+
* Enable logging with a specific log level, independent of Debug Mode.
1904+
*
1905+
* @param iBranchLogging Optional interface to receive logging from the SDK.
1906+
* @param level The minimum log level for logging output.
1907+
*/
1908+
private static void enableLogging(IBranchLoggingCallbacks iBranchLogging, BranchLogger.BranchLogLevel level) {
1909+
BranchLogger.setLoggerCallback(iBranchLogging);
1910+
BranchLogger.setLoggingLevel(level);
1911+
BranchLogger.setLoggingEnabled(true);
1912+
BranchLogger.logAlways(GOOGLE_VERSION_TAG);
1913+
}
1914+
1915+
/**
1916+
* Enable Logging, independent of Debug Mode. Defaults to DEBUG level.
19041917
*/
19051918
public static void enableLogging() {
1906-
enableLogging(null);
1919+
enableLogging(null, BranchLogger.BranchLogLevel.DEBUG);
19071920
}
19081921

19091922
/**
1910-
* Optional interface. Implement a callback to receive logging from the SDK directly to your
1923+
* Enable Logging, independent of Debug Mode. Set to VERBOSE level.
1924+
* Implement a callback to receive logging from the SDK directly to your
19111925
* own logging solution. If null, and enabled, the default android.util.Log is used.
1912-
* @param iBranchLogging
1926+
*
1927+
* @param iBranchLogging Optional interface to receive logging from the SDK.
19131928
*/
1914-
public static void enableLogging(IBranchLoggingCallbacks iBranchLogging){
1915-
BranchLogger.setLoggerCallback(iBranchLogging);
1916-
BranchLogger.logAlways(GOOGLE_VERSION_TAG);
1917-
BranchLogger.setLoggingEnabled(true);
1929+
public static void enableLogging(IBranchLoggingCallbacks iBranchLogging) {
1930+
enableLogging(iBranchLogging, BranchLogger.BranchLogLevel.VERBOSE);
1931+
}
1932+
1933+
/**
1934+
* Enable logging with a specific log level.
1935+
*
1936+
* @param level The minimum log level for logging output.
1937+
*/
1938+
public static void enableLogging(BranchLogger.BranchLogLevel level) {
1939+
enableLogging(null, level);
1940+
19181941
}
19191942

19201943
/**

Branch-SDK/src/main/java/io/branch/referral/BranchLogger.kt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,32 @@ object BranchLogger {
1010

1111
private const val TAG = "BranchSDK"
1212

13+
enum class BranchLogLevel(val level: Int) {
14+
ERROR(1), WARN(2), INFO(3), DEBUG(4), VERBOSE(5)
15+
}
16+
17+
@JvmStatic
18+
var loggingLevel: BranchLogLevel = BranchLogLevel.DEBUG
19+
1320
@JvmStatic
1421
var loggingEnabled = false
1522

1623
@JvmStatic
1724
var loggerCallback: IBranchLoggingCallbacks? = null
1825

26+
private fun shouldLog(level: BranchLogLevel): Boolean = level.level <= loggingLevel.level
27+
1928
/**
2029
* <p>Creates a <b>Error</b> message in the debugger. If debugging is disabled, this will fail silently.</p>
2130
*
2231
* @param message A {@link String} value containing the debug message to record.
2332
*/
2433
@JvmStatic
2534
fun e(message: String) {
26-
if (loggingEnabled && message.isNotEmpty()) {
35+
if (loggingEnabled && shouldLog(BranchLogLevel.ERROR) && message.isNotEmpty()) {
2736
if (useCustomLogger()) {
2837
loggerCallback?.onBranchLog(message, "ERROR")
29-
}
30-
else {
38+
} else {
3139
Log.e(TAG, message)
3240
}
3341
}
@@ -40,11 +48,10 @@ object BranchLogger {
4048
*/
4149
@JvmStatic
4250
fun w(message: String) {
43-
if (loggingEnabled && message.isNotEmpty()) {
51+
if (loggingEnabled && shouldLog(BranchLogLevel.WARN) && message.isNotEmpty()) {
4452
if (useCustomLogger()) {
4553
loggerCallback?.onBranchLog(message, "WARN")
46-
}
47-
else {
54+
} else {
4855
Log.w(TAG, message)
4956
}
5057
}
@@ -57,11 +64,10 @@ object BranchLogger {
5764
*/
5865
@JvmStatic
5966
fun i(message: String) {
60-
if (loggingEnabled && message.isNotEmpty()) {
67+
if (loggingEnabled && shouldLog(BranchLogLevel.INFO) && message.isNotEmpty()) {
6168
if(useCustomLogger()) {
6269
loggerCallback?.onBranchLog(message, "INFO")
63-
}
64-
else {
70+
} else {
6571
Log.i(TAG, message)
6672
}
6773
}
@@ -74,11 +80,10 @@ object BranchLogger {
7480
*/
7581
@JvmStatic
7682
fun d(message: String?) {
77-
if (loggingEnabled && message?.isNotEmpty() == true) {
83+
if (loggingEnabled && shouldLog(BranchLogLevel.DEBUG) && message?.isNotEmpty() == true) {
7884
if (useCustomLogger()) {
7985
loggerCallback?.onBranchLog(message, "DEBUG")
80-
}
81-
else {
86+
} else {
8287
Log.d(TAG, message)
8388
}
8489
}
@@ -91,11 +96,10 @@ object BranchLogger {
9196
*/
9297
@JvmStatic
9398
fun v(message: String) {
94-
if (loggingEnabled && message.isNotEmpty()) {
99+
if (loggingEnabled && shouldLog(BranchLogLevel.VERBOSE) && message.isNotEmpty()) {
95100
if (useCustomLogger()) {
96101
loggerCallback?.onBranchLog(message, "VERBOSE")
97-
}
98-
else {
102+
} else {
99103
Log.v(TAG, message)
100104
}
101105
}
@@ -106,8 +110,7 @@ object BranchLogger {
106110
if (message.isNotEmpty()) {
107111
if (useCustomLogger()) {
108112
loggerCallback?.onBranchLog(message, "INFO")
109-
}
110-
else {
113+
} else {
111114
Log.i(TAG, message)
112115
}
113116
}

Branch-SDK/src/main/java/io/branch/referral/ServerRequestQueue.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@
77
import android.content.SharedPreferences;
88
import android.os.Handler;
99
import android.os.Looper;
10-
import android.text.TextUtils;
1110

1211
import androidx.annotation.Nullable;
1312

1413
import org.json.JSONArray;
1514
import org.json.JSONException;
1615
import org.json.JSONObject;
1716

18-
import java.util.ArrayList;
19-
import java.util.Arrays;
2017
import java.util.Collections;
21-
import java.util.ConcurrentModificationException;
2218
import java.util.HashMap;
23-
import java.util.Iterator;
2419
import java.util.LinkedList;
2520
import java.util.List;
2621
import java.util.NoSuchElementException;
@@ -183,12 +178,15 @@ ServerRequest peek() {
183178
}
184179

185180
public void printQueue(){
186-
synchronized (reqQueueLockObject){
187-
StringBuilder stringBuilder = new StringBuilder();
188-
for(int i = 0; i < queue.size(); i++){
189-
stringBuilder.append(queue.get(i)).append(" with locks ").append(queue.get(i).printWaitLocks()).append("\n");
181+
// Only print the queue if the log level is verbose
182+
if (BranchLogger.getLoggingLevel().getLevel() == BranchLogger.BranchLogLevel.VERBOSE.getLevel()) {
183+
synchronized (reqQueueLockObject) {
184+
StringBuilder stringBuilder = new StringBuilder();
185+
for (int i = 0; i < queue.size(); i++) {
186+
stringBuilder.append(queue.get(i)).append(" with locks ").append(queue.get(i).printWaitLocks()).append("\n");
187+
}
188+
BranchLogger.v("Queue is: " + stringBuilder);
190189
}
191-
BranchLogger.v("Queue is: " + stringBuilder);
192190
}
193191
}
194192

0 commit comments

Comments
 (0)