File tree Expand file tree Collapse file tree 4 files changed +17
-4
lines changed
lambda-runtime/src/commonMain/kotlin/io/github/trueangle/knative/lambda/runtime Expand file tree Collapse file tree 4 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package io.github.trueangle.knative.lambda.runtime
3
3
import io.github.trueangle.knative.lambda.runtime.LambdaEnvironmentException.NonRecoverableStateException
4
4
import io.github.trueangle.knative.lambda.runtime.api.Context
5
5
import io.github.trueangle.knative.lambda.runtime.api.LambdaClient
6
+ import io.github.trueangle.knative.lambda.runtime.api.dto.LogMessageDto
6
7
import io.github.trueangle.knative.lambda.runtime.handler.LambdaBufferedHandler
7
8
import io.github.trueangle.knative.lambda.runtime.handler.LambdaHandler
8
9
import io.github.trueangle.knative.lambda.runtime.handler.LambdaStreamHandler
@@ -23,6 +24,7 @@ import io.ktor.utils.io.ByteWriteChannel
23
24
import io.ktor.utils.io.writeStringUtf8
24
25
import kotlinx.coroutines.runBlocking
25
26
import kotlinx.serialization.json.Json
27
+ import kotlinx.serialization.modules.SerializersModule
26
28
import kotlin.system.exitProcess
27
29
import kotlin.time.TimeSource
28
30
import io.ktor.client.plugins.logging.LogLevel as KtorLogLevel
@@ -31,7 +33,9 @@ object LambdaRuntime {
31
33
private val httpClient = HttpClient (Curl ) {
32
34
install(HttpTimeout )
33
35
install(ContentNegotiation ) {
34
- json(Json { explicitNulls = false })
36
+ json(Json {
37
+ explicitNulls = false
38
+ })
35
39
}
36
40
install(Logging ) {
37
41
val kLogger = KtorLogger ()
Original file line number Diff line number Diff line change 1
1
package io.github.trueangle.knative.lambda.runtime.api.dto
2
2
3
3
import io.github.trueangle.knative.lambda.runtime.log.LogLevel
4
+ import kotlinx.serialization.Contextual
5
+ import kotlinx.serialization.KSerializer
4
6
import kotlinx.serialization.SerialName
5
7
import kotlinx.serialization.Serializable
8
+ import kotlinx.serialization.encoding.Decoder
9
+ import kotlinx.serialization.encoding.Encoder
6
10
7
11
@Serializable
8
12
internal data class LogMessageDto <T >(
Original file line number Diff line number Diff line change @@ -3,10 +3,13 @@ package io.github.trueangle.knative.lambda.runtime.log
3
3
import io.github.trueangle.knative.lambda.runtime.api.Context
4
4
import io.github.trueangle.knative.lambda.runtime.api.dto.LogMessageDto
5
5
import kotlinx.datetime.Clock
6
+ import kotlinx.serialization.InternalSerializationApi
6
7
import kotlinx.serialization.SerializationException
7
8
import kotlinx.serialization.encodeToString
8
9
import kotlinx.serialization.json.Json
10
+ import kotlinx.serialization.serializer
9
11
12
+ @OptIn(InternalSerializationApi ::class )
10
13
internal class JsonLogFormatter : LogFormatter {
11
14
private var requestContext: Context ? = null
12
15
@@ -15,12 +18,16 @@ internal class JsonLogFormatter : LogFormatter {
15
18
Json .encodeToString(
16
19
LogMessageDto (
17
20
timestamp = Clock .System .now().toString(),
18
- message = if (message is Throwable ) message.prettyPrint() else message,
21
+ message = if (message is Throwable ) message.prettyPrint() else message?.let {
22
+ it::class .serializer()
23
+ },
19
24
level = logLevel,
20
25
awsRequestId = requestContext?.awsRequestId
21
26
)
22
27
)
23
28
} catch (e: SerializationException ) {
29
+ Log .warn(" Log serialisation error: ${e.message} " )
30
+
24
31
Json .encodeToString(
25
32
LogMessageDto (
26
33
timestamp = Clock .System .now().toString(),
Original file line number Diff line number Diff line change @@ -7,14 +7,12 @@ internal class KtorLogger : Logger {
7
7
override fun log (message : String ) = when (getLevel()) {
8
8
KtorLogLevel .ALL -> Log .trace(message)
9
9
KtorLogLevel .BODY -> Log .debug(message)
10
- KtorLogLevel .INFO -> Log .info(message)
11
10
else -> Unit
12
11
}
13
12
14
13
fun getLevel () = when (Log .currentLogLevel) {
15
14
LogLevel .TRACE -> KtorLogLevel .ALL
16
15
LogLevel .DEBUG -> KtorLogLevel .BODY
17
- LogLevel .INFO -> KtorLogLevel .INFO
18
16
else -> KtorLogLevel .NONE
19
17
}
20
18
}
You can’t perform that action at this time.
0 commit comments