What is the best way to define log TAG constant in Kotlin?
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.
In general constants are all caps (ex. FOO) and located in the companion object:
class MyClass {
companion object {
public const val FOO = 1
}
}
and to define the TAG field you can use:
private val TAG = MyClass::class.qualifiedName