Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-2384] Add BranchLogLevel for setting minimum logging level #1189

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 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,34 @@ 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.
*/
public static void enableLogging(IBranchLoggingCallbacks iBranchLogging, BranchLogger.BranchLogLevel level) {
BranchLogger.setLoggerCallback(iBranchLogging);
BranchLogger.setMinimumLogLevel(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. Defaults to DEBUG 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.DEBUG);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean customers using this feature will no longer produce logs for VERBOSE by default? This will break those workflows.

If it's not set, the original behavior should be maintained.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also break our own automation tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated it to set logs to Verbose when using a callback.

}

/**
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 minimumLogLevel: BranchLogLevel = BranchLogLevel.DEBUG
nsingh-branch marked this conversation as resolved.
Show resolved Hide resolved

@JvmStatic
var loggingEnabled = false

@JvmStatic
var loggerCallback: IBranchLoggingCallbacks? = null

private fun shouldLog(level: BranchLogLevel): Boolean = level.level <= minimumLogLevel.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 @@ -183,12 +183,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.getMinimumLogLevel().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
Loading