How to silence debug logging with Alamofire (or NSUrlSession)?

Viewed 507

Xcode 12.2, Alamofire 5.4.0, Swift 5


I have this snippet of code to request data from a backend which works fine in the happy flow. But if for some reason the backend is unreachable, I see autogenerated logging I want it to hide or silence. Because it's disturbing me when I'm analysing debug logging.

let request = AF.request("http://192.168.1.5:44444/api/users", headers: headers)
        request.responseJSON { (data) in
    
            switch data.result {
                case .success:
                    print("Request Succes!")
                case .failure(let errorData):
                    print("Request Failed")
                    print("\(errorData.errorDescription ?? "")")
            }
        }

Error output:

Task <xxxxxxxx>.<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
Task <xxxxxxxx>.<1> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x600000d82250 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <xxxxxxxxx>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <xxxxxxxxx>.<1>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://192.168.1.5:44444/developer, NSErrorFailingURLKey=http://192.168.1.5:44444/developer, _kCFStreamErrorDomainKey=1}
Request Failed
URLSessionTask failed with error: Could not connect to the server.

I want to get rid of the following, which is not generated by me:

Task <xxxxxxxx>.<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
Task <xxxxxxxx>.<1> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x600000d82250 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <xxxxxxxxx>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <xxxxxxxxx>.<1>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://192.168.1.5:44444/developer, NSErrorFailingURLKey=http://192.168.1.5:44444/developer, _kCFStreamErrorDomainKey=1}

and see only:

Request Failed
URLSessionTask failed with error: Could not connect to the server.
2 Answers

These are system logs produced by the OS, not Alamofire. While you can silence them by disabling the os subsystem, I don't recommend it, as that disables all os_logs in your app and other os module functionality, like signposts. A feature request to Apple using Feedback Assistant may eventually convince them to let us filter these in Xcode.

I had exactly the same issue, I want to only the the debug entries created by me and not the ones by the system. I solved this using a prefix for all my own log entries. For example "[sync] Synchronizing from server..." or "[sync] Couldn't connect to server.".

Now you can set the filter in XCode (at the bottom of the log pane) to the prefix "[sync]" and you only see your own entries. This even works while running the app and the logs come in!

Related