Skip to content

Commit

Permalink
1.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed May 28, 2024
1 parent 7a18ae3 commit e006b23
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Sources/SwiftAPIClient/Clients/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ extension APIClientCaller where Result == AsyncThrowingValue<Value> {
let message = configs._errorLoggingComponents.errorMessage(
uuid: uuid,
error: error,
request: request,
duration: duration
)
configs.logger.log(level: configs._errorLogLevel, "\(message)")
Expand All @@ -124,6 +125,7 @@ extension APIClientCaller where Result == AsyncThrowingValue<Value> {
let message = logComponents.responseMessage(
for: response,
uuid: uuid,
request: request,
data: data,
duration: duration
)
Expand All @@ -141,6 +143,7 @@ extension APIClientCaller where Result == AsyncThrowingValue<Value> {
let message = configs._errorLoggingComponents.responseMessage(
for: response,
uuid: uuid,
request: request,
data: data,
duration: duration,
error: error
Expand Down
29 changes: 26 additions & 3 deletions Sources/SwiftAPIClient/Types/LoggingComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct LoggingComponents: OptionSet {
/// Example:
/// ```
/// --> POST /greeting (3-byte body)
/// <-- ✅ 200 OK (22ms, 6-byte body)
/// <-- ✅ 200 OK (22ms, 6-byte body) /greeting
/// ```
public static var basic: LoggingComponents { [.method, .path, .query, .bodySize, .duration, .statusCode] }

Expand All @@ -38,7 +38,7 @@ public struct LoggingComponents: OptionSet {
/// Content-Type: application/json
/// --> END PUT
/// [29CDD5AE-1A5D-4135-B76E-52A8973985E4]
/// <-- ✅ 200 OK (0ms, 15-byte body)
/// <-- ✅ 200 OK (0ms, 15-byte body) /petstore
/// ```
public static var standart: LoggingComponents { [.basic, .headers, .location, .uuid] }

Expand All @@ -51,7 +51,7 @@ public struct LoggingComponents: OptionSet {
/// {"id":"666A6886-70C4-454C-BD2D-F36B1B2F7F95"}
/// --> END PUT
/// [9F252BDC-1F9E-4F3D-8C46-1F984C10F4EB]
/// <-- ✅ 200 OK (0ms, 17-byte body)
/// <-- ✅ 200 OK (0ms, 17-byte body) https://example.com/petstore
/// {"name": "Candy"}
/// <-- END
/// ```
Expand Down Expand Up @@ -113,12 +113,14 @@ public extension LoggingComponents {
func responseMessage(
for response: HTTPResponse,
uuid: UUID,
request: HTTPRequestComponents? = nil,
data: Data?,
duration: TimeInterval,
error: Error? = nil
) -> String {
responseMessage(
uuid: uuid,
request: request,
statusCode: response.status,
data: data,
headers: response.headerFields,
Expand All @@ -129,6 +131,7 @@ public extension LoggingComponents {

func responseMessage(
uuid: UUID,
request: HTTPRequestComponents? = nil,
statusCode: HTTPResponse.Status? = nil,
data: Data?,
headers: HTTPFields = [:],
Expand Down Expand Up @@ -164,6 +167,15 @@ public extension LoggingComponents {
if !inBrackets.isEmpty {
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)❗️"
Expand All @@ -189,13 +201,24 @@ public extension LoggingComponents {
func errorMessage(
uuid: UUID,
error: Error,
request: HTTPRequestComponents? = nil,
duration: TimeInterval? = nil,
fileIDLine: FileIDLine? = nil
) -> String {
var message = contains(.uuid) ? "[\(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 = "\(fileIDLine.fileID)/\(fileIDLine.line)\n" + message
}
Expand Down

0 comments on commit e006b23

Please sign in to comment.