What is the best way to define log TAG constant in Kotlin?

后端 未结 17 2118
暗喜
暗喜 2021-01-30 16:00

I\'m creating my first Kotlin classes in my Android application. Usually for logging purposes I have a constant with name TAG. What I would do in Java is:



        
相关标签:
17条回答
  • 2021-01-30 16:33

    I created some Log extension functions to avoid declaring the log tag as we did in Java (maybe less performant, but given that we are talking about logging this should be acceptable IMO). The approach uses reified type parameters and other Kotlin goodies to retrieve the class simple name. Here is a basic example:

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

    You can find a more elaborated gist here

    0 讨论(0)
  • 2021-01-30 16:33

    I'm creating the constant as a companion object:

    companion object {
        val TAG = "SOME_TAG_VALUE"
    }
    

    Then, I can use it like this:

    MyClass.TAG
    
    0 讨论(0)
  • 2021-01-30 16:33

    AnkoLogger uses an interface to define the log tag.

    interface AnkoLogger {
                /**
                 * The logger tag used in extension functions for the [AnkoLogger].
                 * Note that the tag length should not be more than 23 symbols.
                 */
                val loggerTag: String
                    get() = getTag(javaClass)
            }
    private fun getTag(clazz: Class<*>): String {
            val tag = clazz.simpleName
            return if (tag.length <= 23) {
                tag
            } else {
                tag.substring(0, 23)
            }
        }
    inline fun AnkoLogger.info(message: () -> Any?) {
        val tag = loggerTag
        if (Log.isLoggable(tag, Log.INFO)) {
            Log.i(tag, message()?.toString() ?: "null")
        }
    }
    

    You can use it like this:

    class MyClass : AnkoLogger {
        fun someFun(){
           info("logging info")
        }
    }
    

    Maybe AnkoLogger can give you some ideas to implement a custom logging tool.

    0 讨论(0)
  • 2021-01-30 16:34

    Commonly suggested approach of using the companion object generates extra static final instance of a companion class and thus is bad performance and memory-wise.

    The best way (IMHO)

    Define a log tag as a top-level constant, thus only extra class is generated (MyClassKt), but compared to companion object there will be no static final instance of it (and no instance whatsoever):

    private const val TAG = "MyLogTag"
    
    class MyClass {
    
        fun logMe() {
            Log.w(TAG, "Message")
        }
    }
    

    Another option

    Use a normal val. Though this looks unusual to see a log tag not as an all-uppercase constant, this will not generate any classes and has least overhead.

    class MyClass {
    
        private val tag = "myLogTag"
    
        fun logMe() {
            Log.w(tag, "Message")
        }
    }
    
    0 讨论(0)
  • 2021-01-30 16:35

    Simply doing the following worked for me.

    private val TAG = this::class.java.simpleName
    
    0 讨论(0)
提交回复
热议问题