https://kotlinlang.org logo
#android
Title
# android
t

Tim

04/01/2020, 1:25 PM
Hello, sometime recently i believe i came across a library which had a logging feature where instead of using
Log.d(TAG, "something")
, you could just do
d { "something" }
and the logger would automatically figure out what class and method its in. Does anyone either happen to know this library or know how i could implement that myself?
c

Cody Engel

04/01/2020, 1:27 PM
Not sure on the library, but you could implement yourself with something like...
Copy code
fun d(logBlock: () -> Unit) { logBlock() }
🤔 actually probably more like this
Copy code
fun d(logBlock: () -> String) { 
    val logStatement = logBlock()
    Log.d(..., logStatement)
}
t

Tim

04/01/2020, 1:30 PM
thats awesome! thank you, that is definitely better than what i have. ill try to find a way to reliably get at least the class name
do i call
toString()
on the
logStatement
?
j

Javier

04/01/2020, 1:31 PM
You can take a look to my library to get some ideas: https://github.com/JavierSegoviaCordoba/KotlinLogger
it is working fine in intellij, I have to create the android version, but at least it does what you are searching
image.png
t

Tim

04/01/2020, 1:32 PM
@Javier awesome thank you :))
c

Cody Engel

04/01/2020, 1:40 PM
@Tim -- since the output of the function being passed in is a string you just need to invoke it. @Javier -- this looks nice, I've been looking for a logger for a ktor project I've been working on and I think I may have just found it 🙂
j

Javier

04/01/2020, 1:51 PM
@Cody Engel I have to add support to native, now only JVM, I don't know if you need native too
c

Cody Engel

04/01/2020, 1:53 PM
I'm using ktor for a backend project so native doesn't matter, i spend a lot of time looking at logs though so having them look nicer would be a big win
👍 1
d

Dendi

04/02/2020, 3:17 AM
Please remember to add “inline” modifier . As {} will be translated into Function0 ,which will delay your app.