Skip to content

Commit abd93d5

Browse files
committed
- More logging options added
1 parent 3b421b2 commit abd93d5

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ var sessionAuthenticator: RequestAuthenticator {
161161

162162
# Debugging
163163

164-
To turn on logging add `APIErrorLoggingEnabled` to 'Arguments passed on launch' at Schema/Run/Arguments
164+
To turn on request logging add `APIRequestLoggingEnabled` to 'Arguments passed on launch' at Schema/Run/Arguments.
165+
166+
To log server sent errrors turn on `APIErrorLoggingEnabled`.
165167

166168
# TODO list
167169

RESTAPI.xcodeproj/xcshareddata/xcschemes/RESTAPI iOS.xcscheme

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
language = ""
2930
shouldUseLaunchSchemeArgsEnv = "YES">
3031
<Testables>
3132
<TestableReference
@@ -55,6 +56,7 @@
5556
buildConfiguration = "Debug"
5657
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5758
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
59+
language = ""
5860
launchStyle = "0"
5961
useCustomWorkingDirectory = "NO"
6062
ignoresPersistentStateOnLaunch = "NO"
@@ -75,6 +77,10 @@
7577
argument = "APIErrorLoggingEnabled"
7678
isEnabled = "YES">
7779
</CommandLineArgument>
80+
<CommandLineArgument
81+
argument = "APIRequestLoggingEnabled"
82+
isEnabled = "YES">
83+
</CommandLineArgument>
7884
</CommandLineArguments>
7985
<AdditionalOptions>
8086
</AdditionalOptions>

RESTAPI/API.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ internal extension API {
221221
request.httpMethod = method
222222

223223
let session = URLSession(configuration: URLSessionConfiguration.default)
224+
if ProcessInfo.processInfo.arguments.contains("APIRequestLoggingEnabled") {
225+
let loggedRequest = authentication.authenticateURLRequest(request)
226+
print("\n\(loggedRequest.httpMethod ?? "No http method") \(loggedRequest.url?.absoluteString ?? "No URL")")
227+
print("HEADERS:\n\(loggedRequest.allHTTPHeaderFields?.reduce("", { return $0 + "\t\($1.key): \($1.value)\n" }) ?? "No header fields")")
228+
if let body = loggedRequest.httpBody {
229+
print("BODY:\n\(String(data: body, encoding: .utf8) ?? "Cannot parse request body")")
230+
}
231+
else {
232+
print("Empty request body")
233+
}
234+
}
224235
session.dataTask(with: authentication.authenticateURLRequest(request) as URLRequest,
225236
completionHandler: { (data, response, error) -> Void in
226237
if let err = APIError(withResponse: response), ProcessInfo.processInfo.arguments.contains("APIErrorLoggingEnabled") {

0 commit comments

Comments
 (0)