Skip to content

Commit

Permalink
Merge pull request #161 from theleftbit/log-errors
Browse files Browse the repository at this point in the history
Improve logs
  • Loading branch information
piercifani authored Jan 16, 2025
2 parents 399d4c2 + 1f40f41 commit 7765e90
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
32 changes: 20 additions & 12 deletions Sources/BSWFoundation/APIClient/APIClient+Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import OSLog
extension APIClient {

func logRequest(request: URLRequest) {
guard loggingConfiguration.requestBehaviour == .all else {
return
}
let logger = Logger(subsystem: submoduleName("APIClient"), category: "APIClient.Request")
switch loggingConfiguration.requestBehaviour {
case .all:
let httpMethod = request.httpMethod ?? "GET"
let path = request.url?.path ?? ""
logger.debug("Method: \(httpMethod) Path: \(path)")
if let data = request.httpBody, let prettyString = String(data: data, encoding: .utf8) {
logger.debug("Body: \(prettyString)")
}
default:
break
let httpMethod = request.httpMethod ?? "GET"
let path = request.url?.path ?? ""
logger.debug("Sending URLRequest → \(httpMethod) \(path)")
if let data = request.httpBody, let prettyString = String(data: data, encoding: .utf8) {
logger.debug("Body: \(prettyString)")
}
}

Expand All @@ -36,9 +34,19 @@ extension APIClient {
guard shouldLogThis else { return }
let logType: OSLogType = isError ? .error : .debug
let path = response.httpResponse.url?.path ?? ""
logger.log(level: logType, "StatusCode: \(response.httpResponse.statusCode) Path: \(path)")
if isError, let errorString = String(data: response.data, encoding: .utf8) {
logger.log(level: logType, "Receiving Response → Path: \(path) HTTPStatusCode: \(response.httpResponse.statusCode) ")
if isError, let errorString = String(data: response.data, encoding: .utf8), !errorString.isEmpty {
logger.log(level: logType, "Error Message: \(errorString)")
}
}

func logNetworkError(_ networkError: Swift.Error, forRequest request: URLRequest) {
guard loggingConfiguration.responseBehaviour != .none else {
return
}
let logger = Logger(subsystem: submoduleName("APIClient"), category: "APIClient.Network")
let httpMethod = request.httpMethod ?? "GET"
let path = request.url?.path ?? ""
logger.error("Error Received for URLRequest → \(httpMethod) \(path). Error: \(networkError)")
}
}
2 changes: 1 addition & 1 deletion Sources/BSWFoundation/APIClient/APIClient+URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct VoidResponse: Decodable, Hashable, Sendable {}
// MARK: UIApplicationWrapper
/// This is here just to make sure that on non-UIKit
/// platforms we have a nice API to call to.
#if canImport(UIKit)
#if canImport(UIKit.UIApplication)
import UIKit
private extension APIClient {
class ApplicationWrapper {
Expand Down
7 changes: 6 additions & 1 deletion Sources/BSWFoundation/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ private extension APIClient {
func sendNetworkRequest(_ urlRequest: URLRequest) async throws -> APIClient.Response {
try Task.checkCancellation()
logRequest(request: urlRequest)
return try await networkFetcher.fetchData(with: urlRequest)
do {
return try await networkFetcher.fetchData(with: urlRequest)
} catch {
logNetworkError(error, forRequest: urlRequest)
throw error
}
}

func validateResponse(_ response: Response) async throws -> Data {
Expand Down

0 comments on commit 7765e90

Please sign in to comment.