├── AFHTTPRequestOperationLogger.h ├── AFHTTPRequestOperationLogger.m ├── AFHTTPRequestOperationLogger.podspec ├── LICENSE └── README.md /AFHTTPRequestOperationLogger.h: -------------------------------------------------------------------------------- 1 | // AFHTTPRequestLogger.h 2 | // 3 | // Copyright (c) 2011 AFNetworking (http://afnetworking.com/) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | typedef NS_ENUM(NSUInteger, AFHTTPRequestLoggerLevel) { 26 | AFLoggerLevelOff, 27 | AFLoggerLevelDebug, 28 | AFLoggerLevelInfo, 29 | AFLoggerLevelWarn, 30 | AFLoggerLevelError, 31 | AFLoggerLevelFatal = AFLoggerLevelOff, 32 | }; 33 | 34 | /** 35 | `AFHTTPRequestOperationLogger` logs requests and responses made by AFNetworking, with an adjustable level of detail. 36 | 37 | Applications should enable the shared instance of `AFHTTPRequestOperationLogger` in `AppDelegate -application:didFinishLaunchingWithOptions:`: 38 | 39 | [[AFHTTPRequestOperationLogger sharedLogger] startLogging]; 40 | 41 | `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. 42 | */ 43 | @interface AFHTTPRequestOperationLogger : NSObject 44 | 45 | /** 46 | The level of logging detail. See "Logging Levels" for possible values. `AFLoggerLevelInfo` by default. 47 | */ 48 | @property (nonatomic, assign) AFHTTPRequestLoggerLevel level; 49 | 50 | /** 51 | Only log operations conforming to the specified predicate, if specified. `nil` by default. 52 | */ 53 | @property (nonatomic, strong) NSPredicate *filterPredicate; 54 | 55 | /** 56 | Returns the shared logger instance. 57 | */ 58 | + (instancetype)sharedLogger; 59 | 60 | /** 61 | Start logging requests and responses. 62 | */ 63 | - (void)startLogging; 64 | 65 | /** 66 | Stop logging requests and responses. 67 | */ 68 | - (void)stopLogging; 69 | 70 | @end 71 | 72 | ///---------------- 73 | /// @name Constants 74 | ///---------------- 75 | 76 | /** 77 | ## Logging Levels 78 | 79 | The following constants specify the available logging levels for `AFHTTPRequestOperationLogger`: 80 | 81 | enum { 82 | AFLoggerLevelOff, 83 | AFLoggerLevelDebug, 84 | AFLoggerLevelInfo, 85 | AFLoggerLevelWarn, 86 | AFLoggerLevelError, 87 | AFLoggerLevelFatal = AFLoggerLevelOff, 88 | } 89 | 90 | `AFLoggerLevelOff` 91 | Do not log requests or responses. 92 | 93 | `AFLoggerLevelDebug` 94 | Logs HTTP method, URL, header fields, & request body for requests, and status code, URL, header fields, response string, & elapsed time for responses. 95 | 96 | `AFLoggerLevelInfo` 97 | Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses. 98 | 99 | `AFLoggerLevelWarn` 100 | Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses, but only for failed requests. 101 | 102 | `AFLoggerLevelError` 103 | Equivalent to `AFLoggerLevelWarn` 104 | 105 | `AFLoggerLevelFatal` 106 | Equivalent to `AFLoggerLevelOff` 107 | */ 108 | -------------------------------------------------------------------------------- /AFHTTPRequestOperationLogger.m: -------------------------------------------------------------------------------- 1 | // AFHTTPRequestLogger.h 2 | // 3 | // Copyright (c) 2011 AFNetworking (http://afnetworking.com/) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "AFHTTPRequestOperationLogger.h" 24 | #import "AFHTTPRequestOperation.h" 25 | 26 | #import 27 | 28 | @implementation AFHTTPRequestOperationLogger 29 | 30 | + (instancetype)sharedLogger { 31 | static AFHTTPRequestOperationLogger *_sharedLogger = nil; 32 | 33 | static dispatch_once_t onceToken; 34 | dispatch_once(&onceToken, ^{ 35 | _sharedLogger = [[self alloc] init]; 36 | }); 37 | 38 | return _sharedLogger; 39 | } 40 | 41 | - (id)init { 42 | self = [super init]; 43 | if (!self) { 44 | return nil; 45 | } 46 | 47 | self.level = AFLoggerLevelInfo; 48 | 49 | return self; 50 | } 51 | 52 | - (void)dealloc { 53 | [self stopLogging]; 54 | } 55 | 56 | - (void)startLogging { 57 | [self stopLogging]; 58 | 59 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HTTPOperationDidStart:) name:AFNetworkingOperationDidStartNotification object:nil]; 60 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HTTPOperationDidFinish:) name:AFNetworkingOperationDidFinishNotification object:nil]; 61 | } 62 | 63 | - (void)stopLogging { 64 | [[NSNotificationCenter defaultCenter] removeObserver:self]; 65 | } 66 | 67 | #pragma mark - NSNotification 68 | 69 | static void * AFHTTPRequestOperationStartDate = &AFHTTPRequestOperationStartDate; 70 | 71 | - (void)HTTPOperationDidStart:(NSNotification *)notification { 72 | AFHTTPRequestOperation *operation = (AFHTTPRequestOperation *)[notification object]; 73 | 74 | if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) { 75 | return; 76 | } 77 | 78 | objc_setAssociatedObject(operation, AFHTTPRequestOperationStartDate, [NSDate date], OBJC_ASSOCIATION_RETAIN_NONATOMIC); 79 | 80 | if (self.filterPredicate && [self.filterPredicate evaluateWithObject:operation]) { 81 | return; 82 | } 83 | 84 | NSString *body = nil; 85 | if ([operation.request HTTPBody]) { 86 | body = [[NSString alloc] initWithData:[operation.request HTTPBody] encoding:NSUTF8StringEncoding]; 87 | } 88 | 89 | switch (self.level) { 90 | case AFLoggerLevelDebug: 91 | NSLog(@"%@ '%@': %@ %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], [operation.request allHTTPHeaderFields], body); 92 | break; 93 | case AFLoggerLevelInfo: 94 | NSLog(@"%@ '%@'", [operation.request HTTPMethod], [[operation.request URL] absoluteString]); 95 | break; 96 | default: 97 | break; 98 | } 99 | } 100 | 101 | - (void)HTTPOperationDidFinish:(NSNotification *)notification { 102 | AFHTTPRequestOperation *operation = (AFHTTPRequestOperation *)[notification object]; 103 | 104 | if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) { 105 | return; 106 | } 107 | 108 | if (self.filterPredicate && [self.filterPredicate evaluateWithObject:operation]) { 109 | return; 110 | } 111 | 112 | NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:objc_getAssociatedObject(operation, AFHTTPRequestOperationStartDate)]; 113 | 114 | if (operation.error) { 115 | switch (self.level) { 116 | case AFLoggerLevelDebug: 117 | case AFLoggerLevelInfo: 118 | case AFLoggerLevelWarn: 119 | case AFLoggerLevelError: 120 | NSLog(@"[Error] %@ '%@' (%ld) [%.04f s]: %@", [operation.request HTTPMethod], [[operation.response URL] absoluteString], (long)[operation.response statusCode], elapsedTime, operation.error); 121 | default: 122 | break; 123 | } 124 | } else { 125 | switch (self.level) { 126 | case AFLoggerLevelDebug: 127 | NSLog(@"%ld '%@' [%.04f s]: %@ %@", (long)[operation.response statusCode], [[operation.response URL] absoluteString], elapsedTime, [operation.response allHeaderFields], operation.responseString); 128 | break; 129 | case AFLoggerLevelInfo: 130 | NSLog(@"%ld '%@' [%.04f s]", (long)[operation.response statusCode], [[operation.response URL] absoluteString], elapsedTime); 131 | break; 132 | default: 133 | break; 134 | } 135 | } 136 | } 137 | 138 | @end 139 | -------------------------------------------------------------------------------- /AFHTTPRequestOperationLogger.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'AFHTTPRequestOperationLogger' 3 | s.version = '1.0.0' 4 | s.license = 'MIT' 5 | s.summary = 'AFNetworking Extension for HTTP Request Logging' 6 | s.homepage = 'https://github.com/AFNetworking/AFHTTPRequestOperationLogger' 7 | s.authors = { 'Mattt Thompson' => 'm@mattt.me' } 8 | s.source = { :git => 'https://github.com/AFNetworking/AFHTTPRequestOperationLogger.git', :tag => '1.0.0' } 9 | s.source_files = 'AFHTTPRequestOperationLogger.{h,m}' 10 | s.requires_arc = true 11 | 12 | s.dependency 'AFNetworking', '~> 1.0' 13 | end 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 AFNetworking (http://afnetworking.com/) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AFHTTPRequestOperationLogger 2 | 3 | `AFHTTPRequestOperationLogger` is an extension for [AFNetworking 1.x](http://github.com/AFNetworking/AFNetworking/) that logs HTTP requests as they are sent and received. 4 | 5 | ### If you are looking for an AFNetworking 2-compatible logger, check out [AFNetworkActivityLogger](https://github.com/AFNetworking/AFNetworkActivityLogger) 6 | 7 | > `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. 8 | 9 | ## Usage 10 | 11 | Add the following code to `AppDelegate.m -application:didFinishLaunchingWithOptions:`: 12 | 13 | ``` objective-c 14 | [[AFHTTPRequestOperationLogger sharedLogger] startLogging]; 15 | ``` 16 | 17 | Now all `AFHTTPRequestOperation` will have their request and response logged to the console, a la: 18 | 19 | ``` 20 | GET http://example.com/foo/bar.json 21 | 200 http://example.com/foo/bar.json 22 | ``` 23 | 24 | If the default logging level is too verbose—say, if you only want to know when requests fail, then changing it is as simple as: 25 | 26 | ``` objective-c 27 | [[AFHTTPRequestOperationLogger sharedLogger] setLevel:AFLoggerLevelError]; 28 | ``` 29 | 30 | ## Contact 31 | 32 | Mattt Thompson 33 | 34 | - http://github.com/mattt 35 | - http://twitter.com/mattt 36 | - m@mattt.me 37 | 38 | ## License 39 | 40 | AFHTTPRequestOperationLogger is available under the MIT license. See the LICENSE file for more info. 41 | --------------------------------------------------------------------------------