使用 SLF4J MDC 进行 Scala Akka 日志记录

编程入门 行业动态 更新时间:2024-10-25 21:19:38
本文介绍了使用 SLF4J MDC 进行 Scala Akka 日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在配置我的 Akka 应用程序以使用此处指定的 SLF4J 记录器:

I'm configuring my Akka application to use the SLF4J logger as specified here:

doc.akka.io/docs/akka/2.3.4/scala/logging.html

在幕后,我依靠 Logback 进行日志记录.我正在开发一个用于日志记录的通用模块,用户可以在他们的演员系统中使用它.主要是,我正在创建一个他们可以混合的特征.

Underneath the hood, I'm depending on Logback to do the logging. I'm developing a common module for logging purposes that users can use in their actor systems. Mainly, I'm creating a trait they can mixin.

我有一个特点:

我有这样的东西:

trait ActorLogger { val log: DiagnosticLoggingAdapter = Logging(this); }

我有一些额外的逻辑可以将 MDC 值添加到 DiagnosticLoggingAdapter 的 MDC.现在的问题是:如果用户想要混合到他们的非演员类中,我会完全公开一个不同的记录器.所以我可能有这样的事情:

I have some extra logic which will add MDC values to the DiagnosticLoggingAdapter's MDC. The problem is now this: I expose a different logger entirely if users want to mixin to their non-actor classes. So I might have something like this:

trait ClassLogger { val log = LoggerFactory getLogger getClass.getName }

我希望将 MDC 值转移到这个记录器.例如,如果我将 MDC 值放入我的 DiagnosticAdapterLogger,我应该能够从 org.slf4j.MDC 获取这些值

I want the MDC values to carry over to this logger. So for example, if I put MDC values into my DiagnosticAdapterLogger, I should expect to be able to get those values from org.slf4j.MDC

如何以干净的方式实现这一目标?

How can this be achieved in a clean way?

谢谢!

推荐答案

如果你在 actor 系统之外的所有代码都是单线程的(即你没有产生任何额外的期货或线程),那么有一个比那个更简单的解决方案@jasop 引用.

If all your code outside the actor system is single-threaded (i.e. you don't spawn any additional futures or threads), there's a simpler solution than the one @jasop references.

我有这个 mixin 负责填充 MDC 的内部和外部 actor:

I have this mixin that takes care of populating the MDC both inside and outside actors:

import akka.actor.DiagnosticActorLogging import akka.contrib.pattern.ReceivePipeline import org.slf4j.MDC import scala.collection.JavaConverters.mapAsJavaMapConverter trait MdcActorLogging extends DiagnosticActorLogging { this: ReceivePipeline => /** * This is for logging in Akka actors. */ override def mdc(message: Any): akka.event.Logging.MDC = { message match { case MyMessage(requestId) => Map("requestId" -> requestId) case _ => Map() } } /** * This makes the MDC accessible for logging outside of Akka actors by wrapping the actor's * `receive` method. * Implements the [[doc.akka.io/docs/akka/2.4/contrib/receive-pipeline.html ReceivePipeline]] * pattern. */ pipelineOuter { case e @ MyMessage(requestId) => val origContext = MDC.getCopyOfContextMap val mdcWithPath = Map("requestId" -> requestId, // inside actors this is already provided, but outside we have to add this manually "akkaSource" -> self.path.toString) MDC.setContextMap(mdcWithPath.asJava) ReceivePipeline.Inner(evt) // invoke actual actor logic .andAfter { if (origContext != null) MDC.setContextMap(origContext) else MDC.clear() } case e => ReceivePipeline.Inner(e) // pass through } }

非演员代码可以使用任何记录器,例如混入 com.typesafe.scalalogging.LazyLogging 特性.

The non-actor code can use any logger, e.g. mix in the com.typesafe.scalalogging.LazyLogging trait.

更多推荐

使用 SLF4J MDC 进行 Scala Akka 日志记录

本文发布于:2023-11-25 03:57:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   MDC   SLF4J   Akka   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!