nkiesel
04/05/2021, 5:11 PMval logger: Logger = LoggerFactory.getLogger(Foo::class.java)
in the companion object of a class Foo
. I remember a discussion about defining this as a class property instead of as a companion object property (which would then allow to use val logger: Logger = LoggerFactory.getLogger(this::class.java)
) but I can't find it anymore. What is current best practice here ?nanodeath
04/05/2021, 5:13 PM@Singleton
. if it's not, I'd probably avoid defining the logger as an instance field. myself I usually define it at either the companion object or file levelashmelev
04/05/2021, 5:14 PMinterface HasLogger {
val logger: Logger
get() = LoggerFactory.getLogger(javaClass)
}
And then every class that need a logger simply implements the interface:
class MyClass : HasLogger {
logger.debug("some stuff here")
}
nanodeath
04/05/2021, 5:15 PMBig Chungus
04/05/2021, 5:16 PMashmelev
04/05/2021, 5:17 PMBig Chungus
04/05/2021, 5:17 PMnanodeath
04/05/2021, 5:21 PMBig Chungus
04/05/2021, 5:22 PMNir
04/05/2021, 5:22 PMBig Chungus
04/05/2021, 5:23 PMNir
04/05/2021, 5:23 PMlogger
at file levelnkiesel
04/05/2021, 5:25 PMNir
04/05/2021, 5:26 PMNir
04/05/2021, 5:26 PMNir
04/05/2021, 5:26 PMnkiesel
04/05/2021, 5:26 PMlogger
property in every class instance instead of in the single companion object class, and how much is the extra cost of initializing it whenever an object of that class is constructed.Nir
04/05/2021, 5:27 PMnkiesel
04/05/2021, 5:27 PMnkiesel
04/05/2021, 5:29 PMBig Chungus
04/05/2021, 5:30 PMnkiesel
04/05/2021, 5:31 PMval logger: Logger = LoggerFactory(javaClass)
and while that looks nice, it will use Foo$Companion
instead of Foo
as logger name. So I either have to revert this or move it out of the companion objectBig Chungus
04/05/2021, 5:32 PMnkiesel
04/05/2021, 5:32 PMnkiesel
04/05/2021, 5:36 PM@JvmStatic
does not help hereJordan Foo
04/05/2021, 5:36 PMobject MyLoggerFactory {
fun getLogger(name: String?): Logger = LoggerFactory.getLogger(name)
fun getLogger(clazz: Class<*>?): Logger = LoggerFactory.getLogger(clazz)
fun getILoggerFactory(): ILoggerFactory = LoggerFactory.getILoggerFactory()
}
Jordan Foo
04/05/2021, 5:36 PMJordan Foo
04/05/2021, 5:37 PMnkiesel
04/05/2021, 5:38 PMJordan Foo
04/05/2021, 5:38 PMJordan Foo
04/05/2021, 5:39 PMfun getLogger(clazz: Class<*>?): Logger = getLogger(clazz?.simpleName?.removeSuffix("\$Companion"))
Jordan Foo
04/05/2021, 5:39 PMnkiesel
04/05/2021, 5:40 PMnkiesel
04/05/2021, 5:40 PMJordan Foo
04/05/2021, 5:40 PMnkiesel
04/05/2021, 5:42 PMLoggerFactory
importJordan Foo
04/05/2021, 5:42 PMJordan Foo
04/05/2021, 5:43 PMJordan Foo
04/05/2021, 5:43 PMnkiesel
04/05/2021, 5:45 PMJordan Foo
04/05/2021, 5:46 PMnkiesel
04/05/2021, 5:49 PMNir
04/05/2021, 6:10 PMNir
04/05/2021, 6:10 PMNir
04/05/2021, 6:11 PMnkiesel
04/05/2021, 6:14 PMNir
04/05/2021, 6:15 PMnkiesel
04/05/2021, 6:15 PMNir
04/05/2021, 6:15 PMnkiesel
04/05/2021, 6:16 PMNir
04/05/2021, 6:17 PMNir
04/05/2021, 6:17 PMimport mu.KotlinLogging
private val logger = KotlinLogging.logger {}
class FooWithLogging {
val message = "world"
fun bar() {
logger.debug { "hello $message" }
}
}
Nir
04/05/2021, 6:17 PMKotlinLogging.logger
, and it figures out what package the lambda belongs toNir
04/05/2021, 6:18 PMNir
04/05/2021, 6:19 PMlogger = logging.getLogger(___name___)
Nir
04/05/2021, 6:20 PMlogging
for a long while and it's always served me very well, I can't ever remember feeling like I wanted per class customization.Nir
04/05/2021, 6:20 PMnkiesel
04/05/2021, 6:31 PM$
in the resulting .javaClass.name
). One downside is that the logger name for a class C
inside a file C.kt
will be "CKt" and not "C". But interesting idea neverthelessNir
04/05/2021, 6:34 PMnkiesel
04/05/2021, 6:44 PMNir
04/05/2021, 7:04 PMNir
04/05/2021, 7:04 PM