Logging 如何为KTOR添加日志拦截器?
当我使用改型时,我可以很容易地创建日志拦截器来查看logcat上带有主体和标题的调用:Logging 如何为KTOR添加日志拦截器?,logging,kotlin,ktor,Logging,Kotlin,Ktor,当我使用改型时,我可以很容易地创建日志拦截器来查看logcat上带有主体和标题的调用: val loggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY) val httpClient = OkHttpClient.Builder() .addInterceptor(loggingInterceptor) 然后
val loggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
val httpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
然后
val retrofit = retrofit2.Retrofit.Builder()
.client(httpClient.build())
现在,我不知道
我不知道怎么做
- 要使用
李>addInterceptor()
- 而对于[
][2]什么都不会发生install(CallLogging)
fun Application.main() {
install(CallLogging){
level = Level.TRACE
}
}
您是否配置了日志记录?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.
这意味着,您需要在类路径上放置一个记录器实现
最快的选项可能是实现'org.slf4j:slf4j simple:1.7.26'
现在,您应该从服务器获取一些日志
但是,默认情况下,只记录调用
未记录标题和正文
IIRC身体只能被消耗一次,但这可能会有所改变
可以使用自定义拦截器记录标题
例如:
call.request.headers.entries()
.map { it.key to it.value.joinToString(",") }
.joinToString(";") { "${it.first}:${it.second}" }
由于您正在将呼叫记录功能级别配置为跟踪,因此需要将日志系统(无论您使用的是什么sl4j提供程序)配置为将ktor.application记录为跟踪。例如,对于log4j2.xml:
导入io.ktor.features时遇到问题什么问题?@TimCastelijns io.ktor.features来自哪里?显然,实现“io.ktor:ktor features:$ktor_version”对meit不起作用。它似乎是内置的“此功能在类io.ktor.features.CallLogging中定义,不需要其他工件。”请尝试忽略不是问题所在的importNo。我有'implementation'io.ktor:ktor客户端okhttp:$ktor_version'`仅限'。所以我添加了:`implementation“io.ktor:ktor服务器netty:$ktor_version”`