Skip to content

Commit

Permalink
slf4j bridge - init lock (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon authored Nov 22, 2023
1 parent 1803b08 commit 378e144
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package zio.logging.slf4j.bridge

import org.slf4j.impl.ZioLoggerFactory
import zio.{ Runtime, ZIO, ZLayer }
import zio.{ Runtime, Semaphore, Unsafe, ZIO, ZLayer }

object Slf4jBridge {

Expand Down Expand Up @@ -46,12 +46,15 @@ object Slf4jBridge {
def initialize(nameAnnotationKey: String): ZLayer[Any, Nothing, Unit] =
Runtime.enableCurrentFiber ++ layer(nameAnnotationKey)

private val initLock = Semaphore.unsafe.make(1)(Unsafe.unsafe)

private def layer(nameAnnotationKey: String): ZLayer[Any, Nothing, Unit] =
ZLayer {
ZIO.runtime[Any].flatMap { runtime =>
ZIO.succeed {
ZioLoggerFactory.initialize(new ZioLoggerRuntime(runtime, nameAnnotationKey))
}
}
for {
runtime <- ZIO.runtime[Any]
_ <- initLock.withPermit {
ZIO.succeed(ZioLoggerFactory.initialize(new ZioLoggerRuntime(runtime, nameAnnotationKey)))
}
} yield ()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ object Slf4jBridgeSpec extends ZIOSpecDefault {

override def spec =
suite("Slf4jBridge")(
test("parallel init") {
for {
_ <-
ZIO.foreachPar((1 to 5).toList) { _ =>
ZIO
.succeed(org.slf4j.LoggerFactory.getLogger("SLF4J-LOGGER").warn("Test {}!", "WARNING"))
.provide(Slf4jBridge.initialize)
}
} yield assertCompletes
},
test("logs through slf4j - legacy logger name annotation key") {
val testFailure = new RuntimeException("test error")
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package zio.logging.slf4j.bridge

import zio.{ Runtime, ZIO, ZLayer }
import zio.{ Runtime, Semaphore, Unsafe, ZIO, ZLayer }

object Slf4jBridge {

Expand All @@ -29,15 +29,20 @@ object Slf4jBridge {
*/
def initializeWithoutFiberRefPropagation: ZLayer[Any, Nothing, Unit] = layer

private val initLock = Semaphore.unsafe.make(1)(Unsafe.unsafe)

private def layer: ZLayer[Any, Nothing, Unit] =
ZLayer {
ZIO.runtime[Any].flatMap { runtime =>
ZIO.succeed {
org.slf4j.LoggerFactory
.getILoggerFactory()
.asInstanceOf[LoggerFactory]
.attacheRuntime(new ZioLoggerRuntime(runtime))
}
}
for {
runtime <- ZIO.runtime[Any]
_ <- initLock.withPermit {
ZIO.succeed(
org.slf4j.LoggerFactory
.getILoggerFactory()
.asInstanceOf[LoggerFactory]
.attacheRuntime(new ZioLoggerRuntime(runtime))
)
}
} yield ()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ object Slf4jBridgeSpec extends ZIOSpecDefault {

override def spec =
suite("Slf4jBridge")(
test("parallel init") {
for {
_ <-
ZIO.foreachPar((1 to 5).toList) { _ =>
ZIO
.succeed(org.slf4j.LoggerFactory.getLogger("SLF4J-LOGGER").warn("Test {}!", "WARNING"))
.provide(Slf4jBridge.initialize)
}
} yield assertCompletes
},
test("logs through slf4j") {
val testFailure = new RuntimeException("test error")
for {
Expand Down

0 comments on commit 378e144

Please sign in to comment.