Skip to content

Commit

Permalink
feat: Logged improvements (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: danthorpe <[email protected]>
  • Loading branch information
danthorpe and danthorpe authored Nov 6, 2023
1 parent 59f6e74 commit f2b4e80
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
8 changes: 7 additions & 1 deletion Sources/Networking/Components/Cached.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension HTTPRequestData {

private struct Cached: NetworkingModifier {
var cache: Cache<HTTPRequestData, HTTPResponseData>
@NetworkEnvironment(\.logger) var logger
func send(upstream: NetworkingComponent, request: HTTPRequestData) -> ResponseStream<HTTPResponseData> {
guard case let .always(timeToLive) = request.cacheOption else {
return upstream.send(request)
Expand All @@ -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
}
Expand Down Expand Up @@ -76,6 +78,10 @@ extension HTTPResponseData {
nil != cachedMetadata
}

public var isNotCached: Bool {
false == isCached
}

public var cachedOriginalRequest: HTTPRequestData? {
cachedMetadata?.originalRequest
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/Networking/Components/Logged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
)
Expand Down
6 changes: 5 additions & 1 deletion Sources/Networking/Core/HTTPRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 13 additions & 18 deletions Sources/Networking/Core/HTTPResponseData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit f2b4e80

Please sign in to comment.