• This repository has been archived on 17/Jan/2023
  • Stars
    star
    560
  • Rank 79,541 (Top 2 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

AFNetworking 3.0 Extension for Network Request Logging

AFNetworkActivityLogger

AFNetworkActivityLogger is an extension for AFNetworking 3.0 that logs network requests as they are sent and received.

AFNetworkActivityLogger listens AFNetworkingTaskDidStartNotification and AFNetworkingTaskDidFinishNotification notifications, which are posted by AFNetworking as session tasks are started and finish. For further customization of logging output, users are encouraged to implement desired functionality by creating new objects that conform to AFNetworkActivityLoggerProtocol.

2.x -> 3.x Migration

3.0.0 featured the following breaking API changes:

  • The log level property is now found on the individual unique loggers, rather than the shared logger. This allows for more advanced customization options for logging level.
  • The filterPredicate property is now found on the individual unique loggers, rather than the shared logger. This allows for more advanced customization options for logging specific requests.

Usage

Add the following code to AppDelegate.m -application:didFinishLaunchingWithOptions::

[[AFNetworkActivityLogger sharedLogger] startLogging];

Now all NSURLSessionTask objects created by an AFURLSessionManager will have their request and response logged to the console, a la:

GET http://example.com/foo/bar.json
200 http://example.com/foo/bar.json [0.1860 s]

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:

[[AFNetworkActivityLogger sharedLogger] setLevel:AFLoggerLevelError];

Logging Levels

By default, the shared logger is configured with an AFNetworkActivityConsoleLogger with a debug level set to AFLoggerLevelInfo. To change the level, simply access the logger through the loggers property, and adjust the level. The following levels are provided:

  • 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.

Filtering Requests

To limit the requests that are logged by a unique logger, each object that conforms to AFNetworkActivityLoggerProtocol has a filterPredicate property. If the predicate returns true, the request will not be forwarded to the logger. For example, a custom file logger could be created that only logs requests for http://httpbin.org, while a console logger could be used to log all errors in the application.

AFNetworkActivityConsoleLogger *testLogger = [AFNetworkActivityConsoleLogger new];
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSURLRequest *  _Nonnull request, NSDictionary<NSString *,id> * _Nullable bindings) {
    return !([[request URL] baseURL] isEqualToString:@"httpbin.org"])
}];
[testLogger setFilterPredicate:predicate];

Custom Loggers

By default, the shared logger is configured with an AFNetworkActivityConsoleLogger.

To create a custom logger, create a new object that conforms to AFNetworkActivityLoggerProtocol, and add it to the shared logger. Be sure and configure the proper default logging level.

License

AFNetworkActivityLogger is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

AFNetworking

A delightful networking framework for iOS, macOS, watchOS, and tvOS.
Objective-C
33,334
star
2

AFIncrementalStore

1,687
star
3

AFOAuth2Manager

AFNetworking Extension for OAuth 2 Authentication
Objective-C
1,186
star
4

AFAmazonS3Manager

AFNetworking Client for the Amazon S3 API
Objective-C
362
star
5

fastlane

A fastlane setup to deploy Objective-C and Swift frameworks.
Ruby
251
star
6

Xcode-Project-Templates

Objective-C
221
star
7

AFHTTPRequestOperationLogger

AFNetworking Extension for HTTP Request Logging
Objective-C
214
star
8

AFOAuth1Client

AFNetworking Extension for OAuth 1.0a Authentication
Objective-C
210
star
9

AFJSONRPCClient

Client for interacting with JSON-RPC APIs
Objective-C
189
star
10

AFgzipRequestSerializer

gzip Request Serializer for AFNetworking
Objective-C
165
star
11

AFRocketClient

152
star
12

AFOnoResponseSerializer

An XML and HTML response serializer for AFNetworking 2.0, using Ono
Objective-C
113
star
13

AFNetworking-ASIHTTPRequest

75
star
14

AFCoreImageResponseSerializer

AFNetworking 2.0 Serialization Module to apple Core Image Filters to Images
Objective-C
68
star
15

AFWebDAVManager

AFNetworking manager for WebDAV
Objective-C
64
star
16

AFMsgPackSerialization

AFNetworking 2.0 Serialization Module for MsgPack
Objective-C
64
star
17

WebKit-AFNetworking

AFNetworking Extensions for WebKit
Objective-C
48
star
18

AFContentRequestOperations

Legacy content-specific request operation subclasses re-implemented with AFNetworking 2.0 APIs
Objective-C
25
star
19

AFCollectionJSONRequestOperation

AFNetworking Extension for Collection+JSON Hypermedia
20
star
20

afnetworking.com

Source code for AFNetworking.com
CSS
8
star