Rxswift: Option to disable logging in DEBUG for URLSession+Rx

Created on 3 May 2018  路  4Comments  路  Source: ReactiveX/RxSwift

Short description of the issue:

When using URLSession+Rx there is constant logging of responses in DEBUG mode, like below:

curl -X POST 
    -H "Accept: */*" 
    -H "Content-Type: multipart/form-data; boundary=Boundary+831b5b4241e692bf" 
"http://example.com/upload-image/" -i -v
Success (1660ms): Status 200
curl -X POST 
    -H "Accept: */*" 
    -H "Content-Type: multipart/form-data; boundary=Boundary+c93de5352cdf7ffd" 
"http://example.com/upload-image/" -i -v
Success (9105ms): Status 200

Unfortunately there is no ability to turn this off, without changing library inner code.

Expected outcome:

It would be great if there will be some flag or log-policy attribute, to be able to turn this logging off even in debug mode.

What actually happens:

It happens in file URLSession+Rx. There is this code:

 let task = self.base.dataTask(with: request) { (data, response, error) in

                if Logging.URLRequests(request) {
                    let interval = Date().timeIntervalSince(d ?? Date())
                    print(convertURLRequestToCurlCommand(request))
                    #if os(Linux)
                        print(convertResponseToString(response, error.flatMap { $0 as? NSError }, interval))
                    #else
                        print(convertResponseToString(response, error.map { $0 as NSError }, interval))
                    #endif
                }
...

If you jump to definition of Logging.URLRequests, you will see that it always returns true for DEBUG mode and false for RELEASE:

public struct Logging {
    public typealias LogURLRequest = (URLRequest) -> Bool

    /// Log URL requests to standard output in curl format.
    public static var URLRequests: LogURLRequest =  { _ in
    #if DEBUG
        return true
    #else
        return false
    #endif
    }
} 

RxSwift/RxCocoa/RxBlocking/RxTest version/commit

RxSwift 4.1.2

Platform/Environment

  • [x] iOS
  • [ ] macOS
  • [ ] tvOS
  • [ ] watchOS
  • [ ] playgrounds

How easy is to reproduce? (chances of successful reproduce after running the self contained code)

  • [x] easy, 100% repro
  • [ ] sometimes, 10%-100%
  • [ ] hard, 2% - 10%
  • [ ] extremely hard, %0 - 2%

Xcode version:

Version 9.3 (9E145)

Installation method:

  • [x] CocoaPods
  • [ ] Carthage
  • [ ] Git submodules

I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)

  • [ ] yes (which ones)
  • [x] no

Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)

  • [ ] just starting
  • [x] I have a small code base
  • [ ] I have a significant code base

Most helpful comment

Logging.URLRequests = { _ in false };

All 4 comments

Logging.URLRequests = { _ in false };

How do you disable this in 6.0.0?

It's in the README:

URLSession.rx.shouldLogRequest = { request in 
   return false // Or use request to determine if you want to log or not
}

Found it just as you replied :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaumard picture jaumard  路  3Comments

RafaelPlantard picture RafaelPlantard  路  3Comments

acecilia picture acecilia  路  3Comments

trungp picture trungp  路  3Comments

apoloa picture apoloa  路  3Comments