Are there any good Kotlin/Java loggers that doesn'...
# announcements
y
Are there any good Kotlin/Java loggers that doesn't rely on reflection?
r
println
🧌
🧌 4
👌 5
u
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
I'm using Kotlin-Logging, it works with slf4j: https://github.com/MicroUtils/kotlin-logging
👍 1
u
yeah, I didnt like that kotlin-logging made you put private val logger = KotlinLogging.logger {} on each class vs doing it once 😄
k
Yours creates a new instance for every invocation, I'm not sure if that's expensive though.
☝️ 3
m
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
There is also the cod that tries to detremine the file/function/line number that can be very 'reflecty'
m
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
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
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?