Network Logging - iOS

Network logs are automatically collected by Instabug when possible. There are many way to configure and manipulate these logs from the code.

Disable and Enable Request Logging

By default, request logging is enabled. It can be disabled using the API to the right.

Enable and Disable

NetworkLogger.enabled = false
IBGNetworkLogger.enabled = NO;

Omitting Requests from Logs

You can omit requests from being logged based on either their request or response details. [Instabug setNetworkLoggingRequestFilterPredicate:responseFilterPredicate:] allows you to specify two predicates that are going to be evaluated against every request and response to determine if the request should be included in logs or not.

The example code to the right will exclude all requests made to URLs that have /products path. It will also exclude all responses that has a success and redirection status code, thus only including requests with 4xx and 5xx responses.

requestFilterPredicate is evaluated against an NSURLRequest, while responseFilterPredicate is evaluated against an NSHTTPURLResponse.

Omit Request

let path = "/products"
let requestPredicate = NSPredicate(format: "URL.path MATCHES %@", path)
let responsePredicate = NSPredicate(format: "statusCode >= %d AND statusCode <= %d", 200, 399)
NetworkLogger.setNetworkLoggingRequestFilterPredicate(requestPredicate, responseFilterPredicate: responsePredicate)
NSString *path = @"/products";
NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:@"URL.path MATCHES %@", path];
NSPredicate *responsePredicate = [NSPredicate predicateWithFormat:@"statusCode >= %d AND statusCode <= %d", 200, 399];
[IBGNetworkLogger setNetworkLoggingRequestFilterPredicate:requestPredicate responseFilterPredicate:responsePredicate];

Obfuscating Data

Both requests and responses can be obfuscated if required. You can obfuscate user sensitive data in requests, like authentication tokens for example, without filtering out the whole request. As with requests, the response object, as well as the response data, could be modified for obfuscation purposes before they are logged.

Obfuscate Request

NetworkLogger.setRequestObfuscationHandler { (request) -> URLRequest in
    var myRequest:NSMutableURLRequest = request as! NSMutableURLRequest
    let urlString = request.url?.absoluteString
    urlString = obfuscateAuthenticationTokenInString()
    let obfuscatedURL = URL(string: urlString)
    myRequest.url = obfuscatedURL
    return myRequest.copy() as! URLRequest
}
IBGNetworkLogger.requestObfuscationHandler = ^NSURLRequest * _Nonnull(NSURLRequest * _Nonnull request) {
        NSMutableURLRequest *myRequest = [request mutableCopy];
        NSString *urlString = request.URL.absoluteString;
        urlString = [self obfuscateAuthenticationTokenInString:urlString];
        NSURL *obfuscatedURL = [NSURL URLWithString:urlString];
        myRequest.URL = obfuscatedURL;
        return myRequest;
    };
Obfuscate Response

NetworkLogger.setResponseObfuscationHandler { (data, response, completion) in
    if let data = data {
        let modifiedData = self.modify(data: data)
        let modifiedResponse = self.modify(response: response)
        
        completion(modifiedData, modifiedResponse)
    }
}
[IBGNetworkLogger setResponseObfuscationHandler:^(NSData * _Nullable responseData, NSURLResponse * _Nonnull response, NetworkObfuscationCompletionBlock  _Nonnull returnBlock) {
    NSData *modifiedData = [self obfuscateData:responseData];
    NSURLResponse *modifiedResponse = [self obfuscateResponse:response];
    
    returnBlock(modifiedData, modifiedResponse);
}];

Requests Not Appearing

If your network requests aren't being logged automatically because you're probably using a custom NSURLSession or NSURLSessionConfiguration, you would need to enable logging for your NSURLSession using this API.

let configuration = URLSessionConfiguration.ephemeral
NetworkLogger.enableLogging(for: configuration)
let session = URLSession(configuration: configuration)
NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration;
[IBGNetworkLogger enableLoggingForURLSessionConfiguration:configuration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

📘

If the requests still aren't appearing

You'll need to make sure that enableLoggingForURLSessionConfiguration: was called just before using the configuration to create the session.

AFNetworking

To enable logging for AFNetworking, you may create this class, then use IBGAFURLSessionManager to create your requests.

// IBGAFURLSessionManager.h

#import <AFNetworking/AFNetworking.h>

@interface IBGAFURLSessionManager : AFURLSessionManager

@end

// IBGAFURLSessionManager.m
  
#import "IBGAFURLSessionManager.h"
#import <Instabug/Instabug.h>

@implementation IBGAFURLSessionManager

- (instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration {
        [IBGNetworkLogger enableLoggingForURLSessionConfiguration:configuration];
    
    return [super initWithSessionConfiguration:configuration];
}

@end

Alamofire

To enable logging for Alamofire, you may create this class, then use IBGSessionManager to create your requests.

import Alamofire
import Instabug

class IBGSessionManager: Alamofire.Session {
    static let sharedManager: IBGSessionManager = {
        let configuration = URLSessionConfiguration.default
        NetworkLogger.enableLogging(for: configuration)
        let manager = IBGSessionManager(configuration: configuration)
        return manager
    }()
}