Skip to content

Commit

Permalink
Merge pull request #26 from icapps/feature/improve-cached-logging
Browse files Browse the repository at this point in the history
Make sure the logging is correctly displayed even when cached.
  • Loading branch information
fousa authored Mar 3, 2019
2 parents dc2fdec + 40981e9 commit 2922784
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions Sources/Logger/ConsoleLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,52 @@ extension ConsoleLogger: Logger {
os_log("☁️ %{public}@: %{public}@", log: OSLog.request, type: .info, method, url.absoluteString)
}

// swiftlint:disable function_body_length
public func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) {
guard
let method = urlRequest.httpMethod,
let httpResponse = urlResponse as? HTTPURLResponse,
let url = urlRequest.url,
let transactionMetric = metrics.transactionMetrics.first,
let requestEndDate = transactionMetric.requestEndDate,
let domainLookupStartDate = transactionMetric.domainLookupStartDate else { return }
let requestEndDate = transactionMetric.requestEndDate else { return }

let totalDuration = requestEndDate.timeIntervalSince(domainLookupStartDate)
if let error = error {
os_log("⛈ %{public}@ %i: %{public}@ 💥 %{public}@ ⏳ %.3fms",
log: OSLog.request,
type: .info,
method,
httpResponse.statusCode,
url.absoluteString,
error.localizedDescription,
totalDuration)
if let domainLookupStartDate = transactionMetric.domainLookupStartDate {
let totalDuration = requestEndDate.timeIntervalSince(domainLookupStartDate)
if let error = error {
os_log("⛈ %{public}@ %i: %{public}@ 💥 %{public}@ ⏳ %.3fms",
log: OSLog.request,
type: .info,
method,
httpResponse.statusCode,
url.absoluteString,
error.localizedDescription,
totalDuration)
} else {
os_log("☀️ %{public}@ %i: %{public}@ ⏳ %.3fms",
log: OSLog.request,
type: .info,
method,
httpResponse.statusCode,
url.absoluteString,
totalDuration)
}
} else {
os_log("☀️ %{public}@ %i: %{public}@ ⏳ %.3fms",
log: OSLog.request,
type: .info,
method,
httpResponse.statusCode,
url.absoluteString,
totalDuration)
if let error = error {
os_log("⛈ %{public}@ %i: %{public}@ 💥 %{public}@",
log: OSLog.request,
type: .info,
method,
httpResponse.statusCode,
url.absoluteString,
error.localizedDescription)
} else {
os_log("☀️ %{public}@ %i: %{public}@",
log: OSLog.request,
type: .info,
method,
httpResponse.statusCode,
url.absoluteString)
}
}
}
}

0 comments on commit 2922784

Please sign in to comment.