BAEL-1838 (#4692)
* #BAEL-1838 code samples. Renamed LambdaKotlinTest to have the build succeed. * #BAEL-1838 code samples w/inheritance. * #BAEL-1838 renamed logger helper function to getLogger to avoid confusion. * #BAEL-1838 renamed logger helper function to getLogger to avoid confusion.
This commit is contained in:
parent
232f6cbc50
commit
416a88eae5
|
@ -60,7 +60,6 @@
|
|||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<version>${kotlin-reflect.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
|
@ -224,10 +223,11 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<kotlin-maven-plugin.version>1.2.41</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.2.41</kotlin-test-junit.version>
|
||||
<kotlin-stdlib.version>1.2.41</kotlin-stdlib.version>
|
||||
<kotlin-reflect.version>1.2.41</kotlin-reflect.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<kotlin-maven-plugin.version>1.2.51</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.2.51</kotlin-test-junit.version>
|
||||
<kotlin-stdlib.version>1.2.51</kotlin-stdlib.version>
|
||||
<kotlin-reflect.version>1.2.51</kotlin-reflect.version>
|
||||
<kotlinx.version>0.22.5</kotlinx.version>
|
||||
<ktor.io.version>0.9.2</ktor.io.version>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
|
||||
open class LoggerAsExtensionOnAny {
|
||||
val logger = logger()
|
||||
|
||||
fun log(s: String) {
|
||||
logger().info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionSubclass : LoggerAsExtensionOnAny()
|
||||
|
||||
fun <T : Any> T.logger(): Logger = getLogger(getClassForLogging(javaClass))
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsExtensionOnAny().log("test")
|
||||
ExtensionSubclass().log("sub")
|
||||
"foo".logger().info("foo")
|
||||
1.logger().info("uh-oh!")
|
||||
SomeOtherClass().logger()
|
||||
}
|
||||
|
||||
class SomeOtherClass {
|
||||
fun logger(): String {
|
||||
return "foo"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
interface Logging
|
||||
|
||||
inline fun <reified T : Logging> T.logger(): Logger =
|
||||
//Wrong logger name!
|
||||
//LoggerFactory.getLogger(javaClass.name + " w/interface")
|
||||
LoggerFactory.getLogger(getClassForLogging(T::class.java).name + " w/interface")
|
||||
|
||||
open class LoggerAsExtensionOnMarkerInterface : Logging {
|
||||
companion object : Logging {
|
||||
val logger = logger()
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
logger().info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
class MarkerExtensionSubclass : LoggerAsExtensionOnMarkerInterface()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsExtensionOnMarkerInterface().log("test")
|
||||
MarkerExtensionSubclass().log("sub")
|
||||
"foo".logger().info("foo")
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.kotlin.logging
|
||||
|
||||
open class LoggerAsProperty {
|
||||
private val logger = getLogger(javaClass)
|
||||
|
||||
fun log(s: String) {
|
||||
logger.info(s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class PropertySubclass : LoggerAsProperty()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsProperty().log("test")
|
||||
PropertySubclass().log("sub")
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
open class LoggerAsPropertyDelegate {
|
||||
private val lazyLogger by lazyLogger()
|
||||
protected val logger by LoggerDelegate()
|
||||
private val logger2 = logger
|
||||
|
||||
companion object {
|
||||
private val lazyLoggerComp by lazyLogger()
|
||||
private val loggerComp by LoggerDelegate()
|
||||
}
|
||||
|
||||
open fun log(s: String) {
|
||||
logger.info(s)
|
||||
logger2.info(s)
|
||||
lazyLogger.info(s)
|
||||
loggerComp.info(s)
|
||||
lazyLoggerComp.info(s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DelegateSubclass : LoggerAsPropertyDelegate() {
|
||||
override fun log(s: String) {
|
||||
logger.info("-- in sub")
|
||||
super.log(s)
|
||||
}
|
||||
}
|
||||
|
||||
fun lazyLogger(forClass: Class<*>): Lazy<Logger> =
|
||||
lazy { getLogger(getClassForLogging(forClass)) }
|
||||
|
||||
fun <T : Any> T.lazyLogger(): Lazy<Logger> = lazyLogger(javaClass)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsPropertyDelegate().log("test")
|
||||
DelegateSubclass().log("sub")
|
||||
}
|
||||
|
||||
class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>) =
|
||||
getLogger(getClassForLogging(thisRef.javaClass))
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.kotlin.logging
|
||||
|
||||
open class LoggerInCompanionObject {
|
||||
companion object {
|
||||
private val loggerWithExplicitClass = getLogger(LoggerInCompanionObject::class.java)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
private val loggerWithWrongClass = getLogger(javaClass)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
private val logger = getLogger(javaClass.enclosingClass)
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
loggerWithExplicitClass.info(s)
|
||||
loggerWithWrongClass.info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
|
||||
class Inner {
|
||||
companion object {
|
||||
private val loggerWithExplicitClass = getLogger(Inner::class.java)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
@JvmStatic
|
||||
private val loggerWithWrongClass = getLogger(javaClass)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
@JvmStatic
|
||||
private val logger = getLogger(javaClass.enclosingClass)
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
loggerWithExplicitClass.info(s)
|
||||
loggerWithWrongClass.info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class CompanionSubclass : LoggerInCompanionObject()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerInCompanionObject().log("test")
|
||||
LoggerInCompanionObject.Inner().log("test")
|
||||
CompanionSubclass().log("sub")
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import kotlin.reflect.full.companionObject
|
||||
|
||||
fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass)
|
||||
|
||||
fun <T : Any> getClassForLogging(javaClass: Class<T>): Class<*> {
|
||||
return javaClass.enclosingClass?.takeIf {
|
||||
it.kotlin.companionObject?.java == javaClass
|
||||
} ?: javaClass
|
||||
}
|
Loading…
Reference in New Issue