Skip to content

Commit

Permalink
Logging level changes (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored May 4, 2021
1 parent 0ff4dd6 commit 8691bc4
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 24 deletions.
9 changes: 5 additions & 4 deletions Sources/SotoCore/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public final class AWSClient {
self.credentialProvider = credentialProviderFactory.createProvider(context: .init(
httpClient: httpClient,
eventLoop: httpClient.eventLoopGroup.next(),
logger: clientLogger
logger: clientLogger,
options: options
))

self.middlewares = middlewares
Expand Down Expand Up @@ -171,7 +172,7 @@ public final class AWSClient {
case .createNew, .createNewWithEventLoopGroup:
self.httpClient.shutdown(queue: queue) { error in
if let error = error {
self.clientLogger.error("Error shutting down HTTP client", metadata: [
self.clientLogger.log(level: self.options.errorLogLevel, "Error shutting down HTTP client", metadata: [
"aws-error": "\(error)",
])
}
Expand Down Expand Up @@ -230,7 +231,7 @@ public final class AWSClient {
/// - Parameter requestLogLevel:Log level used for request logging
public init(
requestLogLevel: Logger.Level = .debug,
errorLogLevel: Logger.Level = .info
errorLogLevel: Logger.Level = .debug
) {
self.requestLogLevel = requestLogLevel
self.errorLogLevel = errorLogLevel
Expand Down Expand Up @@ -570,7 +571,7 @@ extension AWSClient {
// if we can create an AWSResponse and create an error from it return that
if let awsResponse = try? AWSResponse(from: response, serviceProtocol: serviceConfig.serviceProtocol)
.applyMiddlewares(serviceConfig.middlewares + middlewares, config: serviceConfig),
let error = awsResponse.generateError(serviceConfig: serviceConfig, logger: logger)
let error = awsResponse.generateError(serviceConfig: serviceConfig, logLevel: options.errorLogLevel, logger: logger)
{
return error
} else {
Expand Down
2 changes: 2 additions & 0 deletions Sources/SotoCore/Credential/CredentialProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public struct CredentialProviderFactory {
public let eventLoop: EventLoop
/// The `Logger` attached to the AWSClient
public let logger: Logger
/// AWSClient options
public let options: AWSClient.Options
}

private let cb: (Context) -> CredentialProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class DeferredCredentialProvider: CredentialProvider {
.flatMapErrorThrowing { _ in throw CredentialProviderError.noProvider }
.map { credential in
self.credential = credential
context.logger.info("AWS credentials ready", metadata: ["aws-credential-provider": .string("\(self)")])
context.logger.debug("AWS credentials ready", metadata: ["aws-credential-provider": .string("\(self)")])
return credential
}
.cascade(to: self.startupPromise)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SotoCore/Credential/MetaDataCredentialProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ struct InstanceMetaDataClient: MetaDataClient {
func getMetaData(on eventLoop: EventLoop, logger: Logger) -> EventLoopFuture<InstanceMetaData> {
return getToken(on: eventLoop, logger: logger)
.map { token in
logger.info("Found IMDSv2 token")
logger.trace("Found IMDSv2 token")
return HTTPHeaders([(Self.TokenHeaderName, token)])
}
.flatMapErrorThrowing { _ in
// If we didn't find a session key then assume we are running IMDSv1.
// (we could be running from a Docker container and the hop count for the PUT
// request is still set to 1)
logger.info("Did not find IMDSv2 token, use IMDSv1")
logger.trace("Did not find IMDSv2 token, use IMDSv1")
return HTTPHeaders()
}
.flatMap { (headers) -> EventLoopFuture<(AWSHTTPResponse, HTTPHeaders)> in
Expand Down
4 changes: 2 additions & 2 deletions Sources/SotoCore/Credential/RotatingCredentialProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public final class RotatingCredentialProvider: CredentialProvider {
return future
}

logger.info("Refeshing AWS credentials", metadata: ["aws-credential-provider": .string("\(self)")])
logger.debug("Refeshing AWS credentials", metadata: ["aws-credential-provider": .string("\(self)")])

credentialFuture = self.provider.getCredential(on: eventLoop, logger: logger)
.map { (credential) -> (Credential) in
// update the internal credential locked
self.lock.withLock {
self.credentialFuture = nil
self.credential = credential
logger.info("AWS credentials ready", metadata: ["aws-credential-provider": .string("\(self)")])
logger.debug("AWS credentials ready", metadata: ["aws-credential-provider": .string("\(self)")])
}
return credential
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class RuntimeSelectorCredentialProvider: CredentialProviderSelector {
provider.getCredential(on: context.eventLoop, logger: context.logger).whenComplete { result in
switch result {
case .success:
context.logger.info("Select credential provider", metadata: ["aws-credential-provider": .string("\(provider)")])
context.logger.debug("Select credential provider", metadata: ["aws-credential-provider": .string("\(provider)")])
self.startupPromise.succeed(provider)
case .failure:
context.logger.log(level: context.options.errorLogLevel, "Select credential provider failed")
_setupInternalProvider(index + 1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SotoCore/Message/AWSResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public struct AWSResponse {
}

/// extract error code and message from AWSResponse
func generateError(serviceConfig: AWSServiceConfig, logger: Logger) -> Error? {
func generateError(serviceConfig: AWSServiceConfig, logLevel: Logger.Level = .info, logger: Logger) -> Error? {
var apiError: APIError?
switch serviceConfig.serviceProtocol {
case .restjson:
Expand Down Expand Up @@ -225,7 +225,7 @@ public struct AWSResponse {
code = String(code[code.index(index, offsetBy: 1)...])
}

logger.error("AWS Error", metadata: [
logger.log(level: logLevel, "AWS Error", metadata: [
"aws-error-code": .string(code),
"aws-error-message": .string(errorMessage.message),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConfigFileCredentialProviderTests: XCTestCase {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = eventLoopGroup.next()
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoop))
return (.init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger), eventLoopGroup, httpClient)
return (.init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init()), eventLoopGroup, httpClient)
}

func testCredentialProviderStatic() {
Expand Down Expand Up @@ -89,7 +89,7 @@ class ConfigFileCredentialProviderTests: XCTestCase {
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
let factory = CredentialProviderFactory.configFile(credentialsFilePath: filenameURL.path)

let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger))
let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init()))

var credential: Credential?
XCTAssertNoThrow(credential = try provider.getCredential(on: eventLoop, logger: TestEnvironment.logger).wait())
Expand Down Expand Up @@ -118,7 +118,7 @@ class ConfigFileCredentialProviderTests: XCTestCase {
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
let factory = CredentialProviderFactory.configFile(credentialsFilePath: filenameURL.path)

let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger))
let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init()))

var credential: Credential?
XCTAssertNoThrow(credential = try provider.getCredential(on: eventLoop, logger: TestEnvironment.logger).wait())
Expand All @@ -137,7 +137,7 @@ class ConfigFileCredentialProviderTests: XCTestCase {
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
let factory = CredentialProviderFactory.configFile(credentialsFilePath: filenameURL.path)

let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger))
let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init()))

XCTAssertThrowsError(_ = try provider.getCredential(on: eventLoop, logger: TestEnvironment.logger).wait()) { error in
print("\(error)")
Expand Down
2 changes: 1 addition & 1 deletion Tests/SotoCoreTests/Credential/ConfigFileLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConfigFileLoadersTests: XCTestCase {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = eventLoopGroup.next()
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoop))
return (.init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger), eventLoopGroup, httpClient)
return (.init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init()), eventLoopGroup, httpClient)
}

func save(content: String, prefix: String) throws -> String {
Expand Down
6 changes: 3 additions & 3 deletions Tests/SotoCoreTests/Credential/CredentialProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CredentialProviderTests: XCTestCase {
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
let eventLoop = eventLoopGroup.next()
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger)
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init())
let deferredProvider = DeferredCredentialProvider(context: context, provider: MyCredentialProvider())
XCTAssertNoThrow(_ = try deferredProvider.getCredential(on: eventLoop, logger: TestEnvironment.logger).wait())
XCTAssertNoThrow(_ = try deferredProvider.getCredential(on: eventLoop, logger: TestEnvironment.logger).wait())
Expand All @@ -75,7 +75,7 @@ class CredentialProviderTests: XCTestCase {
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
let factory = CredentialProviderFactory.configFile(credentialsFilePath: filenameURL.path)

let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger))
let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init()))

var credential: Credential?
XCTAssertNoThrow(credential = try provider.getCredential(on: eventLoop, logger: TestEnvironment.logger).wait())
Expand All @@ -94,7 +94,7 @@ class CredentialProviderTests: XCTestCase {
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) }
let factory = CredentialProviderFactory.configFile(credentialsFilePath: filenameURL.path)

let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger))
let provider = factory.createProvider(context: .init(httpClient: httpClient, eventLoop: eventLoop, logger: TestEnvironment.logger, options: .init()))

XCTAssertThrowsError(_ = try provider.getCredential(on: eventLoop, logger: TestEnvironment.logger).wait()) { error in
print("\(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class RotatingCredentialProviderTests: XCTestCase {
hitCount += 1
return $0.makeSucceededFuture(cred)
}
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: loop, logger: Logger(label: "soto"))
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: loop, logger: Logger(label: "soto"), options: .init())
let provider = RotatingCredentialProvider(context: context, provider: client)

// get credentials for first time
Expand Down Expand Up @@ -98,7 +98,7 @@ class RotatingCredentialProviderTests: XCTestCase {
hitCount += 1
return promise.futureResult
}
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: loop, logger: TestEnvironment.logger)
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: loop, logger: TestEnvironment.logger, options: .init())
let provider = RotatingCredentialProvider(context: context, provider: client)

var resultFutures = [EventLoopFuture<Void>]()
Expand Down Expand Up @@ -159,7 +159,7 @@ class RotatingCredentialProviderTests: XCTestCase {
)
return eventLoop.makeSucceededFuture(cred)
}
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: loop, logger: TestEnvironment.logger)
let context = CredentialProviderFactory.Context(httpClient: httpClient, eventLoop: loop, logger: TestEnvironment.logger, options: .init())
let provider = RotatingCredentialProvider(context: context, provider: client)
XCTAssertNoThrow(_ = try provider.getCredential(on: loop, logger: TestEnvironment.logger).wait())
hitCount = 0
Expand Down
3 changes: 2 additions & 1 deletion Tests/SotoCoreTests/LoggingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class LoggingTests: XCTestCase {
defer { XCTAssertNoThrow(try server.stop()) }
let client = AWSClient(
credentialProvider: .static(accessKeyId: "foo", secretAccessKey: "bar"),
options: .init(requestLogLevel: .debug, errorLogLevel: .info),
httpClientProvider: .createNew,
logger: logger
)
Expand All @@ -117,7 +118,7 @@ class LoggingTests: XCTestCase {

XCTAssertThrowsError(_ = try response.wait())
XCTAssertEqual(logCollection.filter(metadata: "aws-error-code", with: "AccessDenied").first?.message, "AWS Error")
XCTAssertEqual(logCollection.filter(metadata: "aws-error-code", with: "AccessDenied").first?.level, .error)
XCTAssertEqual(logCollection.filter(metadata: "aws-error-code", with: "AccessDenied").first?.level, .info)
}

func testRetryRequest() {
Expand Down

0 comments on commit 8691bc4

Please sign in to comment.