Skip to content

Commit 203bf64

Browse files
author
Jasper Blues
committed
Add error logger/provider. Default impl prints, as before. Custom can for eg log to sentry.
1 parent 544f712 commit 203bf64

File tree

4 files changed

+96
-30
lines changed

4 files changed

+96
-30
lines changed

Demo.xcodeproj/project.pbxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@
183183
44DFD3B01D9A34540014E9F2 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 148341701CF1CF0500E91F01 /* Image.swift */; };
184184
44DFD3B11D9A3FC80014E9F2 /* pig.png in Resources */ = {isa = PBXBuildFile; fileRef = 14B0CEE31CF1D0D700049AD6 /* pig.png */; };
185185
44DFD3B21D9A3FC90014E9F2 /* pig.png in Resources */ = {isa = PBXBuildFile; fileRef = 14B0CEE31CF1D0D700049AD6 /* pig.png */; };
186+
BA79824C3FD10818DA976F8A /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
187+
BA7985EE02F8B15CAE1C86DD /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
188+
BA79866331D2E147C9A9EE21 /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
189+
BA79878BCE133FA5429E6DAA /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
190+
BA798837CAF824FACE6DDC91 /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
191+
BA7988926836681D75A2D44C /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
192+
BA798D222B6284F771465C87 /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
193+
BA798DDD639F9AAFCF98EF39 /* ErrorLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */; };
186194
E6ED61311E27CD630058ACE9 /* UnauthorizedCallbackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */; };
187195
E6ED61321E27CD630058ACE9 /* UnauthorizedCallbackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */; };
188196
E6ED61331E27CD630058ACE9 /* UnauthorizedCallbackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */; };
@@ -247,6 +255,7 @@
247255
44DFD39B1D9A2D5A0014E9F2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
248256
44DFD39F1D9A2EA70014E9F2 /* CellData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellData.swift; sourceTree = "<group>"; };
249257
44DFD3A11D9A33610014E9F2 /* FakeImageController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeImageController.swift; sourceTree = "<group>"; };
258+
BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorLogger.swift; sourceTree = "<group>"; };
250259
E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnauthorizedCallbackTests.swift; sourceTree = "<group>"; };
251260
/* End PBXFileReference section */
252261

@@ -380,6 +389,7 @@
380389
4488DBC01D9BBFCF007D7A14 /* FakeRequest.swift */,
381390
1474C1C21CA2988900E15D84 /* TestCheck.swift */,
382391
446C24621DFEE964006FA6CC /* Helpers.swift */,
392+
BA7983C1B85B8AE9BAF59E3E /* ErrorLogger.swift */,
383393
);
384394
path = Sources;
385395
sourceTree = "<group>";
@@ -815,6 +825,7 @@
815825
1413F4771CE7968E00482096 /* JSON.swift in Sources */,
816826
4488DBCC1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */,
817827
445F053321007512003906C2 /* NetworkingTests.swift in Sources */,
828+
BA798837CAF824FACE6DDC91 /* ErrorLogger.swift in Sources */,
818829
);
819830
runOnlyForDeploymentPostprocessing = 0;
820831
};
@@ -834,6 +845,7 @@
834845
14638D0A1CC64629002B9433 /* Networking.swift in Sources */,
835846
4488DBC51D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
836847
14638D0B1CC64629002B9433 /* TestCheck.swift in Sources */,
848+
BA7985EE02F8B15CAE1C86DD /* ErrorLogger.swift in Sources */,
837849
);
838850
runOnlyForDeploymentPostprocessing = 0;
839851
};
@@ -853,6 +865,7 @@
853865
14638D201CC64733002B9433 /* Networking.swift in Sources */,
854866
4488DBC61D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
855867
14638D211CC64733002B9433 /* TestCheck.swift in Sources */,
868+
BA79866331D2E147C9A9EE21 /* ErrorLogger.swift in Sources */,
856869
);
857870
runOnlyForDeploymentPostprocessing = 0;
858871
};
@@ -872,6 +885,7 @@
872885
14638D361CC647B8002B9433 /* Networking.swift in Sources */,
873886
4488DBC71D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
874887
14638D371CC647B8002B9433 /* TestCheck.swift in Sources */,
888+
BA798D222B6284F771465C87 /* ErrorLogger.swift in Sources */,
875889
);
876890
runOnlyForDeploymentPostprocessing = 0;
877891
};
@@ -891,6 +905,7 @@
891905
14638D4C1CC64829002B9433 /* Networking.swift in Sources */,
892906
4488DBC81D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
893907
14638D4D1CC64829002B9433 /* TestCheck.swift in Sources */,
908+
BA79878BCE133FA5429E6DAA /* ErrorLogger.swift in Sources */,
894909
);
895910
runOnlyForDeploymentPostprocessing = 0;
896911
};
@@ -926,6 +941,7 @@
926941
1474C1C81CA2988900E15D84 /* Networking.swift in Sources */,
927942
4488DBCA1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */,
928943
445F053121007512003906C2 /* NetworkingTests.swift in Sources */,
944+
BA7988926836681D75A2D44C /* ErrorLogger.swift in Sources */,
929945
);
930946
runOnlyForDeploymentPostprocessing = 0;
931947
};
@@ -961,6 +977,7 @@
961977
44B5D3A01D19B9E5004378B9 /* Networking.swift in Sources */,
962978
4488DBCB1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */,
963979
445F053221007512003906C2 /* NetworkingTests.swift in Sources */,
980+
BA79824C3FD10818DA976F8A /* ErrorLogger.swift in Sources */,
964981
);
965982
runOnlyForDeploymentPostprocessing = 0;
966983
};
@@ -984,6 +1001,7 @@
9841001
446C24631DFEE964006FA6CC /* Helpers.swift in Sources */,
9851002
44DFD3A01D9A2EA70014E9F2 /* CellData.swift in Sources */,
9861003
44A6D5481E4122DE00405A7E /* Networking+Private.swift in Sources */,
1004+
BA798DDD639F9AAFCF98EF39 /* ErrorLogger.swift in Sources */,
9871005
);
9881006
runOnlyForDeploymentPostprocessing = 0;
9891007
};

Sources/ErrorLogger.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// SYMBIOSE
4+
// Copyright 2020 Symbiose Inc
5+
// All Rights Reserved.
6+
//
7+
// NOTICE: This software is proprietary information.
8+
// Unauthorized use is prohibited.
9+
//
10+
////////////////////////////////////////////////////////////////////////////////
11+
12+
import Foundation
13+
14+
public protocol ErrorLoggerProvider {
15+
16+
func provide(error: Error?) -> ErrorLogger
17+
18+
}
19+
20+
public class ConsoleLogProvider: ErrorLoggerProvider {
21+
22+
public func provide(error: Error?) -> ErrorLogger {
23+
ConsoleErrorLogger()
24+
}
25+
26+
}
27+
28+
public protocol ErrorLogger {
29+
30+
func log(_ message: String)
31+
32+
func flush()
33+
34+
}
35+
36+
class ConsoleErrorLogger: ErrorLogger {
37+
38+
func log(_ message: String) {
39+
print(message)
40+
}
41+
42+
func flush() {} // Not required for console logger
43+
44+
}

Sources/Networking+Private.swift

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -347,32 +347,33 @@ extension Networking {
347347
func logError(parameterType: ParameterType?, parameters: Any? = nil, data: Data?, request: URLRequest?, response: URLResponse?, error: NSError?) {
348348
guard isErrorLoggingEnabled else { return }
349349
guard let error = error else { return }
350+
let logger = logProvider.provide(error: error)
350351

351-
print(" ")
352-
print("========== Networking Error ==========")
353-
print(" ")
352+
logger.log(" ")
353+
logger.log("========== Networking Error ==========")
354+
logger.log(" ")
354355

355356
let isCancelled = error.code == NSURLErrorCancelled
356357
if isCancelled {
357358
if let request = request, let url = request.url {
358-
print("Cancelled request: \(url.absoluteString)")
359-
print(" ")
359+
logger.log("Cancelled request: \(url.absoluteString)")
360+
logger.log(" ")
360361
}
361362
} else {
362-
print("*** Request ***")
363-
print(" ")
363+
logger.log("*** Request ***")
364+
logger.log(" ")
364365

365-
print("Error \(error.code): \(error.description)")
366-
print(" ")
366+
logger.log("Error \(error.code): \(error.description)")
367+
logger.log(" ")
367368

368369
if let request = request, let url = request.url {
369-
print("URL: \(url.absoluteString)")
370-
print(" ")
370+
logger.log("URL: \(url.absoluteString)")
371+
logger.log(" ")
371372
}
372373

373374
if let headers = request?.allHTTPHeaderFields {
374-
print("Headers: \(headers)")
375-
print(" ")
375+
logger.log("Headers: \(headers)")
376+
logger.log(" ")
376377
}
377378

378379
if let parameterType = parameterType, let parameters = parameters {
@@ -382,44 +383,45 @@ extension Networking {
382383
let data = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
383384
let string = String(data: data, encoding: .utf8)
384385
if let string = string {
385-
print("Parameters: \(string)")
386-
print(" ")
386+
logger.log("Parameters: \(string)")
387+
logger.log(" ")
387388
}
388389
} catch let error as NSError {
389-
print("Failed pretty printing parameters: \(parameters), error: \(error)")
390-
print(" ")
390+
logger.log("Failed pretty printing parameters: \(parameters), error: \(error)")
391+
logger.log(" ")
391392
}
392393
case .formURLEncoded:
393394
guard let parametersDictionary = parameters as? [String: Any] else { fatalError("Couldn't cast parameters as dictionary: \(parameters)") }
394395
do {
395396
let formattedParameters = try parametersDictionary.urlEncodedString()
396-
print("Parameters: \(formattedParameters)")
397+
logger.log("Parameters: \(formattedParameters)")
397398
} catch let error as NSError {
398-
print("Failed parsing Parameters: \(parametersDictionary)\(error)")
399+
logger.log("Failed parsing Parameters: \(parametersDictionary)\(error)")
399400
}
400-
print(" ")
401+
logger.log(" ")
401402
default: break
402403
}
403404
}
404405

405406
if let data = data, let stringData = String(data: data, encoding: .utf8) {
406-
print("Data: \(stringData)")
407-
print(" ")
407+
logger.log("Data: \(stringData)")
408+
logger.log(" ")
408409
}
409410

410411
if let response = response as? HTTPURLResponse {
411-
print("*** Response ***")
412-
print(" ")
412+
logger.log("*** Response ***")
413+
logger.log(" ")
413414

414-
print("Headers: \(response.allHeaderFields)")
415-
print(" ")
415+
logger.log("Headers: \(response.allHeaderFields)")
416+
logger.log(" ")
416417

417-
print("Status code: \(response.statusCode)\(HTTPURLResponse.localizedString(forStatusCode: response.statusCode))")
418-
print(" ")
418+
logger.log("Status code: \(response.statusCode)\(HTTPURLResponse.localizedString(forStatusCode: response.statusCode))")
419+
logger.log(" ")
419420
}
420421
}
421-
print("================= ~ ==================")
422-
print(" ")
422+
logger.log("================= ~ ==================")
423+
logger.log(" ")
424+
logger.flush()
423425
}
424426

425427
func cacheOrPurgeJSON(object: Any?, path: String, cacheName: String?, cachingLevel: CachingLevel) throws {

Sources/Networking.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ open class Networking {
111111
URLSession(configuration: self.configuration)
112112
}()
113113

114+
public var logProvider: ErrorLoggerProvider = ConsoleLogProvider()
115+
114116
/// Caching options
115117
public enum CachingLevel {
116118
case memory

0 commit comments

Comments
 (0)