Skip to content

[core] Give user the possibility to pass their logger to the lambda runtime #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
#endif

import Logging

public struct LambdaJSONEventDecoder: LambdaEventDecoder {
@usableFromInline let jsonDecoder: JSONDecoder

Expand Down Expand Up @@ -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<Event: Decodable, Output>(
decoder: JSONDecoder = JSONDecoder(),
encoder: JSONEncoder = JSONEncoder(),
logger: Logger = Logger(label: "LambdaRuntime"),
body: sending @escaping (Event, LambdaContext) async throws -> Output
)
where
Expand All @@ -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<Event: Decodable>(
decoder: JSONDecoder = JSONDecoder(),
logger: Logger = Logger(label: "LambdaRuntime"),
body: sending @escaping (Event, LambdaContext) async throws -> Void
)
where
Expand All @@ -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
17 changes: 13 additions & 4 deletions Sources/AWSLambdaRuntime/LambdaHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -175,17 +176,22 @@ public struct ClosureHandler<Event: Decodable, Output>: 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,
Expand All @@ -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
Expand All @@ -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<Event: Decodable, Decoder: LambdaEventDecoder>(
decoder: sending Decoder,
logger: Logger = Logger(label: "LambdaRuntime"),
body: sending @escaping (Event, LambdaContext) async throws -> Void
)
where
Expand All @@ -239,6 +248,6 @@ extension LambdaRuntime {
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
)

self.init(handler: handler)
self.init(handler: handler, logger: logger)
}
}
6 changes: 5 additions & 1 deletion Sources/AWSLambdaRuntime/LambdaRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public final class LambdaRuntime<Handler>: @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")
}
Expand Down
Loading