Using Actor Logging Via SLF4j and logback vs using org.slf4j to log using SLF4J and logback

家住魔仙堡 提交于 2019-12-25 09:30:25

问题


I am confused with the best practice to use in logging. I found by reading that SLF4J removes the coupling between the application and logging API etc. So if i use actor logging, will it not be closely coupled to application?.I have also checked a lot of github codes and i noticed that they only use actor logging and not org.SLF4J.LoggerFactory logging. Why?

Some links which i referred

SACHA'S BLOG OF PROGRAMMATICALNESS

Another Good read

stack Overflow -> Checked the last answer

Thanks in advance


回答1:


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



来源:https://stackoverflow.com/questions/41262196/using-actor-logging-via-slf4j-and-logback-vs-using-org-slf4j-to-log-using-slf4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!