Skip to content

Commit

Permalink
1.36.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Aug 15, 2024
1 parent 27b3e87 commit 706e4f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Sources/SwiftAPIClient/APIClientCaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ public extension APIClient {
do {
return try withRequest { request, configs in
let fileIDLine = configs.fileIDLine ?? FileIDLine(fileID: fileID, line: line)
let configs = configs.with(\.fileIDLine, fileIDLine)

if !configs.loggingComponents.isEmpty {
if configs.loggingComponents.contains(.onRequest), configs.loggingComponents != .onRequest {
let message = configs.loggingComponents.requestMessage(for: request, uuid: uuid, fileIDLine: fileIDLine)
configs.logger.log(level: configs.logLevel, "\(message)")
}
Expand Down
9 changes: 6 additions & 3 deletions Sources/SwiftAPIClient/Clients/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ extension APIClientCaller where Result == AsyncThrowingValue<(Value, HTTPRespons
uuid: uuid,
error: error,
request: request,
duration: duration
duration: duration,
fileIDLine: configs.fileIDLine
)
configs.logger.log(level: configs._errorLogLevel, "\(message)")
}
Expand All @@ -149,7 +150,8 @@ extension APIClientCaller where Result == AsyncThrowingValue<(Value, HTTPRespons
uuid: uuid,
request: request,
data: data,
duration: duration
duration: duration,
fileIDLine: configs.fileIDLine
)
configs.logger.log(
level: isError ? configs._errorLogLevel : configs.logLevel,
Expand All @@ -168,7 +170,8 @@ extension APIClientCaller where Result == AsyncThrowingValue<(Value, HTTPRespons
request: request,
data: data,
duration: duration,
error: error
error: error,
fileIDLine: configs.fileIDLine
)
configs.logger.log(level: configs._errorLogLevel, "\(message)")
}
Expand Down
48 changes: 20 additions & 28 deletions Sources/SwiftAPIClient/Types/LoggingComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct LoggingComponents: OptionSet {
public static let query = LoggingComponents(rawValue: 1 << 8)
public static let uuid = LoggingComponents(rawValue: 1 << 9)
public static let location = LoggingComponents(rawValue: 1 << 10)
public static let onRequest = LoggingComponents(rawValue: 1 << 11)

public static var url: LoggingComponents { [.path, .baseURL, .query] }

Expand Down Expand Up @@ -69,12 +70,12 @@ public extension LoggingComponents {
func requestMessage(
for request: HTTPRequestComponents,
uuid: UUID,
fileIDLine: FileIDLine
fileIDLine: FileIDLine?
) -> String {
guard !isEmpty else { return "" }
var message = "--> 🌐"
var isMultiline = false
if contains(.location) {
if contains(.location), let fileIDLine {
message = "\(fileIDLine.fileID)/\(fileIDLine.line)\n" + message
}
if contains(.uuid) {
Expand Down Expand Up @@ -116,7 +117,8 @@ public extension LoggingComponents {
request: HTTPRequestComponents? = nil,
data: Data?,
duration: TimeInterval,
error: Error? = nil
error: Error? = nil,
fileIDLine: FileIDLine?
) -> String {
responseMessage(
uuid: uuid,
Expand All @@ -125,7 +127,8 @@ public extension LoggingComponents {
data: data,
headers: response.headerFields,
duration: duration,
error: error
error: error,
fileIDLine: fileIDLine
)
}

Expand All @@ -136,13 +139,18 @@ public extension LoggingComponents {
data: Data?,
headers: HTTPFields = [:],
duration: TimeInterval? = nil,
error: Error? = nil
error: Error? = nil,
fileIDLine: FileIDLine?
) -> String {
guard !isEmpty else { return "" }
var message = "<-- "
if contains(.uuid) {
message = "[\(uuid.uuidString)]\n" + message
}
var message = "<-- "
if let request {
message = requestMessage(for: request, uuid: uuid, fileIDLine: fileIDLine) + "\n" + message
} else {
if contains(.uuid) {
message = "[\(uuid.uuidString)]\n" + message
}
}
switch (statusCode?.kind, error) {
case (_, .some), (.serverError, _), (.clientError, _), (.invalid, _):
message.append("🛑")
Expand All @@ -168,15 +176,6 @@ public extension LoggingComponents {
message += " (\(inBrackets.joined(separator: ", ")))"
}

if let request {
if contains(.method) {
message += " \(request.method.rawValue)"
}
if let url = request.url, !intersection(.url).isEmpty {
message += " \(urlString(url))"
}
}

if let error {
message += "\n❗️\(error.humanReadable)❗️"
isMultiline = true
Expand Down Expand Up @@ -205,21 +204,14 @@ public extension LoggingComponents {
duration: TimeInterval? = nil,
fileIDLine: FileIDLine? = nil
) -> String {
var message = contains(.uuid) ? "[\(uuid.uuidString)] " : ""
var message = contains(.uuid) && request == nil ? "[\(uuid.uuidString)] " : ""
if let duration, contains(.duration) {
message += "\(Int(duration * 1000))ms "
}

if let request {
if contains(.method) {
message += " \(request.method.rawValue)"
}
if let url = request.url, !intersection(.url).isEmpty {
message += " \(urlString(url))"
}
}

if let fileIDLine, contains(.location) {
message = requestMessage(for: request, uuid: uuid, fileIDLine: fileIDLine) + "\n" + message
} else if let fileIDLine, contains(.location) {
message = "\(fileIDLine.fileID)/\(fileIDLine.line)\n" + message
}
message += "❗️\(error.humanReadable)❗️"
Expand Down

0 comments on commit 706e4f0

Please sign in to comment.