Skip to content

Commit ce23a65

Browse files
committed
Merge branch 'main' into sebsto/fix_507
2 parents 0b111cd + de38324 commit ce23a65

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

Sources/AWSLambdaRuntime/FoundationSupport/Lambda+JSON.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import class Foundation.JSONDecoder
2323
import class Foundation.JSONEncoder
2424
#endif
2525

26+
import Logging
27+
2628
public struct LambdaJSONEventDecoder: LambdaEventDecoder {
2729
@usableFromInline let jsonDecoder: JSONDecoder
2830

@@ -87,10 +89,12 @@ extension LambdaRuntime {
8789
/// - Parameters:
8890
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
8991
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
92+
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
9093
/// - body: The handler in the form of a closure.
9194
public convenience init<Event: Decodable, Output>(
9295
decoder: JSONDecoder = JSONDecoder(),
9396
encoder: JSONEncoder = JSONEncoder(),
97+
logger: Logger = Logger(label: "LambdaRuntime"),
9498
body: sending @escaping (Event, LambdaContext) async throws -> Output
9599
)
96100
where
@@ -108,14 +112,16 @@ extension LambdaRuntime {
108112
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
109113
)
110114

111-
self.init(handler: handler)
115+
self.init(handler: handler, logger: logger)
112116
}
113117

114118
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a `Void` return type**.
115119
/// - Parameter body: The handler in the form of a closure.
116120
/// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
121+
/// - Parameter logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
117122
public convenience init<Event: Decodable>(
118123
decoder: JSONDecoder = JSONDecoder(),
124+
logger: Logger = Logger(label: "LambdaRuntime"),
119125
body: sending @escaping (Event, LambdaContext) async throws -> Void
120126
)
121127
where
@@ -132,7 +138,7 @@ extension LambdaRuntime {
132138
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
133139
)
134140

135-
self.init(handler: handler)
141+
self.init(handler: handler, logger: logger)
136142
}
137143
}
138144
#endif // trait: FoundationJSONSupport

Sources/AWSLambdaRuntime/LambdaHandlers.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Logging
1516
import NIOCore
1617

1718
/// 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<Event: Decodable, Output>: LambdaHandler {
175176

176177
extension LambdaRuntime {
177178
/// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure.
178-
/// - Parameter body: The handler in the form of a closure.
179+
/// - Parameter
180+
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
181+
/// - body: The handler in the form of a closure.
179182
public convenience init(
183+
logger: Logger = Logger(label: "LambdaRuntime"),
180184
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void
185+
181186
) where Handler == StreamingClosureHandler {
182-
self.init(handler: StreamingClosureHandler(body: body))
187+
self.init(handler: StreamingClosureHandler(body: body), logger: logger)
183188
}
184189

185190
/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a non-`Void` return type**, an encoder, and a decoder.
186191
/// - Parameters:
187192
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`.
188193
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type.
194+
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
189195
/// - body: The handler in the form of a closure.
190196
public convenience init<
191197
Event: Decodable,
@@ -195,6 +201,7 @@ extension LambdaRuntime {
195201
>(
196202
encoder: sending Encoder,
197203
decoder: sending Decoder,
204+
logger: Logger = Logger(label: "LambdaRuntime"),
198205
body: sending @escaping (Event, LambdaContext) async throws -> Output
199206
)
200207
where
@@ -214,15 +221,17 @@ extension LambdaRuntime {
214221
handler: streamingAdapter
215222
)
216223

217-
self.init(handler: codableWrapper)
224+
self.init(handler: codableWrapper, logger: logger)
218225
}
219226

220227
/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**, an encoder, and a decoder.
221228
/// - Parameters:
222229
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type.
230+
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
223231
/// - body: The handler in the form of a closure.
224232
public convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
225233
decoder: sending Decoder,
234+
logger: Logger = Logger(label: "LambdaRuntime"),
226235
body: sending @escaping (Event, LambdaContext) async throws -> Void
227236
)
228237
where
@@ -239,6 +248,6 @@ extension LambdaRuntime {
239248
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
240249
)
241250

242-
self.init(handler: handler)
251+
self.init(handler: handler, logger: logger)
243252
}
244253
}

Sources/AWSLambdaRuntime/LambdaRuntime.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
5252
// developers have to wait for AWS Lambda to dispose and recreate a runtime environment to pickup a change
5353
// this approach is less flexible but more performant than reading the value of the environment variable at each invocation
5454
var log = logger
55-
log.logLevel = Lambda.env("LOG_LEVEL").flatMap(Logger.Level.init) ?? .info
55+
56+
// use the LOG_LEVEL environment variable to set the log level.
57+
// if the environment variable is not set, use the default log level from the logger provided
58+
log.logLevel = Lambda.env("LOG_LEVEL").flatMap(Logger.Level.init) ?? logger.logLevel
59+
5660
self.logger = log
5761
self.logger.debug("LambdaRuntime initialized")
5862
}

0 commit comments

Comments
 (0)