How do I get my Logger to print subclass name inst...
# announcements
k
How do I get my Logger to print subclass name instead of abstract class name? https://stackoverflow.com/q/60217253
d
I can’t confirm right now but I believe you should be able to use the
logger(name: String)
method using
this
class name Instead of
val logger = KotlinLogging.logger { }
Try
val logger = KotlinLogging.logger(“${this::class.java.name}”)
Let me know how it goes
a
It isn't problem of the logger, you weren't able to get the object that was instantiated right in the child class. You could have sent the object through constructor. BTW for more info and code snippet check the answer in stackoverflow.
If that helped you'd accept/upvote my answer if you like to
d
I also confirmed the code above and answered your SO question https://stackoverflow.com/a/60230434/12899678
a
@Dave K yeah that's neat, i like your way.
But if OP wanted to use logger elsewhere in the B file, then its not worth it :)
d
Updated the answer a bit with more info on why
That's correct, the logger logs method calls in A, B doesn't know about logging
In the current example OP wasn't using the logger in B, so that is definitely something to consider. Meanwhile in my example, there's no need to pass a logger up the chain from subclass through to the parent class. Imagining a grandchild class, C, B would need to have logic for having a nullable logger sent to it, check if it's null on instantiation, and then creates its own logger if needed before passing a logger up to A which could get messy