Skip to content

Commit d304bd1

Browse files
committed
Update logs
1 parent c16c07f commit d304bd1

File tree

8 files changed

+29
-52
lines changed

8 files changed

+29
-52
lines changed

APIService/Sources/Logger/APILogger.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import os.log
55
public protocol APILogger: AnyObject {
66
/// Logs the details of a URLRequest.
77
///
8-
/// - Parameter urlRequest: The URLRequest to log.
9-
func logRequest(_ urlRequest: URLRequest)
8+
/// - Parameter request: The URLRequest to log.
9+
func logRequest(_ request: URLRequest)
1010

1111
/// Logs the details of a URLResponse along with any associated data.
1212
///
1313
/// - Parameters:
14+
/// - request: The URLRequest associated with the response.
1415
/// - response: The URLResponse to log.
1516
/// - data: The data associated with the response, if any.
16-
func logResponse(_ response: URLResponse?, data: Data?)
17+
func logResponse(forRequest request: URLRequest?, response: URLResponse?, data: Data?)
1718
}

APIService/Sources/Logger/BaseLogger.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ open class BaseLogger: APILogger {
2121
/// The log to use for logging.
2222
open var log: LogProtocol = OSLog()
2323

24-
public func logRequest(_ urlRequest: URLRequest) {
24+
public func logRequest(_ request: URLRequest) {
2525
fatalError("Subclasses must implement this method")
2626
}
2727

28-
public func logResponse(_ response: URLResponse?, data: Data?) {
28+
public func logResponse(forRequest request: URLRequest?, response: URLResponse?, data: Data?) {
2929
fatalError("Subclasses must implement this method")
3030
}
3131
}

APIService/Sources/Logger/CompactLogger.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ open class CompactLogger: BaseLogger {
88
super.init()
99
}
1010

11-
/// Logs minimal information about a URLRequest.
12-
///
13-
/// - Parameter urlRequest: The URLRequest to log.
14-
open override func logRequest(_ urlRequest: URLRequest) {
15-
let method = urlRequest.httpMethod ?? "UNKNOWN"
16-
let urlString = urlRequest.url?.absoluteString ?? ""
11+
open override func logRequest(_ request: URLRequest) {
12+
let method = request.httpMethod ?? "UNKNOWN"
13+
let urlString = request.url?.absoluteString ?? ""
1714
var logString = method + " " + urlString
1815

1916
if let prefix {
@@ -22,13 +19,8 @@ open class CompactLogger: BaseLogger {
2219

2320
log.log(logString)
2421
}
25-
26-
/// Logs minimal information about a URLResponse, including status code and URL.
27-
///
28-
/// - Parameters:
29-
/// - response: The URLResponse to log.
30-
/// - data: The data associated with the response, if any.
31-
open override func logResponse(_ response: URLResponse?, data: Data?) {
22+
23+
open override func logResponse(forRequest request: URLRequest?, response: URLResponse?, data: Data?) {
3224
var logString = ""
3325

3426
if let httpResponse = response as? HTTPURLResponse {

APIService/Sources/Logger/IntermediateLogger.swift

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@ open class IntermediateLogger: BaseLogger {
1010
super.init()
1111
}
1212

13-
/// Logs detailed information about a URLRequest, including headers and body.
14-
///
15-
/// - Parameter urlRequest: The URLRequest to log.
16-
open override func logRequest(_ urlRequest: URLRequest) {
17-
let method = urlRequest.httpMethod ?? "UNKNOWN"
18-
let urlString = urlRequest.url?.absoluteString ?? ""
13+
open override func logRequest(_ request: URLRequest) {
14+
let method = request.httpMethod ?? "UNKNOWN"
15+
let urlString = request.url?.absoluteString ?? ""
1916
var logString = method + " " + urlString + "\n"
2017

21-
if let headers = urlRequest.allHTTPHeaderFields {
18+
if let headers = request.allHTTPHeaderFields {
2219
for (key, value) in headers {
2320
logString += "\(key): \(value)\n"
2421
}
2522
}
2623

27-
if let bodyData = urlRequest.httpBody, let bodyString = bodyData.toJSONString(prettyPrinted: prettyPrinted) {
24+
if let bodyData = request.httpBody, let bodyString = bodyData.toJSONString(prettyPrinted: prettyPrinted) {
2825
logString += bodyString
2926
}
3027

@@ -35,12 +32,7 @@ open class IntermediateLogger: BaseLogger {
3532
log.log(logString)
3633
}
3734

38-
/// Logs detailed information about a URLResponse, including status code, headers, and body.
39-
///
40-
/// - Parameters:
41-
/// - response: The URLResponse to log.
42-
/// - data: The data associated with the response, if any.
43-
open override func logResponse(_ response: URLResponse?, data: Data?) {
35+
open override func logResponse(forRequest request: URLRequest?, response: URLResponse?, data: Data?) {
4436
var logString = ""
4537

4638
if let httpResponse = response as? HTTPURLResponse {

APIService/Sources/Logger/VerboseLogger.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,26 @@ open class VerboseLogger: BaseLogger {
1010
super.init()
1111
}
1212

13-
/// Logs detailed information about a URLRequest, including headers and body.
14-
///
15-
/// - Parameter urlRequest: The URLRequest to log.
16-
open override func logRequest(_ urlRequest: URLRequest) {
17-
let httpMethod = urlRequest.httpMethod ?? "UNKNOWN"
18-
let path = urlRequest.url?.path ?? "UNKNOWN"
13+
open override func logRequest(_ request: URLRequest) {
14+
let httpMethod = request.httpMethod ?? "UNKNOWN"
15+
let path = request.url?.path ?? "UNKNOWN"
1916
var logString = "\(httpMethod) \(path)\n"
2017

21-
if let queryItems = urlRequest.url?.query {
18+
if let queryItems = request.url?.query {
2219
logString += "Query: \(queryItems)\n"
2320
}
2421

25-
if let host = urlRequest.url?.host {
22+
if let host = request.url?.host {
2623
logString += "Host: \(host)\n"
2724
}
2825

29-
if let headers = urlRequest.allHTTPHeaderFields {
26+
if let headers = request.allHTTPHeaderFields {
3027
for (key, value) in headers {
3128
logString += "\(key): \(value)\n"
3229
}
3330
}
3431

35-
if let bodyData = urlRequest.httpBody, let bodyString = bodyData.toJSONString(prettyPrinted: prettyPrinted) {
32+
if let bodyData = request.httpBody, let bodyString = bodyData.toJSONString(prettyPrinted: prettyPrinted) {
3633
logString += "\n\(bodyString)"
3734
}
3835

@@ -43,12 +40,7 @@ open class VerboseLogger: BaseLogger {
4340
log.log(logString)
4441
}
4542

46-
/// Logs detailed information about a URLResponse, including status code, headers, and body.
47-
///
48-
/// - Parameters:
49-
/// - response: The URLResponse to log.
50-
/// - data: The data associated with the response, if any.
51-
open override func logResponse(_ response: URLResponse?, data: Data?) {
43+
open override func logResponse(forRequest request: URLRequest?, response: URLResponse?, data: Data?) {
5244
var logString = ""
5345

5446
if let httpResponse = response as? HTTPURLResponse {

APIService/Sources/Service/APIService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public extension APIService {
3939

4040
return session.dataTaskPublisher(for: urlRequest)
4141
.tryMap { [weak self] output in
42-
self?.logger?.logResponse(output.response, data: output.data)
42+
self?.logger?.logResponse(forRequest: urlRequest, response: output.response, data: output.data)
4343

4444
guard let httpResponse = output.response as? HTTPURLResponse else {
4545
throw URLError(.badServerResponse)
@@ -117,7 +117,7 @@ public extension APIService {
117117

118118
return session.downloadTaskPublisher(for: urlRequest)
119119
.handleEvents(receiveOutput: { [weak self] output in
120-
self?.logger?.logResponse(output.response, data: nil)
120+
self?.logger?.logResponse(forRequest: urlRequest, response: output.response, data: nil)
121121
})
122122
.map { $0.url }
123123
.receive(on: queue)

APIService/Sources/Service/DataWithProgress.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class DataTaskHandler: NSObject, URLSessionDataDelegate {
8989
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
9090
guard let url = task.originalRequest?.url else { return }
9191

92-
logger?.logResponse(task.response, data: nil)
92+
logger?.logResponse(forRequest: task.originalRequest, response: task.response, data: nil)
9393

9494
if let error = error as? URLError {
9595
didComplete?(url, error)

APIService/Sources/Service/DownloadWithProgress.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class DownloadTaskHandler: NSObject, URLSessionDownloadDelegate {
6868

6969
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
7070
guard let url = downloadTask.originalRequest?.url else { return }
71-
logger?.logResponse(downloadTask.response, data: nil)
71+
logger?.logResponse(forRequest: downloadTask.originalRequest, response: downloadTask.response, data: nil)
7272
didFinishDownloading?(url, location)
7373
}
7474

0 commit comments

Comments
 (0)