Kotlin Android print to console

前端 未结 6 943
难免孤独
难免孤独 2021-02-03 17:54

I need to print some str to console (Android Studio) using Kotlin. I\'ve tried the:

Log.v() 
Log.d() 
Log.i() 
Log.w() 
Log.e() 

methods. But i

6条回答
  •  不思量自难忘°
    2021-02-03 18:08

    I've written some extension functions that make use of reified type parameters in order to avoid dealing with declaring log tags in all project's classes. The basic idea is shown by the following snippet:

    inline fun  T.logi(message: String) = Log.i(T::class.java.simpleName, message)
    

    Basically, you can log something to the logcat with the following invocation (W/O external dependencies):

    logi("My log message")
    

    You can find a gist here. The functions declared in the gist are a little more elaborated given that they allow:

    • Lazy evaluation of the string to be logged out (if for example the string needs to be generated in some way)
    • Logging only when in debug mode by default
    • Use a given class name when you need to log from within an anonymous class that has no name

提交回复
热议问题