Skip to content
This repository was archived by the owner on Jan 17, 2023. It is now read-only.

add delegate for logging #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 24 additions & 2 deletions AFHTTPRequestOperationLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,32 @@ typedef NS_ENUM(NSUInteger, AFHTTPRequestLoggerLevel) {
AFLoggerLevelFatal = AFLoggerLevelOff,
};


/**
Protocol for delegate to be informed of AFHTTPRequestOperation status. Must implement method -(void)AFHTTPRequestOperationLoggerLog:(NSString*)logString;
*/

@protocol AFHTTPRequestOperationLoggerDelegate

/**
Method called on delegate whenever a request operation should be logged.
@param logString the String that will be logged, content depends on AFHTTPRequestLoggerLevel
*/

@required -(void)AFHTTPRequestOperationLoggerLog:(NSString*)logString;

@end

/**
`AFHTTPRequestOperationLogger` logs requests and responses made by AFNetworking, with an adjustable level of detail.

Applications should enable the shared instance of `AFHTTPRequestOperationLogger` in `AppDelegate -application:didFinishLaunchingWithOptions:`:

[[AFHTTPRequestOperationLogger sharedLogger] startLogging];
[[AFHTTPRequestOperationLogger sharedLogger] startLogging];

`AFHTTPRequestOperationLogger` listens for `AFNetworkingOperationDidStartNotification` and `AFNetworkingOperationDidFinishNotification` notifications, which are posted by AFNetworking as request operations are started and finish. For further customization of logging output, users are encouraged to implement desired functionality by listening for these notifications.
*/

@interface AFHTTPRequestOperationLogger : NSObject

/**
Expand All @@ -52,6 +69,11 @@ typedef NS_ENUM(NSUInteger, AFHTTPRequestLoggerLevel) {
*/
@property (nonatomic, strong) NSPredicate *filterPredicate;

/**
Set a delegate to inform instead of logging to console. `nil` by default.
*/
@property (nonatomic, strong) id<AFHTTPRequestOperationLoggerDelegate> delegate;

/**
Returns the shared logger instance.
*/
Expand Down
18 changes: 12 additions & 6 deletions AFHTTPRequestOperationLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

@implementation AFHTTPRequestOperationLogger

@synthesize delegate = _delegate;

+ (instancetype)sharedLogger {
static AFHTTPRequestOperationLogger *_sharedLogger = nil;

Expand Down Expand Up @@ -85,17 +87,18 @@ - (void)HTTPOperationDidStart:(NSNotification *)notification {
if ([operation.request HTTPBody]) {
body = [[NSString alloc] initWithData:[operation.request HTTPBody] encoding:NSUTF8StringEncoding];
}

NSString* logString;
switch (self.level) {
case AFLoggerLevelDebug:
NSLog(@"%@ '%@': %@ %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], [operation.request allHTTPHeaderFields], body);
logString = [NSString stringWithFormat:@"%@ '%@': %@ %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], [operation.request allHTTPHeaderFields], body];
break;
case AFLoggerLevelInfo:
NSLog(@"%@ '%@'", [operation.request HTTPMethod], [[operation.request URL] absoluteString]);
logString = [NSString stringWithFormat:@"%@ '%@'", [operation.request HTTPMethod], [[operation.request URL] absoluteString]];
break;
default:
break;
}
if (!_delegate) NSLog(@"%@", logString); else [_delegate AFHTTPRequestOperationLoggerLog:logString];
}

- (void)HTTPOperationDidFinish:(NSNotification *)notification {
Expand All @@ -111,28 +114,31 @@ - (void)HTTPOperationDidFinish:(NSNotification *)notification {

NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:objc_getAssociatedObject(operation, AFHTTPRequestOperationStartDate)];

NSString* logString;

if (operation.error) {
switch (self.level) {
case AFLoggerLevelDebug:
case AFLoggerLevelInfo:
case AFLoggerLevelWarn:
case AFLoggerLevelError:
NSLog(@"[Error] %@ '%@' (%ld) [%.04f s]: %@", [operation.request HTTPMethod], [[operation.response URL] absoluteString], (long)[operation.response statusCode], elapsedTime, operation.error);
logString = [NSString stringWithFormat:@"[Error] %@ '%@' (%ld) [%.04f s]: %@", [operation.request HTTPMethod], [[operation.response URL] absoluteString], (long)[operation.response statusCode], elapsedTime, operation.error];
default:
break;
}
} else {
switch (self.level) {
case AFLoggerLevelDebug:
NSLog(@"%ld '%@' [%.04f s]: %@ %@", (long)[operation.response statusCode], [[operation.response URL] absoluteString], elapsedTime, [operation.response allHeaderFields], operation.responseString);
logString = [NSString stringWithFormat:@"%ld '%@' [%.04f s]: %@ %@", (long)[operation.response statusCode], [[operation.response URL] absoluteString], elapsedTime, [operation.response allHeaderFields], operation.responseString];
break;
case AFLoggerLevelInfo:
NSLog(@"%ld '%@' [%.04f s]", (long)[operation.response statusCode], [[operation.response URL] absoluteString], elapsedTime);
logString = [NSString stringWithFormat:@"%ld '%@' [%.04f s]", (long)[operation.response statusCode], [[operation.response URL] absoluteString], elapsedTime];
break;
default:
break;
}
}
if (!_delegate) NSLog(@"%@", logString); else [_delegate AFHTTPRequestOperationLoggerLog:logString];
}

@end