Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Logged improvements #40

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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