Skip to content

Commit

Permalink
slf4j bridge - init lock
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Nov 19, 2023
1 parent 1803b08 commit e6c7c4b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package zio.logging.slf4j.bridge
import org.slf4j.impl.ZioLoggerFactory
import zio.{ Runtime, ZIO, ZLayer }

import java.util.concurrent.locks.ReentrantLock

object Slf4jBridge {

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

private val initLock: ReentrantLock = new ReentrantLock()

private def layer(nameAnnotationKey: String): ZLayer[Any, Nothing, Unit] =
ZLayer {
ZIO.runtime[Any].flatMap { runtime =>
ZIO.succeed {
ZioLoggerFactory.initialize(new ZioLoggerRuntime(runtime, nameAnnotationKey))
initLock.lock()
try
ZioLoggerFactory.initialize(new ZioLoggerRuntime(runtime, nameAnnotationKey))
finally
initLock.unlock()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import org.slf4j.impl.StaticMarkerBinder
import zio.test._
import zio.{ Cause, Chunk, LogLevel, ZIO, ZIOAspect }

import java.util.UUID

object Slf4jBridgeSpec extends ZIOSpecDefault {

final case class LogEntry(
Expand All @@ -17,6 +19,17 @@ object Slf4jBridgeSpec extends ZIOSpecDefault {

override def spec =
suite("Slf4jBridge")(
test("parallel init") {
val uuids = List.fill(5)(UUID.randomUUID())
for {
_ <-
ZIO.foreachPar(uuids) { u =>
ZIO
.succeed(org.slf4j.LoggerFactory.getLogger("SLF4J-LOGGER").warn("Test {} {}!", "WARNING", u.toString))
.provide(Slf4jBridge.initialize)
}
} yield assertTrue(true)
},
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 @@ -17,6 +17,8 @@ package zio.logging.slf4j.bridge

import zio.{ Runtime, ZIO, ZLayer }

import java.util.concurrent.locks.ReentrantLock

object Slf4jBridge {

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

private val initLock: ReentrantLock = new ReentrantLock()

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))
initLock.lock()
try
org.slf4j.LoggerFactory
.getILoggerFactory()
.asInstanceOf[LoggerFactory]
.attacheRuntime(new ZioLoggerRuntime(runtime))
finally
initLock.unlock()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package zio.logging.slf4j.bridge
import zio.test._
import zio.{ Cause, Chunk, LogLevel, ZIO, ZIOAspect }

import java.util.UUID

object Slf4jBridgeSpec extends ZIOSpecDefault {

final case class LogEntry(
Expand All @@ -15,6 +17,17 @@ object Slf4jBridgeSpec extends ZIOSpecDefault {

override def spec =
suite("Slf4jBridge")(
test("parallel init") {
val uuids = List.fill(5)(UUID.randomUUID())
for {
_ <-
ZIO.foreachPar(uuids) { u =>
ZIO
.succeed(org.slf4j.LoggerFactory.getLogger("SLF4J-LOGGER").warn("Test {} {}!", "WARNING", u.toString))
.provide(Slf4jBridge.initialize)
}
} yield assertTrue(true)
},
test("logs through slf4j") {
val testFailure = new RuntimeException("test error")
for {
Expand Down

0 comments on commit e6c7c4b

Please sign in to comment.