Network Activity logger for AFNetworking 3.0
December 23, 2016
Log all of your iOS project network activity to your console.
If you are an iOS developer (or Any mobile developer), you might be in a situation where you want to debug the network requests and responses for your app and figure out how exactly the app dealing with web services.
With the help of AFNetworkActivityLogger we can log all the details about the network operations to xCode console.
Install Cocoapods if you don’t have already with the following command
gem install cocoapods
Note: CocoaPods version 0.39.0+ is required.
To integrate AFNetworkActivityLogger into your xCode project using Cocoapods, specify it in your Podfile
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' target 'TargetName' do pod 'AFNetworkActivityLogger' end
Above pod file integration will take the latest version from Github repo. Unfortunately the latest version only works with AFNetworking v2.0. Incase if you want to use it for AFNetworking v3.0 edit the pod file as below.
pod ‘AFNetworkActivityLogger’, :git => ‘https://github.com/AFNetworking/AFNetworkActivityLogger.git', :branch => ‘3_0_0’
Above line will take the latest version from Git branch 300 which can be used with AFNetworking v3.0
Add the following code to
[[AFNetworkActivityLogger sharedLogger] startLogging];
Ta-Da! now all your network activities will be logged in your Xcode console. By default log level is set as INFO, but incase if you want to see more logs follow the below lines.
AFNetworkActivityConsoleLogger *logger = [AFNetworkActivityLogger sharedLogger].loggers.anyObject; logger.level = AFLoggerLevelDebug; [[AFNetworkActivityLogger sharedLogger] startLogging];
Supported log levels are
AFLoggerLevelOff: Do not log requests or responses.
AFLoggerLevelDebug:Logs HTTP method, URL, header fields, & request body for requests, and status code, URL, header fields, response string, & elapsed time for responses.
AFLoggerLevelInfo: Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses.
AFLoggerLevelError: Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses, but only for failed requests.
If you are using version 2.0 follow the below code.
[[AFNetworkActivityLogger sharedLogger] startLogging]; [[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelError];