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 2 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 @@ -85,10 +87,12 @@ extension LambdaCodableAdapter {
extension LambdaRuntime {
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a non-`Void` return type**.
/// - Parameters:
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - 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.
/// - body: The handler in the form of a closure.
public convenience init<Event: Decodable, Output>(
logger: Logger = Logger(label: "LambdaRuntime"),
decoder: JSONDecoder = JSONDecoder(),
encoder: JSONEncoder = JSONEncoder(),
body: sending @escaping (Event, LambdaContext) async throws -> Output
Expand All @@ -108,13 +112,15 @@ 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 logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - 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.
public convenience init<Event: Decodable>(
logger: Logger = Logger(label: "LambdaRuntime"),
decoder: JSONDecoder = JSONDecoder(),
body: sending @escaping (Event, LambdaContext) async throws -> Void
)
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
9 changes: 7 additions & 2 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,11 +176,15 @@ 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.
Expand Down
Loading