Hi, simple but nonetheless important question! I'm...
# android
p
Hi, simple but nonetheless important question! I'm migrating some Java -> Kotlin with a lot of logging going on; How do you guys declare the
TAG
string? Currently I'm just declaring it like:
private val tag = AutoDispose::class.java.simpleName!!
s
If you want to use class name as a tag, you can use extension function. Something like this one: fun Any.debug(message: String) { Log.d(this.javaClass.simpleName, message) }
k
That does reflection every time you log though. I use an interface I made myself that you implement on a companion object via delegation to a reified function that gets the class tag, and gives you methods like
info
,
warn
, etc. Anko Logging also does something similar https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-Logging#trait-like-style
p
@kevinmost Thanks, I didn't think of the class name being accessed via reflection regarding Segeis method but that makes total sense! I found some inspiration here https://github.com/Kotlin/anko/blob/master/anko/library/static/commons/src/Logging.kt (the impl of AnkoLogger, there's a lot of goodies in there regarding building libs)
k
yup, that's basically how my single-interface logger is built too actually
it works very well
­čÖî 1