https://kotlinlang.org logo
#announcements
Title
# announcements
y

yen

09/02/2019, 12:46 PM
Are there any good Kotlin/Java loggers that doesn't rely on reflection?
r

ribesg

09/02/2019, 12:49 PM
println
🧌
🧌 4
👌 5
u

Uriel Salischiker

09/02/2019, 12:59 PM
I use the following
Copy code
inline val <reified T> T.log: Logger
    get() = LoggerFactory.getLogger(T::class.java)
it means you can just use log.info("whatever") by just importing the function, no need to create a variable in each class or use delegates
Made it myself by combining a few things so not sure if its a good way or not 😅
r

rodolpho.couto

09/02/2019, 1:02 PM
I'm using Kotlin-Logging, it works with slf4j: https://github.com/MicroUtils/kotlin-logging
👍 1
u

Uriel Salischiker

09/02/2019, 1:03 PM
yeah, I didnt like that kotlin-logging made you put private val logger = KotlinLogging.logger {} on each class vs doing it once 😄
k

karelpeeters

09/02/2019, 4:34 PM
Yours creates a new instance for every invocation, I'm not sure if that's expensive though.
☝️ 3
m

Mike

09/03/2019, 12:20 PM
I think the only time 'reflection' is used by the logger is to determine what to name the logger. And really, this is done as a static, so it's done once when the class is created, and really is only used to give the logger a unique name. You could use the
getLogger(String)
interface just as easily, and avoid any 'reflection'. Have you found something else that concerns you? And there's no reason you HAVE to create a logger in every class. As I say, it creates a logger with that name, and shows up better in the logs, but isn't mandatory. You could use a global logger for your application.
d

DALDEI

09/08/2019, 11:35 PM
There is also the cod that tries to detremine the file/function/line number that can be very 'reflecty'
m

Mike

09/09/2019, 1:43 AM
I don't think the logger outputs anything other than the logger name. If you're seeing filenames and line numbers, isn't that a stack trace from an exception? If so, then that's independent of the logger. It's the system and why exceptions can be expensive to create.
d

DALDEI

09/09/2019, 5:48 AM
Some loggers, such as log4j or the kotlin log4j and micrologger (probably more) optionally attempt to determine the source line , function, module of each log statement,
m

Mike

09/09/2019, 12:19 PM
If it's optional, then it shouldn't be a problem as one can opt-out. I guess I'm unsure what the OP (@yen) is truly trying to avoid reflection of/for. Any guidance given the comments above?