Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slf4j bridge - init lock #789

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, sure but i think it will be better to fix it in next major release

)
}
} 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