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

后端 未结 17 2145
暗喜
暗喜 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:22

    This extension allows us to use TAG in any class

    val Any.TAG: String
        get() {
            val tag = javaClass.simpleName
            return if (tag.length <= 23) tag else tag.substring(0, 23)
        }
    
    //usage
    Log.e(TAG,"some value")
    

    It it also validated to work as an Android valid Log tag.

提交回复
热议问题