首页 > 解决方案 > 如何在 Koin 中使用记录器?

问题描述

Koin 版本:2.1.5

此处的文档中缺少说明。

这就是我所拥有的:

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()

        startKoin {
            // use AndroidLogger as Koin Logger - default Level.INFO
            androidLogger()

            // use the Android context given there
            androidContext(this@MyApp)

            // module list
            modules(emptyList())
        }
    }
}
class MainActivity : AppCompatActivity() {
    private val logger : Logger by inject()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        logger.debug("onCreate")
    }
}

并且看起来依赖图中没有 Logger 的实例:

04-26 12:02:03.781 4672-4672/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 4672
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'org.koin.core.logger.Logger'. Check your definitions!
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'org.koin.core.logger.Logger'. Check your definitions!
        at org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:247)
        at org.koin.core.scope.Scope.resolveInstance(Scope.kt:216)
        at org.koin.core.scope.Scope.get(Scope.kt:181)
        at com.example.myapplication.MainActivity$$special$$inlined$inject$1.invoke(ComponentCallbackExt.kt:51)
        at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
        at com.example.myapplication.MainActivity.getLogger(MainActivity.kt)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:17)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

为什么?

标签: androidkoin

解决方案


我的理解:这个记录器用于内部 Koin 记录。我通过查看源代码得出了结论。这个记录器可以在 Koin 本身之外使用,getKoin()._logger.info("hello world")但我怀疑这是库创建者的意图。


推荐阅读