SLF4J是否可以在多线程应用程序中用于记录到数据库? [英] Is SLF4J good to be used in a multithreaded application for logging to a database?

查看:162
本文介绍了SLF4J是否可以在多线程应用程序中用于记录到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在研究多线程应用程序。我已经在使用Log4j将语句记录到文件中。

我最近一直在研究登录到Mysql数据库的可能性。


日志记录组件必须在高负载下可靠并且必须是线程安全的。

我听说过SFL4J。我对SLF4J的理解是它只是一个外观或抽象层。



I am working on a multithreaded application. I am already using Log4j to log statements to files.
I have been recently looking into the possibility of logging to a Mysql database.

The logging component has to be reliable under a heavy load and has to be thread safe.
I heard of SFL4J. My understanding of SLF4J is that is just a facade or abstraction layer.


  • 我想知道SLF4J是否是一个很好的数据库记录解决方案在
    多线程应用程序中使用?


  • 在负载很重的情况下,添加一个层(如缓冲区或队列)并让线程记录到它不是直接调用日志记录线程(日志记录线程会将队列中发现的语句逐个发送到数据库)?

任何提示,最佳做法或代码示例都会非常感激,

问候,

Any tips, best practices or code example would greatly be appreciated,
Regards,

推荐答案

你是对的,SFL4J只是一个外观,它允许图书馆和框架的编写者通过外观进行日志记录并拥有应用程序指定自己的日志框架。在应用程序级别,没有令人信服的理由使用SLF4J,您也可以直接使用log4j。唯一的好处是你可以在未来的日期交换到更少的更改。这并不是那么引人注目,除非你有一个案例,你不确定你想要使用哪个日志实现,在这种情况下你可以使用抽象层,以便你可以在比较测试中交换实现。

You're right that SFL4J is just a facade, it allows writers of libraries and frameworks to do their logging through the facade and have the application specify its own logging framework. At the application level there's not as compelling a reason to use SLF4J, you might as well use log4j directly. The only benefit would be that you could swap over to logback at a future date with less changes. That's not all that compelling, unless you have a case where you are uncertain which logging implementation you want to go with, in which case you could use the abstraction layer so that you could swap out implementations in comparison tests.

如果您使用SLF4j,我认为它不会影响并发性。您可能想要为实际记录器考虑不同的appender(或自己动手),但这不会影响您调用SLF4J的方式。并发性受影响的唯一地方是appender。

If you do use SLF4j I don't think it would affect concurrency. You might want to consider different appenders (or roll your own) for the actual logger, it wouldn't affect how you called SLF4J though. The only place where concurrency would be impacted is in the appender.

这篇关于SLF4J是否可以在多线程应用程序中用于记录到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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