通过SLF4j和登录使用Actor日志与通过SLF4J和登录使用org.slf4j进行日志记录 [英] Using Actor Logging Via SLF4j and logback vs using org.slf4j to log using SLF4J and logback

查看:125
本文介绍了通过SLF4j和登录使用Actor日志与通过SLF4J和登录使用org.slf4j进行日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对用于日志记录的最佳实践感到困惑.通过阅读我发现SLF4J消除了应用程序和日志记录API等之间的耦合.因此,如果我使用actor日志记录,它将不会与应用程序紧密耦合吗?我还检查了很多github代码,我注意到它们仅使用actor日志记录,而不是org.SLF4J.LoggerFactory日志记录.为什么?

我引用的某些链接

萨哈的程序化博客

另一本好书

堆栈溢出->检查了最后一个答案

预先感谢

解决方案

因此,我们处于一个新世界,在这个世界中,我们希望进行反应性和无障碍性,但在此之前需要一些信息.

Akka用于记录SLF4j api,它是常见日志记录库(即log4j,logback)之上的基础,但是为了不阻止actor进行所有这些异步操作.

所以:

log.info("Received msg: {} use thread {} to dispatch", message, Thread.currentThread().getName());

在演员中,日志定义为:

LoggingAdapter log = Logging.getLogger(getContext().system(), this);

将日志消息发送给日志参与者,并且IO操作实际上是由该专门参与者完成的.

Log4j或logback仅是实际日志记录的实现,这就是为什么您仍需要在项目中配置if之一的原因.

log4j.appender.file.layout.ConversionPattern=[%d,%p] [%X{sourceThread}] [%c{1}.%M:%L] %m%n

%X {sourceThread}实际上为您提供了由原始actor发出日志消息的执行线程,而不是为在loging actor内部执行IO操作的线程(由%t记录)提供了更多信息-有关此 http://doc.akka.io/docs/akka/current/java/logging.html

我的建议是使用log4j 2,因为它正在使用异步操作,并且不会阻塞登录角色之后的线程-有关SACHA'S BLOG OF PROGRAMMATICALNESS

Another Good read

stack Overflow -> Checked the last answer

Thanks in advance

解决方案

So we are in a new world, a world where we want to be reactive and non-blocking but some information before that.

Akka is using for logging SLF4j api that is a facade over common logging libraries i.e. log4j, logback, but does all of this async in order to not block actor.

So:

log.info("Received msg: {} use thread {} to dispatch", message, Thread.currentThread().getName());

in an actor where log is defined as:

LoggingAdapter log = Logging.getLogger(getContext().system(), this);

sends a logging message to logging actor, and the IO operations is actually done by this specialized actor.

Log4j or logback is only the implementation of actual logging, that's why you still need to configure one of if in your project.

log4j.appender.file.layout.ConversionPattern=[%d,%p] [%X{sourceThread}] [%c{1}.%M:%L] %m%n

%X{sourceThread} actually gives you the execution thread where the logging message was issued by original actor and not the thread that executes the IO operation inside logging actor (logged by %t) - more on this http://doc.akka.io/docs/akka/current/java/logging.html

My advice is to use log4j 2 since is using async operations and will not block the thread behind logging actor - more about log4j 2 performance on https://www.grobmeier.de/log4j-2-performance-close-to-insane-20072013.html

这篇关于通过SLF4j和登录使用Actor日志与通过SLF4J和登录使用org.slf4j进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆