Noa*_*ein 5 logging kotlin ktor
当我使用 Retrofit 时,我可以轻松创建日志拦截器来查看 logcat 上带有正文和标头的调用:
val loggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
val httpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
Run Code Online (Sandbox Code Playgroud)
进而
val retrofit = retrofit2.Retrofit.Builder()
.client(httpClient.build())
Run Code Online (Sandbox Code Playgroud)
现在,我们切换到 Ktor,我不知道我不知道如何
addInterceptor()?install(CallLogging)][2] 没有任何反应..有什么建议或例子吗?
[2] 来源:
fun Application.main() {
install(CallLogging){
level = Level.TRACE
}
}
Run Code Online (Sandbox Code Playgroud)
您配置了日志记录吗?Ktor 仅提供 slf4j api。当您启动服务器时,您应该看到以下内容:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Run Code Online (Sandbox Code Playgroud)
这意味着,您需要将记录器实现放在类路径上。
最快的选择可能是implementation 'org.slf4j:slf4j-simple:1.7.26'.
现在您应该从服务器获取一些日志。
但是,默认情况下,仅记录呼叫。
不记录标头和正文。
IIRC 身体只能被消耗一次,但也许有所改变。
可以使用自定义拦截器记录标头。
例如:
call.request.headers.entries()
.map { it.key to it.value.joinToString(",") }
.joinToString(";") { "${it.first}:${it.second}" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5482 次 |
| 最近记录: |