Skip to content

Commit

Permalink
1.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed May 20, 2024
1 parent 91089e4 commit a469639
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Sources/SwiftAPIClient/APIClientCaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ public extension APIClient {
} catch {
try withConfigs { configs in
let fileIDLine = configs.fileIDLine ?? FileIDLine(fileID: fileID, line: line)
if !configs.loggingComponents.isEmpty {
let message = configs.loggingComponents.errorMessage(
if !configs._errorLoggingComponents.isEmpty {
let message = configs._errorLoggingComponents.errorMessage(
uuid: uuid,
error: error,
fileIDLine: fileIDLine
)
configs.logger.log(level: configs.logLevel, "\(message)")
configs.logger.log(level: configs._errorLogLevel, "\(message)")
}
if configs.reportMetrics {
updateTotalErrorsMetrics(for: nil)
Expand Down
23 changes: 14 additions & 9 deletions Sources/SwiftAPIClient/Clients/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ extension APIClientCaller where Result == AsyncThrowingValue<Value> {
(value, response) = try await configs.httpClientMiddleware.execute(request: request, configs: configs, next: task)
} catch {
let duration = Date().timeIntervalSince(start)
if !configs.loggingComponents.isEmpty {
let message = configs.loggingComponents.errorMessage(
if !configs._errorLoggingComponents.isEmpty {
let message = configs._errorLoggingComponents.errorMessage(
uuid: uuid,
error: error,
duration: duration
)
configs.logger.log(level: configs.logLevel, "\(message)")
configs.logger.log(level: configs._errorLogLevel, "\(message)")
}
if configs.reportMetrics {
updateHTTPMetrics(for: request, status: nil, duration: duration, successful: false)
Expand All @@ -118,29 +118,34 @@ extension APIClientCaller where Result == AsyncThrowingValue<Value> {
let result = try serialize(value) {
try validate(value, response, configs)
}
if !configs.loggingComponents.isEmpty {
let message = configs.loggingComponents.responseMessage(
let isError = response.status.kind.isError
let logComponents = isError ? configs._errorLoggingComponents : configs.loggingComponents
if !logComponents.isEmpty {
let message = logComponents.responseMessage(
for: response,
uuid: uuid,
data: data,
duration: duration
)
configs.logger.log(level: configs.logLevel, "\(message)")
configs.logger.log(
level: isError ? configs._errorLogLevel : configs.logLevel,
"\(message)"
)
}
if configs.reportMetrics {
updateHTTPMetrics(for: request, status: response.status, duration: duration, successful: true)
}
return result
} catch {
if !configs.loggingComponents.isEmpty {
let message = configs.loggingComponents.responseMessage(
if !configs._errorLoggingComponents.isEmpty {
let message = configs._errorLoggingComponents.responseMessage(
for: response,
uuid: uuid,
data: data,
duration: duration,
error: error
)
configs.logger.log(level: configs.logLevel, "\(message)")
configs.logger.log(level: configs._errorLogLevel, "\(message)")
}
if configs.reportMetrics {
updateHTTPMetrics(for: request, status: response.status, duration: duration, successful: false)
Expand Down
39 changes: 39 additions & 0 deletions Sources/SwiftAPIClient/Modifiers/LoggingModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@ public extension APIClient {
configs(\.logLevel, level)
}

/// Sets the logging level for error logs.
/// - Parameter level: The `Logger.Level` to be used for error logs. When `nil`, `logLevel` is used.
/// - Returns: An instance of `APIClient` configured with the specified error logging level.
func errorLog(level: Logger.Level?) -> APIClient {
configs(\.errorLogLevel, level)
}

/// Sets the components to be logged.
func loggingComponents(_ components: LoggingComponents) -> APIClient {
configs(\.loggingComponents, components)
}

/// Sets the components to be logged for error logs.
/// - Parameter components: The `LoggingComponents` to be used for error logs. When `nil`, `loggingComponents` is used.
/// - Returns: An instance of `APIClient` configured with the specified error logging components.
func errorLoggingComponents(_ components: LoggingComponents?) -> APIClient {
configs(\.errorLogginComponents, components)
}
}

public extension APIClient.Configs {
Expand All @@ -39,12 +53,37 @@ public extension APIClient.Configs {
set { self[\.logLevel] = newValue }
}

/// The log level to be used for error logs.
/// - Returns: A `Logger.Level` used in error logs.
var errorLogLevel: Logger.Level? {
get { self[\.errorLogLevel] ?? nil }
set { self[\.errorLogLevel] = newValue }
}

/// The components to be logged.
/// - Returns: A `LoggingComponents` instance configured with the appropriate components.
var loggingComponents: LoggingComponents {
get { self[\.loggingComponents] ?? .standart }
set { self[\.loggingComponents] = newValue }
}

/// The components to be logged for error logs.
/// - Returns: A `LoggingComponents` instance configured with the appropriate components.
var errorLogginComponents: LoggingComponents? {
get { self[\.errorLogginComponents] ?? nil }
set { self[\.errorLogginComponents] = newValue }
}
}

extension APIClient.Configs {

var _errorLogLevel: Logger.Level {
errorLogLevel ?? logLevel
}

var _errorLoggingComponents: LoggingComponents {
errorLogginComponents ?? loggingComponents
}
}

private let defaultLogger = Logger(label: "swift-api-client")

0 comments on commit a469639

Please sign in to comment.