diff --git a/Sources/SwiftAPIClient/APIClientCaller.swift b/Sources/SwiftAPIClient/APIClientCaller.swift index 8a15745..f425d68 100644 --- a/Sources/SwiftAPIClient/APIClientCaller.swift +++ b/Sources/SwiftAPIClient/APIClientCaller.swift @@ -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) diff --git a/Sources/SwiftAPIClient/Clients/HTTPClient.swift b/Sources/SwiftAPIClient/Clients/HTTPClient.swift index 46210f2..6ac4693 100644 --- a/Sources/SwiftAPIClient/Clients/HTTPClient.swift +++ b/Sources/SwiftAPIClient/Clients/HTTPClient.swift @@ -99,13 +99,13 @@ extension APIClientCaller where Result == AsyncThrowingValue { (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) @@ -118,29 +118,34 @@ extension APIClientCaller where Result == AsyncThrowingValue { 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) diff --git a/Sources/SwiftAPIClient/Modifiers/LoggingModifier.swift b/Sources/SwiftAPIClient/Modifiers/LoggingModifier.swift index 9d97c24..21e1da5 100644 --- a/Sources/SwiftAPIClient/Modifiers/LoggingModifier.swift +++ b/Sources/SwiftAPIClient/Modifiers/LoggingModifier.swift @@ -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 { @@ -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")