Skip to content

Commit

Permalink
chore: Misc. logging changes (#797)
Browse files Browse the repository at this point in the history
* Add .none ClientLogMode and make it defualt to make logging opt-in.

* Adjust logging levels for URL session HTTP client.

* Add public init and set default log level for initialize() method to .error.

* Fix SwiftLogger::log method to pass in the designated log level (self.level) to the underlying Logger::log method, instead of wrongly copying the log level passed into SwiftLogger::log.

---------

Co-authored-by: Sichan Yoo <[email protected]>
  • Loading branch information
sichanyoo and Sichan Yoo committed Aug 13, 2024
1 parent a6d1331 commit 896e95a
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public protocol DefaultClientConfiguration: ClientConfiguration {

/// The log mode to use for client logging.
///
/// If none is provided, `.request` will be used.
/// If none is provided, `.none` will be used.
var clientLogMode: ClientLogMode { get set }

/// The network endpoint to use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct DefaultSDKRuntimeConfiguration<DefaultSDKRuntimeRetryStrategy: Ret

/// The log mode to use for client logging.
///
/// If none is provided, `.request` will be used.
/// If none is provided, `.none` will be used.
public var clientLogMode: ClientLogMode

/// The network endpoint to use.
Expand Down Expand Up @@ -126,8 +126,8 @@ public extension DefaultSDKRuntimeConfiguration {

/// The log mode to use when none is provided
///
/// Defaults to `.request`.
static var defaultClientLogMode: ClientLogMode { .requestWithoutAuthorizationHeader }
/// Defaults to `.none`.
static var defaultClientLogMode: ClientLogMode { .none }

static var defaultAuthSchemeResolver: AuthSchemeResolver { DefaultAuthSchemeResolver() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class FoundationStreamBridge: NSObject, StreamDelegate {
// `writeCount` will be a positive number if bytes were written.
// Remove the written bytes from the front of the buffer.
if writeCount > 0 {
logger.info("FoundationStreamBridge: wrote \(writeCount) bytes to request body")
logger.debug("FoundationStreamBridge: wrote \(writeCount) bytes to request body")
buffer.removeFirst(writeCount)
// TICK - smithy.client.http.bytes_sent
var attributes = Attributes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ public final class URLSessionHTTPClient: HTTPClient {
guard let tlsOptions = tlsOptions, tlsOptions.useSelfSignedCertificate,
let certFile = tlsOptions.certificate,
let serverTrust = challenge.protectionSpace.serverTrust else {
logger.error(
logger.info(
"Either TLSOptions not set or missing values! Using default trust store."
)
completionHandler(.performDefaultHandling, nil)
return
}

guard let customRoot = Bundle.main.certificate(named: certFile) else {
logger.error("Certificate not found! Using default trust store.")
logger.info("Certificate not found! Using default trust store.")
completionHandler(.performDefaultHandling, nil)
return
}
Expand All @@ -232,7 +232,7 @@ public final class URLSessionHTTPClient: HTTPClient {
guard let tlsOptions, tlsOptions.useProvidedKeystore,
let keystoreName = tlsOptions.pkcs12Path,
let keystorePasword = tlsOptions.pkcs12Password else {
logger.error(
logger.info(
"Either TLSOptions not set or missing values! Using default keystore."
)
completionHandler(.performDefaultHandling, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

public enum ClientLogMode {
case none
case request
case requestWithBody
case response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public actor SDKLoggingSystem {
private var isInitialized = false
private var factories: [String: SDKLogHandlerFactory] = [:]

public init() {}

public func add(logHandlerFactory: SDKLogHandlerFactory) {
let label = logHandlerFactory.label
factories[label] = logHandlerFactory
}

public func initialize(defaultLogLevel: SDKLogLevel = .info) async {
public func initialize(defaultLogLevel: SDKLogLevel = .error) async {
if isInitialized { return } else { isInitialized = true }
let ptr = factories
LoggingSystem.bootstrap { label in
Expand Down
8 changes: 7 additions & 1 deletion Sources/Smithy/Logging/SwiftLog+LogAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public struct SwiftLogger: LogAgent {
self.logLevel = LogAgentLevel.info
}

public init(label: String, logLevel: LogAgentLevel) {
self.label = label
self.logger = Logger(label: label)
self.logLevel = logLevel
}

public var level: LogAgentLevel {
get {
return logLevel
Expand All @@ -39,7 +45,7 @@ public struct SwiftLogger: LogAgent {
let mappedDict = metadata?.mapValues { (value) -> Logger.MetadataValue in
return Logger.MetadataValue.string(value)
}
self.logger.log(level: logLevel.toLoggerLevel(),
self.logger.log(level: level.toLoggerLevel(),
Logger.Message(stringLiteral: message),
metadata: mappedDict,
source: source,
Expand Down

0 comments on commit 896e95a

Please sign in to comment.