为什么使用 akka eventhandler 进行日志记录
在以下文档中,事件处理程序被描述为取代日志记录 http://akka.io/docs/akka/1.2/general/event -handler.html
有一个事件处理程序取代了 Akka 中的日志系统:
akka.event.EventHandler
具体来说,此链接提供了如何在使用 slf4j 时执行此操作的示例: http://akka.io/docs/akka/1.2/general/slf4j.html
我的问题是‘这有什么好处? “为什么我要这样做,而不是仅仅使用使用标准模式的记录器?”
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
private static Logger log = LoggerFactory.getLogger(MyActor.class);
...
log.info("doing something");
基于线程或调度程序内部结构,通过在我没有看到的上述记录器模式上使用事件处理程序,我是否可以获得某种潜在的好处?如果没有,使用事件处理程序进行日志记录感觉就像没有明确原因地偏离了熟悉的模式。
感谢您的任何意见!
In the following document, event handlers are described as taking the place of logging
http://akka.io/docs/akka/1.2/general/event-handler.html
There is an Event Handler which takes the place of a logging system in Akka:
akka.event.EventHandler
Specifically, this link provides an example of how to do this while using slf4j:
http://akka.io/docs/akka/1.2/general/slf4j.html
My question is 'what advantages does this give? 'why would I do this instead of just using a logger using the standard pattern?'
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
private static Logger log = LoggerFactory.getLogger(MyActor.class);
...
log.info("doing something");
Is there some kind of underlying benefit I'd get, based on threading or dispatcher internals, by using an event-handler over the above logger pattern that I'm not seeing? If not, using an event-handler for logging feels like deviating from a familiar pattern for no clear reason.
Thanks for any input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
日志记录通常意味着 IO,这会减慢代码的运行速度。在参与者的上下文中,每条消息都必须在接收方法中以单个文件的形式进行处理,在某些情况下,这种开销可能会导致该方法完成的时间出现一个数量级(或更多)的差异。将日志记录移到运行接收块的线程(或 Erlang 领域中的进程)的控制流之外,这已经是基于 Erlang 的系统中的常见模式。如果您的参与者并不严重依赖于接收块的时间,那么您始终可以回退到标准日志记录模式(如果这对您来说更容易),但习惯基于 EventHandler 的方法可能是个好主意。
Logging generally means IO, which can slow down the operations of your code. In the context of an actor where each message must be processed single-file in your receive method, this overhead can, in some cases, make an order (or more) of magnitude difference in the time for that method to complete. It's already a common pattern within Erlang-based systems to move the logging outside of the flow of control of the thread (or process, in the Erlang sphere) that is running the receive block. If your actors are not heavily dependent on the timing of the receive block, you can always fall back to the standard logging pattern if that makes things easier for you, but it's probably a good idea to get used to the EventHandler-based approach.
@DParsin,您需要在类路径中包含 application.conf 文件,其中至少包含以下内容:
然后当然还要确保您正在使用 logback (或 slf4j-log4j 等)。
如果您的类路径中有 logback-classic-1.0.0.jar,请确保您的类路径中没有任何其他 SLF4J 适配器。
@DParsin, you would need to have application.conf file in your classpath with at least the following:
and then of course also make sure you are using logback (or slf4j-log4j etc.).
If you have logback-classic-1.0.0.jar in your classpath, make sure that you do NOT have any other SLF4J adapters in your classpath too.
在 Akka 1.2 中使用 Slf4jEventHandler 时要小心。您失去了在每个类的基础上设置日志记录级别的能力,而
原因是 Slf4jEventHandler 仅使用一个名为“akka.event.slf4j.Slf4jEventHandler”的记录器
Be careful using Slf4jEventHandler in Akka 1.2. You lose the ability to set logging levels on a per class basis versus
Reason being Slf4jEventHandler uses only one logger with the name of "akka.event.slf4j.Slf4jEventHandler"