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-2527] Add request & response objects to the BranchLogCallback #1458

Merged
merged 4 commits into from
Dec 11, 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
12 changes: 10 additions & 2 deletions Branch-TestBed/Branch-TestBed/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ - (BOOL)application:(UIApplication *)application
// test pre init support
//[self testDispatchToIsolationQueue:branch]


[Branch enableLoggingAtLevel:BranchLogLevelVerbose withCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
[Branch enableLoggingAtLevel:BranchLogLevelVerbose withCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) {
nsingh-branch marked this conversation as resolved.
Show resolved Hide resolved
// Handle the log message and error here. For example, printing to the console:
if (error) {
NSLog(@"[BranchLog] Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription);
} else {
NSLog(@"[BranchLog] Level: %lu, Message: %@", (unsigned long)logLevel, message);
}

if (request) {
NSString *jsonString = [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding];
nsingh-branch marked this conversation as resolved.
Show resolved Hide resolved
NSLog(@"[BranchLog] Got %@ Request: %@", request.URL , jsonString);
}

if (response) {
NSLog(@"[BranchLog] Got Response for request(%@): %@", response.requestId, response.data);
}

NSString *logEntry = error ? [NSString stringWithFormat:@"Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription]
: [NSString stringWithFormat:@"Level: %lu, Message: %@", (unsigned long)logLevel, message];
APPLogHookFunction([NSDate date], logLevel, logEntry);
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCServerInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ - (NSURLRequest *)preparePostRequest:(NSDictionary *)params url:(NSString *)url
[request setHTTPBody:postData];

if ([[BranchLogger shared] shouldLog:BranchLogLevelDebug]) {
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nHeaders %@\nBody %@", request, [request allHTTPHeaderFields], [BNCEncodingUtils prettyPrintJSON:updatedParams]] error:nil];
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nHeaders %@\nBody %@", request, [request allHTTPHeaderFields], [BNCEncodingUtils prettyPrintJSON:updatedParams]] error:nil request:request response:nil];
}

return request;
Expand All @@ -310,7 +310,7 @@ - (BNCServerResponse *)processServerResponse:(NSURLResponse *)response data:(NSD
serverResponse.requestId = requestId;

if ([[BranchLogger shared] shouldLog:BranchLogLevelDebug]) {
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nBody %@", response, [BNCEncodingUtils prettyPrintJSON:serverResponse.data]] error:nil];
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nBody %@", response, [BNCEncodingUtils prettyPrintJSON:serverResponse.data]] error:nil request:nil response:serverResponse];
}

} else {
Expand Down
23 changes: 15 additions & 8 deletions Sources/BranchSDK/BranchLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ - (instancetype)init {
_includeCallerDetails = YES;

// default callback sends logs to os_log
_logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
_logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) {
NSString *formattedMessage = [BranchLogger formatMessage:message logLevel:logLevel error:error];

os_log_t log = os_log_create("io.branch.sdk", "BranchSDK");
Expand Down Expand Up @@ -53,22 +53,29 @@ - (void)disableCallerDetails {
}

- (void)logError:(NSString *)message error:(NSError *_Nullable)error {
[self logMessage:message withLevel:BranchLogLevelError error:error];
[self logMessage:message withLevel:BranchLogLevelError error:error request:nil response:nil];
}

- (void)logWarning:(NSString *)message error:(NSError *_Nullable)error {
[self logMessage:message withLevel:BranchLogLevelWarning error:error];
[self logMessage:message withLevel:BranchLogLevelWarning error:error request:nil response:nil];
}

- (void)logDebug:(NSString *)message error:(NSError *_Nullable)error {
[self logMessage:message withLevel:BranchLogLevelDebug error:error];
- (void)logDebug:(NSString * _Nonnull)message error:(NSError * _Nullable)error {
[self logDebug:message error:error request:nil response:nil];
}

- (void)logDebug:(NSString * _Nonnull)message
error:(NSError * _Nullable)error
request:(NSMutableURLRequest * _Nullable)request
response:(BNCServerResponse * _Nullable)response {
[self logMessage:message withLevel:BranchLogLevelDebug error:error request:request response:response];
}

- (void)logVerbose:(NSString *)message error:(NSError *_Nullable)error {
[self logMessage:message withLevel:BranchLogLevelVerbose error:error];
[self logMessage:message withLevel:BranchLogLevelVerbose error:error request:nil response:nil];
}

- (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NSError *_Nullable)error {
- (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NSError *_Nullable)error request:(NSMutableURLRequest * _Nullable)request response:(BNCServerResponse * _Nullable)response {
if (!self.loggingEnabled || level < self.logLevelThreshold || message.length == 0) {
return;
}
Expand All @@ -79,7 +86,7 @@ - (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NS
}

if (self.logCallback) {
self.logCallback(formattedMessage, level, error);
self.logCallback(formattedMessage, level, error, request, response);
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/BranchSDK/Public/BranchLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <Foundation/Foundation.h>
#import "BNCServerResponse.h"

typedef NS_ENUM(NSUInteger, BranchLogLevel) {
BranchLogLevelVerbose, // development
Expand All @@ -15,7 +16,7 @@ typedef NS_ENUM(NSUInteger, BranchLogLevel) {
BranchLogLevelError, // severe errors. SDK is probably in a bad state.
};

typedef void(^BranchLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error);
typedef void(^BranchLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response);

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -37,6 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)logError:(NSString * _Nonnull)message error:(NSError * _Nullable)error;
- (void)logWarning:(NSString * _Nonnull)message error:(NSError * _Nullable)error;
- (void)logDebug:(NSString * _Nonnull)message error:(NSError * _Nullable)error;
- (void)logDebug:(NSString * _Nonnull)message error:(NSError * _Nullable)error request:(NSMutableURLRequest * _Nullable)request response:(BNCServerResponse * _Nullable)response;
- (void)logVerbose:(NSString * _Nonnull)message error:(NSError * _Nullable)error;

// default Branch log format
Expand Down
Loading