Skip to content

Commit

Permalink
fix: add new setter methods with "set" prefix for java builder patter…
Browse files Browse the repository at this point in the history
…n compatibility
  • Loading branch information
Numichi committed Aug 5, 2022
1 parent 9f846ad commit d56b757
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 109 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {

testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")
testImplementation("io.projectreactor:reactor-test:3.4.21")
testImplementation("org.apache.logging.log4j:log4j-slf4j-impl:2.17.2")
testImplementation("org.apache.logging.log4j:log4j-slf4j-impl:2.18.0")
testImplementation("org.mockito:mockito-core:4.6.1")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=2.1.1
version=2.1.3
group=io.github.numichi
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,48 @@ abstract class ACoroutine<T : IReactorLogger>(
var mdcContextKey: String,
var enableError: Boolean,
) {

fun setLogger(logger: L): Builder<E, L, R> {
this.logger = logger
return this
}

fun setContextKey(contextKey: CCKey<E>): Builder<E, L, R> {
this.contextKey = contextKey
return this
}

fun setContextExtractive(contextExtractive: CCResolveFn<E>): Builder<E, L, R> {
this.contextExtractive = contextExtractive
return this
}

fun setScheduler(scheduler: Scheduler): Builder<E, L, R> {
this.scheduler = scheduler
return this
}

fun setMDCContextKey(mdcContextKey: String): Builder<E, L, R> {
check(mdcContextKey.trim().isNotEmpty()) { "mdcContextKey must not be blank or empty" }
this.mdcContextKey = mdcContextKey
return this
}

fun setError(enableError: Boolean): Builder<E, L, R> {
this.enableError = enableError
return this
}

/**
* Which scheduler does use for logging?
*
* @param scheduler Default: Schedulers.boundedElastic()
*/
@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setScheduler(scheduler)"),
level = DeprecationLevel.WARNING,
)
fun withScheduler(scheduler: Scheduler): Builder<E, L, R> {
this.scheduler = scheduler
return this
Expand All @@ -35,36 +72,62 @@ abstract class ACoroutine<T : IReactorLogger>(
*
* @param mdcContextKey Default: "DEFAULT_REACTOR_CONTEXT_MDC_KEY"
*/
@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setMDCContextKey(mdcContextKey)"),
level = DeprecationLevel.WARNING,
)
fun withMDCContextKey(mdcContextKey: String): Builder<E, L, R> {
check(mdcContextKey.trim().isNotEmpty()) { "mdcContextKey must not be blank or empty" }
this.mdcContextKey = mdcContextKey
return this
}

@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setContextKey(contextKey)"),
level = DeprecationLevel.WARNING,
)
fun withContextKey(contextKey: CoroutineContext.Key<E>): Builder<E, L, R> {
this.contextKey = contextKey
return this
}

@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setContextExtractive(contextExtractive)"),
level = DeprecationLevel.WARNING,
)
fun withContextExtractive(contextExtractive: suspend (CoroutineContext.Key<out E>) -> Context?): Builder<E, L, R> {
this.contextExtractive = contextExtractive
return this
}

@Deprecated(
message = "It can be misleading due to enabling word in method name and boolean parameter. Use: withError(boolean); False by default",
replaceWith = ReplaceWith("withError(enable)"),
level = DeprecationLevel.WARNING
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setError(enable)"),
level = DeprecationLevel.WARNING,
)
fun withEnableError(enable: Boolean): Builder<E, L, R> {
this.enableError = enable
return this
}

@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setError(enable)"),
level = DeprecationLevel.WARNING
)
fun withError(enable: Boolean = true): Builder<E, L, R> {
this.enableError = enable
return this
}

@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setLogger(logger)"),
level = DeprecationLevel.WARNING,
)
fun withLogger(logger: L): Builder<E, L, R> {
this.logger = logger
return this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.github.numichi.reactive.logger.exception;
package io.github.numichi.reactive.logger.exception

class ContextNotExistException(message: String, throwable: Throwable) : RuntimeException(message, throwable)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.github.numichi.reactive.logger.exception;
package io.github.numichi.reactive.logger.exception

class InvalidContextDataException(throwable: Throwable) : RuntimeException("Reached data is not Map<String, String>", throwable)
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,86 @@ abstract class AReactive(
var enableError: Boolean
) {

fun setLogger(logger: L): Builder<L, R> {
this.logger = logger
return this
}

fun setScheduler(scheduler: Scheduler): Builder<L, R> {
this.scheduler = scheduler
return this
}

fun setMDCContextKey(mdcContextKey: String): Builder<L, R> {
check(mdcContextKey.trim().isNotEmpty()) { "mdcContextKey must not be blank or empty" }
this.mdcContextKey = mdcContextKey
return this
}

fun setError(enableError: Boolean): Builder<L, R> {
this.enableError = enableError
return this
}

/**
* Which scheduler does use for logging?
*
* @param scheduler Default: Schedulers.boundedElastic()
*/
@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setScheduler(scheduler)"),
level = DeprecationLevel.WARNING,
)
fun withScheduler(scheduler: Scheduler): Builder<L, R> {
this.scheduler = scheduler
return this;
return this
}

/**
* Which under reactor context key is the MDC stored?
*
* @param mdcContextKey Default: "DEFAULT_REACTOR_CONTEXT_MDC_KEY"
*/
@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setMDCContextKey(mdcContextKey)"),
level = DeprecationLevel.WARNING,
)
fun withMDCContextKey(mdcContextKey: String): Builder<L, R> {
check(mdcContextKey.trim().isNotEmpty()) { "mdcContextKey must not be blank or empty" }
this.mdcContextKey = mdcContextKey
return this;
return this
}

@Deprecated(
message = "It can be misleading due to enabling word in method name and boolean parameter. Use: withError(boolean); False by default",
replaceWith = ReplaceWith("withError(enableError)"),
level = DeprecationLevel.WARNING
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setError(enable)"),
level = DeprecationLevel.WARNING,
)
fun withEnableError(enable: Boolean): Builder<L, R> {
this.enableError = enable
return this
}

@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setError(enable)"),
level = DeprecationLevel.WARNING,
)
fun withError(enable: Boolean = true): Builder<L, R> {
this.enableError = enable
return this
}

@Deprecated(
message = "Change name prefix from \"with\" to \"set\" for java builder pattern compatibility. It will be removed in version 2.3.0.",
replaceWith = ReplaceWith("setLogger(logger)"),
level = DeprecationLevel.WARNING,
)
fun withLogger(logger: L): Builder<L, R> {
this.logger = logger
return this;
return this
}

abstract fun build(): R
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import java.util.*
@ExperimentalCoroutinesApi
internal class CoroutineKLoggerTest {
private val imperativeLogger: KLogger = mockk(relaxed = true)
private val logger: ICoroutineKLogger = CoroutineKLogger.reactorBuilder().withLogger(imperativeLogger).build()
private val loggerScheduled: ICoroutineKLogger = CoroutineKLogger.reactorBuilder().withLogger(imperativeLogger).withScheduler(Schedulers.parallel()).build()
private val loggerWithError: ICoroutineKLogger = CoroutineKLogger.reactorBuilder().withLogger(imperativeLogger).withError().build()
private val logger: ICoroutineKLogger = CoroutineKLogger.reactorBuilder().setLogger(imperativeLogger).build()
private val loggerScheduled: ICoroutineKLogger = CoroutineKLogger.reactorBuilder().setLogger(imperativeLogger).setScheduler(Schedulers.parallel()).build()
private val loggerWithError: ICoroutineKLogger = CoroutineKLogger.reactorBuilder().setLogger(imperativeLogger).setError(true).build()

companion object {
@JvmStatic
Expand Down Expand Up @@ -101,9 +101,9 @@ internal class CoroutineKLoggerTest {
@Test
fun `should configure empty context if not exist context or null`(){
runTest {
val instance1 = CoroutineLogger.builder(ReactorContext) { coroutineContext[it]?.context }.withLogger(imperativeLogger).build()
val instance2 = CoroutineLogger.builder(ReactorContext) { null }.withLogger(imperativeLogger).build()
val instance4 = CoroutineLogger.builder(ReactorContext) { Context.empty() }.withLogger(imperativeLogger).build()
val instance1 = CoroutineLogger.builder(ReactorContext) { coroutineContext[it]?.context }.setLogger(imperativeLogger).build()
val instance2 = CoroutineLogger.builder(ReactorContext) { null }.setLogger(imperativeLogger).build()
val instance4 = CoroutineLogger.builder(ReactorContext) { Context.empty() }.setLogger(imperativeLogger).build()

instance1.info("")
instance2.info("")
Expand Down Expand Up @@ -150,7 +150,7 @@ internal class CoroutineKLoggerTest {
@Test
fun contextKey() {
val contextKey = "another-context-key"
val loggerWithCustomScheduler = CoroutineLogger.reactorBuilder().withMDCContextKey(contextKey).build()
val loggerWithCustomScheduler = CoroutineLogger.reactorBuilder().setMDCContextKey(contextKey).build()
assertSame(loggerWithCustomScheduler.mdcContextKey, contextKey)
}

Expand All @@ -163,12 +163,14 @@ internal class CoroutineKLoggerTest {
}

@Test
suspend fun traceEnabledMarker() {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isTraceEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isTraceEnabled(marker), "trace not enabled when it should be")
assertFalse(logger.isTraceEnabled(marker), "trace enabled when it should not be")
assertTrue(logger.isTraceEnabled(marker), "trace not enabled when it should be")
fun traceEnabledMarker() {
runTest {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isTraceEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isTraceEnabled(marker), "trace not enabled when it should be")
assertFalse(logger.isTraceEnabled(marker), "trace enabled when it should not be")
assertTrue(logger.isTraceEnabled(marker), "trace not enabled when it should be")
}
}

@Test
Expand All @@ -180,12 +182,14 @@ internal class CoroutineKLoggerTest {
}

@Test
suspend fun debugEnabledMarker() {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isDebugEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isDebugEnabled(marker), "debug not enabled when it should be")
assertFalse(logger.isDebugEnabled(marker), "debug enabled when it should not be")
assertTrue(logger.isDebugEnabled(marker), "debug not enabled when it should be")
fun debugEnabledMarker() {
runTest {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isDebugEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isDebugEnabled(marker), "debug not enabled when it should be")
assertFalse(logger.isDebugEnabled(marker), "debug enabled when it should not be")
assertTrue(logger.isDebugEnabled(marker), "debug not enabled when it should be")
}
}

@Test
Expand All @@ -197,12 +201,14 @@ internal class CoroutineKLoggerTest {
}

@Test
suspend fun infoEnabledMarker() {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isInfoEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isInfoEnabled(marker), "info not enabled when it should be")
assertFalse(logger.isInfoEnabled(marker), "info enabled when it should not be")
assertTrue(logger.isInfoEnabled(marker), "info not enabled when it should be")
fun infoEnabledMarker() {
runTest {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isInfoEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isInfoEnabled(marker), "info not enabled when it should be")
assertFalse(logger.isInfoEnabled(marker), "info enabled when it should not be")
assertTrue(logger.isInfoEnabled(marker), "info not enabled when it should be")
}
}

@Test
Expand All @@ -214,12 +220,14 @@ internal class CoroutineKLoggerTest {
}

@Test
suspend fun warnEnabledMarker() {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isWarnEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isWarnEnabled(marker), "warn not enabled when it should be")
assertFalse(logger.isWarnEnabled(marker), "warn enabled when it should not be")
assertTrue(logger.isWarnEnabled(marker), "warn not enabled when it should be")
fun warnEnabledMarker() {
runTest {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isWarnEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isWarnEnabled(marker), "warn not enabled when it should be")
assertFalse(logger.isWarnEnabled(marker), "warn enabled when it should not be")
assertTrue(logger.isWarnEnabled(marker), "warn not enabled when it should be")
}
}

@Test
Expand All @@ -231,12 +239,14 @@ internal class CoroutineKLoggerTest {
}

@Test
suspend fun errorEnabledMarker() {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isErrorEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isErrorEnabled(marker), "error not enabled when it should be")
assertFalse(logger.isErrorEnabled(marker), "error enabled when it should not be")
assertTrue(logger.isErrorEnabled(marker), "error not enabled when it should be")
fun errorEnabledMarker() {
runTest {
val marker = MarkerFactory.getMarker(randomText())
every { imperativeLogger.isErrorEnabled(marker) } returnsMany listOf(true, false, true)
assertTrue(logger.isErrorEnabled(marker), "error not enabled when it should be")
assertFalse(logger.isErrorEnabled(marker), "error enabled when it should not be")
assertTrue(logger.isErrorEnabled(marker), "error not enabled when it should be")
}
}

//region Trace
Expand Down
Loading

0 comments on commit d56b757

Please sign in to comment.