diff --git a/Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift b/Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift index 9bd4d30f..9b47bd7d 100644 --- a/Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift +++ b/Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift @@ -23,6 +23,8 @@ import class Foundation.JSONDecoder import class Foundation.JSONEncoder #endif +import Logging + public struct LambdaJSONEventDecoder: LambdaEventDecoder { @usableFromInline let jsonDecoder: JSONDecoder @@ -87,10 +89,12 @@ extension LambdaRuntime { /// - Parameters: /// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default. /// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default. + /// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime". /// - body: The handler in the form of a closure. public convenience init( decoder: JSONDecoder = JSONDecoder(), encoder: JSONEncoder = JSONEncoder(), + logger: Logger = Logger(label: "LambdaRuntime"), body: sending @escaping (Event, LambdaContext) async throws -> Output ) where @@ -108,14 +112,16 @@ extension LambdaRuntime { handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body)) ) - self.init(handler: handler) + self.init(handler: handler, logger: logger) } /// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a `Void` return type**. /// - Parameter body: The handler in the form of a closure. /// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default. + /// - Parameter logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime". public convenience init( decoder: JSONDecoder = JSONDecoder(), + logger: Logger = Logger(label: "LambdaRuntime"), body: sending @escaping (Event, LambdaContext) async throws -> Void ) where @@ -132,7 +138,7 @@ extension LambdaRuntime { handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body)) ) - self.init(handler: handler) + self.init(handler: handler, logger: logger) } } #endif // trait: FoundationJSONSupport diff --git a/Sources/AWSLambdaRuntime/LambdaHandlers.swift b/Sources/AWSLambdaRuntime/LambdaHandlers.swift index 4b5975cb..cc23fa4a 100644 --- a/Sources/AWSLambdaRuntime/LambdaHandlers.swift +++ b/Sources/AWSLambdaRuntime/LambdaHandlers.swift @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +import Logging import NIOCore /// The base handler protocol that receives a `ByteBuffer` representing the incoming event and returns the response as a `ByteBuffer` too. @@ -175,17 +176,22 @@ public struct ClosureHandler: LambdaHandler { extension LambdaRuntime { /// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure. - /// - Parameter body: The handler in the form of a closure. + /// - Parameter + /// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime". + /// - body: The handler in the form of a closure. public convenience init( + logger: Logger = Logger(label: "LambdaRuntime"), body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void + ) where Handler == StreamingClosureHandler { - self.init(handler: StreamingClosureHandler(body: body)) + self.init(handler: StreamingClosureHandler(body: body), logger: logger) } /// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a non-`Void` return type**, an encoder, and a decoder. /// - Parameters: /// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. /// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. + /// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime". /// - body: The handler in the form of a closure. public convenience init< Event: Decodable, @@ -195,6 +201,7 @@ extension LambdaRuntime { >( encoder: sending Encoder, decoder: sending Decoder, + logger: Logger = Logger(label: "LambdaRuntime"), body: sending @escaping (Event, LambdaContext) async throws -> Output ) where @@ -214,15 +221,17 @@ extension LambdaRuntime { handler: streamingAdapter ) - self.init(handler: codableWrapper) + self.init(handler: codableWrapper, logger: logger) } /// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**, an encoder, and a decoder. /// - Parameters: /// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. + /// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime". /// - body: The handler in the form of a closure. public convenience init( decoder: sending Decoder, + logger: Logger = Logger(label: "LambdaRuntime"), body: sending @escaping (Event, LambdaContext) async throws -> Void ) where @@ -239,6 +248,6 @@ extension LambdaRuntime { handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body)) ) - self.init(handler: handler) + self.init(handler: handler, logger: logger) } } diff --git a/Sources/AWSLambdaRuntime/LambdaRuntime.swift b/Sources/AWSLambdaRuntime/LambdaRuntime.swift index 7aba2812..d1d1674c 100644 --- a/Sources/AWSLambdaRuntime/LambdaRuntime.swift +++ b/Sources/AWSLambdaRuntime/LambdaRuntime.swift @@ -46,7 +46,11 @@ public final class LambdaRuntime: @unchecked Sendable where Handler: St // developers have to wait for AWS Lambda to dispose and recreate a runtime environment to pickup a change // this approach is less flexible but more performant than reading the value of the environment variable at each invocation var log = logger - log.logLevel = Lambda.env("LOG_LEVEL").flatMap(Logger.Level.init) ?? .info + + // use the LOG_LEVEL environment variable to set the log level. + // if the environment variable is not set, use the default log level from the logger provided + log.logLevel = Lambda.env("LOG_LEVEL").flatMap(Logger.Level.init) ?? logger.logLevel + self.logger = log self.logger.debug("LambdaRuntime initialized") }