-
Notifications
You must be signed in to change notification settings - Fork 81
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 - fiber propagation #718
Changes from all commits
277bc23
f2f90db
ef4f1f3
5d96317
c8b54db
0ef0d07
3a3e0d6
34acb03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,8 @@ package org.slf4j.impl | |
|
||
import com.github.ghik.silencer.silent | ||
import org.slf4j.{ ILoggerFactory, Logger } | ||
import zio.ZIO | ||
import zio.logging.slf4j.bridge.Slf4jBridge | ||
import zio.{ Fiber, ZIO } | ||
|
||
import java.util.concurrent.ConcurrentHashMap | ||
import scala.jdk.CollectionConverters._ | ||
|
@@ -43,7 +43,13 @@ class ZioLoggerFactory extends ILoggerFactory { | |
private[impl] def run(f: ZIO[Any, Nothing, Any]): Unit = | ||
if (runtime != null) { | ||
zio.Unsafe.unsafe { implicit u => | ||
runtime.unsafe.run(f) | ||
runtime.unsafe.run { | ||
val fiberRefs = Fiber.currentFiber().map(_.asInstanceOf[Fiber.Runtime[_, _]].unsafe.getFiberRefs()) | ||
fiberRefs match { | ||
case Some(fiberRefs) => ZIO.setFiberRefs(fiberRefs) *> f | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern of running a ZIO workflow just to log something is not very efficient. We can get the current loggers from the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i think you are right, we can probably do it better, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
case None => f | ||
} | ||
} | ||
() | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package zio.logging.slf4j.bridge | ||
|
||
import zio.logging._ | ||
import zio.{ ExitCode, LogLevel, Runtime, Scope, ZIO, ZIOAppArgs, ZIOAppDefault, ZLayer } | ||
|
||
import java.util.UUID | ||
|
||
object Slf4jBridgeExampleApp extends ZIOAppDefault { | ||
|
||
private val slf4jLogger = org.slf4j.LoggerFactory.getLogger("SLF4J-LOGGER") | ||
|
||
private val logFilter: LogFilter[String] = LogFilter.logLevelByName( | ||
LogLevel.Info, | ||
"zio.logging.slf4j" -> LogLevel.Debug, | ||
"SLF4J-LOGGER" -> LogLevel.Warning | ||
) | ||
|
||
override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] = | ||
Runtime.removeDefaultLoggers >>> consoleJsonLogger( | ||
ConsoleLoggerConfig( | ||
LogFormat.label( | ||
"name", | ||
LoggerNameExtractor.loggerNameAnnotationOrTrace.toLogFormat() | ||
) + LogFormat.logAnnotation(LogAnnotation.UserId) + LogFormat.logAnnotation( | ||
LogAnnotation.TraceId | ||
) + LogFormat.default, | ||
logFilter | ||
) | ||
) >+> Slf4jBridge.initialize | ||
|
||
private val uuids = List.fill(2)(UUID.randomUUID()) | ||
|
||
override def run: ZIO[Scope, Any, ExitCode] = | ||
for { | ||
_ <- ZIO.logInfo("Start") | ||
_ <- ZIO.foreachPar(uuids) { u => | ||
ZIO.succeed(slf4jLogger.warn("Test {}!", "WARNING")) @@ LogAnnotation.UserId( | ||
u.toString | ||
) | ||
} @@ LogAnnotation.TraceId(UUID.randomUUID()) | ||
_ <- ZIO.logDebug("Done") | ||
} yield ExitCode.success | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are within the ZIO name space so you could just use
_currentFiber
here to avoid the need for the cast. I will also look at specializing the return type ofcurrentFiber
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i changed it slf4j v2 bridge to use
_currentFiber
, as it is in zio namespace