kotlin - 如何让 kotlin-logging (MicroUtils) 打印到子类名而不是抽象类名?
问题描述
我正在使用 kotlin-logging ( https://github.com/MicroUtils/kotlin-logging ),我希望我的记录器打印到子类名称而不是抽象类名称。示例我有这些课程
// in A.kt file:
import mu.KotlinLogging
private val logger = KotlinLogging.logger {}
abstract class A<T> {
fun someMethod(item: T?) {
logger.info("testing")
}
}
fun main() {
val b: A<String> = B()
b.someMethod("123")
}
// in B.kt file
import mu.KotlinLogging
private val logger = KotlinLogging.logger {}
class B : A<String>() {
}
它打印:[main] INFO A - testing
但我希望它打印:[main] INFO B - testing (其中 B 是子类名称)
解决方案
您可能希望将记录器发送到抽象类。
// in A.kt file:
import mu.KotlinLogging
abstract class A<T>(private val logger: KLogger) {
fun someMethod(item: T?) {
logger.info("testing")
}
}
fun main() {
val b: A<String> = B()
b.someMethod("123")
}
// in B.kt file
import mu.KotlinLogging
private val logger = KotlinLogging.logger {}
class B : A<String>(logger) {
}
结果: [main] INFO B - testing
推荐阅读
- python - 如何对多级索引数据帧的所有行执行复杂的 df 操作?
- flutter - Flutter BLoC:在父 Widget 更新时维护子状态
- powershell - 替换字符串中的管道字符
- react-native - 动作未调度本机反应,状态正常
- javascript - 没有'let','var','const'的变量的声明
- laravel - Laravel Eloquent 查询带参数
- django - 如何使用 django rest 框架为不同的用户类型创建自定义用户模型
- amazon-web-services - 带有 ubuntu 容器的 ECS
- r - 将同一目录中的多个 Rmd 文件渲染为单个 pdf(R,bookdown)
- python-3.x - python3 - 对 OSM API 的请求至少需要配置的连接超时持续时间