Skip to content

Commit

Permalink
Add BranchLogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
nsingh-branch committed Apr 19, 2024
1 parent 2f842ac commit a0e6fa3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
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);
}

/**
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

@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

0 comments on commit a0e6fa3

Please sign in to comment.