Skip to content

Commit

Permalink
Add LoggerFactory and set to ContextView where not need Context
Browse files Browse the repository at this point in the history
  • Loading branch information
Numichi committed May 22, 2022
1 parent 5217be9 commit cc531e3
Show file tree
Hide file tree
Showing 14 changed files with 1,679 additions and 1,373 deletions.
20 changes: 10 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = project.property("group") as String
version = project.property("version") as String as String
version = project.property("version") as String

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -20,20 +20,20 @@ java {

dependencies {
implementation(kotlin("stdlib"))
implementation("io.projectreactor:reactor-core:3.4.14")
implementation("io.projectreactor:reactor-core:3.4.18")
implementation("org.slf4j:slf4j-api:1.7.36")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions:1.1.5")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.6.0")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions:1.1.6")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.6.1")
implementation("com.google.code.findbugs:jsr305:3.0.2")
implementation("io.github.microutils:kotlin-logging-jvm:2.1.20")
implementation("io.github.microutils:kotlin-logging-jvm:2.1.21")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testImplementation("io.projectreactor:reactor-test:3.4.14")
testImplementation("org.apache.logging.log4j:log4j-slf4j-impl:2.17.1")
testImplementation("org.mockito:mockito-core:4.2.0")
testImplementation("io.projectreactor:reactor-test:3.4.18")
testImplementation("org.apache.logging.log4j:log4j-slf4j-impl:2.17.2")
testImplementation("org.mockito:mockito-core:4.5.1")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0")
testImplementation("io.mockk:mockk:1.12.2")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1")
testImplementation("io.mockk:mockk:1.12.4")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=2.0.0-RC0
version=2.0.0-RC1
group=io.github.numichi
developerId=numichi
developerName=Donát Csongor
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/github/numichi/reactive/logger/LoggerFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.numichi.reactive.logger

import mu.KotlinLogging
import org.slf4j.Logger

object LoggerFactory {
fun getLogger(string: String) = org.slf4j.LoggerFactory.getLogger(string)
fun getLogger(clazz: Class<*>) = org.slf4j.LoggerFactory.getLogger(clazz)

fun getKLogger(string: String) = KotlinLogging.logger(string)
fun getKLogger(logger: Logger) = KotlinLogging.logger(logger)
fun getKLogger(clazz: Class<*>) = KotlinLogging.logger(org.slf4j.LoggerFactory.getLogger(clazz))
fun getKLogger(func: () -> Unit) = KotlinLogging.logger(func)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ interface IReactorCore : ICore {
}
}

fun wrap(runnable: Runnable): Mono<Context> {
fun wrap(runnable: Runnable): Mono<ContextView> {
return Mono.deferContextual { contextView: ContextView ->
val context = Context.of(contextView)

try {
takeMDCSnapshot(context).use { runnable.run() }
takeMDCSnapshot(contextView).use { runnable.run() }
} catch (exception: ContextNotExistException) {
return@deferContextual Mono.error<Context>(exception)
}

Mono.just(context)
Mono.just(contextView)
}.subscribeOn(scheduler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@ package io.github.numichi.reactive.logger.reactor
import mu.KLogger
import mu.Marker
import reactor.core.publisher.Mono
import reactor.util.context.Context
import reactor.util.context.ContextView

interface IReactorKLogger : IReactorLogger {
override val logger: KLogger

fun trace(msg: () -> Any?): Mono<Context> = wrap { logger.trace(msg) }
fun trace(t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.trace(t, msg) }
fun trace(marker: Marker?, msg: () -> Any?): Mono<Context> = wrap { logger.trace(marker, msg) }
fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.trace(marker, t, msg) }

fun debug(msg: () -> Any?): Mono<Context> = wrap { logger.debug(msg) }
fun debug(t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.debug(t, msg) }
fun debug(marker: Marker?, msg: () -> Any?): Mono<Context> = wrap { logger.debug(marker, msg) }
fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.debug(marker, t, msg) }

fun info(msg: () -> Any?): Mono<Context> = wrap { logger.info(msg) }
fun info(t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.info(t, msg) }
fun info(marker: Marker?, msg: () -> Any?): Mono<Context> = wrap { logger.info(marker, msg) }
fun info(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.info(marker, t, msg) }

fun warn(msg: () -> Any?): Mono<Context> = wrap { logger.warn(msg) }
fun warn(t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.warn(t, msg) }
fun warn(marker: Marker?, msg: () -> Any?): Mono<Context> = wrap { logger.warn(marker, msg) }
fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.warn(marker, t, msg) }

fun error(msg: () -> Any?): Mono<Context> = wrap { logger.error(msg) }
fun error(t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.error(t, msg) }
fun error(marker: Marker?, msg: () -> Any?): Mono<Context> = wrap { logger.error(marker, msg) }
fun error(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<Context> = wrap { logger.error(marker, t, msg) }

fun entry(vararg argArray: Any?): Mono<Context> = wrap { logger.entry(*argArray) }
fun exit(): Mono<Context> = wrap { logger.exit() }
fun <T> exit(result: T): Mono<Pair<Context?, T>> = wrap { logger.exit(result) }.map { Pair(it, result) }
fun trace(msg: () -> Any?): Mono<ContextView> = wrap { logger.trace(msg) }
fun trace(t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.trace(t, msg) }
fun trace(marker: Marker?, msg: () -> Any?): Mono<ContextView> = wrap { logger.trace(marker, msg) }
fun trace(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.trace(marker, t, msg) }

fun debug(msg: () -> Any?): Mono<ContextView> = wrap { logger.debug(msg) }
fun debug(t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.debug(t, msg) }
fun debug(marker: Marker?, msg: () -> Any?): Mono<ContextView> = wrap { logger.debug(marker, msg) }
fun debug(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.debug(marker, t, msg) }

fun info(msg: () -> Any?): Mono<ContextView> = wrap { logger.info(msg) }
fun info(t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.info(t, msg) }
fun info(marker: Marker?, msg: () -> Any?): Mono<ContextView> = wrap { logger.info(marker, msg) }
fun info(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.info(marker, t, msg) }

fun warn(msg: () -> Any?): Mono<ContextView> = wrap { logger.warn(msg) }
fun warn(t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.warn(t, msg) }
fun warn(marker: Marker?, msg: () -> Any?): Mono<ContextView> = wrap { logger.warn(marker, msg) }
fun warn(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.warn(marker, t, msg) }

fun error(msg: () -> Any?): Mono<ContextView> = wrap { logger.error(msg) }
fun error(t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.error(t, msg) }
fun error(marker: Marker?, msg: () -> Any?): Mono<ContextView> = wrap { logger.error(marker, msg) }
fun error(marker: Marker?, t: Throwable?, msg: () -> Any?): Mono<ContextView> = wrap { logger.error(marker, t, msg) }

fun entry(vararg argArray: Any?): Mono<ContextView> = wrap { logger.entry(*argArray) }
fun exit(): Mono<ContextView> = wrap { logger.exit() }
fun <T> exit(result: T): Mono<Pair<ContextView?, T>> = wrap { logger.exit(result) }.map { Pair(it, result) }
fun <T : Throwable> throwing(throwable: T): Mono<T> = wrap { logger.throwing(throwable) }.map { throwable }
fun <T : Throwable> catching(throwable: T): Mono<T> = wrap { logger.catching(throwable) }.map { throwable }
}
Loading

0 comments on commit cc531e3

Please sign in to comment.