diff --git a/Sources/Networking/Components/Cached.swift b/Sources/Networking/Components/Cached.swift index 9cdd485c..abe7f8e1 100644 --- a/Sources/Networking/Components/Cached.swift +++ b/Sources/Networking/Components/Cached.swift @@ -23,6 +23,7 @@ extension HTTPRequestData { private struct Cached: NetworkingModifier { var cache: Cache + @NetworkEnvironment(\.logger) var logger func send(upstream: NetworkingComponent, request: HTTPRequestData) -> ResponseStream { guard case let .always(timeToLive) = request.cacheOption else { return upstream.send(request) @@ -36,7 +37,8 @@ private struct Cached: NetworkingModifier { copy.cachedMetadata = CachedMetadata( originalRequest: cachedValue.request ) - continuation.yield(.value(copy, BytesReceived())) + logger?.debug("🎯 Cached from \(cachedValue.request.prettyPrintedIdentifier)") + continuation.yield(.value(copy, BytesReceived(data: cachedValue.data))) continuation.finish() return } @@ -76,6 +78,10 @@ extension HTTPResponseData { nil != cachedMetadata } + public var isNotCached: Bool { + false == isCached + } + public var cachedOriginalRequest: HTTPRequestData? { cachedMetadata?.originalRequest } diff --git a/Sources/Networking/Components/Logged.swift b/Sources/Networking/Components/Logged.swift index cb345319..9d8ef72c 100644 --- a/Sources/Networking/Components/Logged.swift +++ b/Sources/Networking/Components/Logged.swift @@ -16,12 +16,20 @@ extension NetworkingComponent { ) -> some NetworkingComponent { modified( Logged( - onStart: onStart ?? { logger.info("↗️ \($0.debugDescription)") }, + onStart: onStart ?? { + logger.info("↗️ \($0.debugDescription)") + logger.debug("\($0.prettyPrintedHeaders)") + logger.debug("\($0.prettyPrintedBody)") + }, onFailure: onFailure ?? { request, error in - logger.error("⚠️ \(request.debugDescription), error: \(String(describing: error))") + logger.warning("⚠️ \(request.debugDescription), error: \(String(describing: error))") }, onSuccess: onSuccess ?? { request, response, _ in logger.info("🆗 \(response.debugDescription)") + if response.isNotCached { + logger.debug("\(response.prettyPrintedHeaders)") + logger.debug("\(response.prettyPrintedBody)") + } logger.info("↙️ \(request.debugDescription)") } ) diff --git a/Sources/Networking/Core/HTTPRequestData.swift b/Sources/Networking/Core/HTTPRequestData.swift index 572752d5..5213618b 100644 --- a/Sources/Networking/Core/HTTPRequestData.swift +++ b/Sources/Networking/Core/HTTPRequestData.swift @@ -261,13 +261,17 @@ extension HTTPRequestData: Hashable { extension HTTPRequestData: CustomDebugStringConvertible { public var debugDescription: String { - "[\(RequestSequence.number):\(identifier)] \(request.debugDescription)" + "[\(prettyPrintedIdentifier)] \(request.debugDescription)" } } // MARK: - Logging Helpers extension HTTPRequestData { + public var prettyPrintedIdentifier: String { + "\(RequestSequence.number):\(identifier)" + } + public var prettyPrintedHeaders: String { headerFields.debugDescription } diff --git a/Sources/Networking/Core/HTTPResponseData.swift b/Sources/Networking/Core/HTTPResponseData.swift index 0af468b9..fb304611 100644 --- a/Sources/Networking/Core/HTTPResponseData.swift +++ b/Sources/Networking/Core/HTTPResponseData.swift @@ -119,24 +119,19 @@ extension HTTPResponseData: Hashable { extension HTTPResponseData: CustomDebugStringConvertible { public var debugDescription: String { - var debugDescription = "\(self.status.description)" - if data.isEmpty { - debugDescription += " No Data" - } else { - if let contentType = self.headerFields[.contentType] { - debugDescription += "\(contentType.description)" - #if hasFeature(BareSlashRegexLiterals) - let regex = /(json)/ - #else - let regex = #/(json)/# - #endif - if contentType.contains(regex) { - let dataDescription = String(decoding: data, as: UTF8.self) - debugDescription += "\n\(dataDescription)" - } - } - } - return debugDescription + "\(self.status.description)" + } +} + +// MARK: - Logging Helpers + +extension HTTPResponseData { + public var prettyPrintedHeaders: String { + http.headerFields.debugDescription + } + + public var prettyPrintedBody: String { + data.prettyPrintedData } }