Skip to content

Commit

Permalink
Add additional logs
Browse files Browse the repository at this point in the history
  • Loading branch information
trueangle committed Aug 25, 2024
1 parent 673cf26 commit a679d6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.trueangle.knative.lambda.runtime.api.LambdaClient
import io.github.trueangle.knative.lambda.runtime.handler.LambdaBufferedHandler
import io.github.trueangle.knative.lambda.runtime.handler.LambdaHandler
import io.github.trueangle.knative.lambda.runtime.handler.LambdaStreamHandler
import io.github.trueangle.knative.lambda.runtime.log.KtorLogger
import io.github.trueangle.knative.lambda.runtime.log.Log
import io.github.trueangle.knative.lambda.runtime.log.LogLevel
import io.ktor.client.HttpClient
Expand Down Expand Up @@ -33,10 +34,10 @@ object LambdaRuntime {
json(Json { explicitNulls = false })
}
install(Logging) {
level = if (Log.currentLogLevel == LogLevel.TRACE) KtorLogLevel.ALL else KtorLogLevel.NONE
logger = object : Logger {
override fun log(message: String) = Log.trace(message)
}
val kLogger = KtorLogger()
level = kLogger.getLevel()
logger = kLogger

filter { it.headers.contains("Lambda-Runtime-Function-Response-Mode") }
}
}
Expand Down Expand Up @@ -86,7 +87,6 @@ object LambdaRuntime {
val response = bufferedResponse(context) { handler.handleRequest(event, context) }

Log.info("$handlerName invocation completed")
Log.trace("$handlerName's buffered response: $response")

client.sendResponse(context, response, outputTypeInfo)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ internal inline fun streamingResponse(crossinline handler: suspend (ByteWriteCha
try {
handler(channel)
} catch (e: Exception) {
Log.warn(e)
Log.warn("Exception occurred on streaming: " + e.message)

channel.writeStringUtf8(e.toTrailer())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.trueangle.knative.lambda.runtime.log

internal fun Throwable.prettyPrint() = buildString {
internal fun Throwable.prettyPrint(includeStackTrace: Boolean = true) = buildString {
append("An exception occurred:\n")
message?.let {
append("Message: ${message}\n")
}
append("Stack Trace:\n")
append(stackTraceToString())
if (includeStackTrace) {
append("Stack Trace:\n")
append(stackTraceToString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.trueangle.knative.lambda.runtime.log

import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.LogLevel as KtorLogLevel

internal class KtorLogger : Logger {
override fun log(message: String) = when (getLevel()) {
KtorLogLevel.ALL -> Log.trace(message)
KtorLogLevel.BODY -> Log.debug(message)
KtorLogLevel.INFO -> Log.info(message)
else -> Unit
}

fun getLevel() = when (Log.currentLogLevel) {
LogLevel.TRACE -> KtorLogLevel.ALL
LogLevel.DEBUG -> KtorLogLevel.BODY
else -> KtorLogLevel.NONE
}
}

0 comments on commit a679d6d

Please sign in to comment.