如何使用log4j 2.0和slf4j以及Commons一起登录 [英] How to use log4j 2.0 and slf4j and Commons Logging together

查看:164
本文介绍了如何使用log4j 2.0和slf4j以及Commons一起登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开始一个新的Webapp(在tomcat 6上运行)
我有使用slf4j的组件和使用commons记录的组件
我计划使用log4j 2.0作为日志实现由于几个原因(主要用于appenders:SocketAppender和SyslogAppender,但也因为提升的配置重新加载而不丢失日志事件)

I currently am starting a new Webapp (running on tomcat 6) I have components using slf4j and components using commons logging I plan to use log4j 2.0 as log implementation due to several reasons (mainly for the appenders:SocketAppender and SyslogAppender but also because of the promoted config reloading without loss of log events)

现在我的问题是:
- 我应该使用哪个界面编程我的新课程? loag4j还是slf4j?甚至公共记录?

Now my questions are: - To which interface do I program my new classes? loag4j or slf4j? or even commons logging?


  • 部署罐子的首选方法是什么?把它们放在我的应用程序大战中还是将它们放入tomcat库中?

  • What's the preferred way to deploy the jars? put them in my application war or do i put them into the tomcat libs?

我需要部署哪些罐子? log4j(包括slf4j和commons绑定),commons logging(slf4j-api-1.7.2.jar)和slf4j api(slf4j-api-1.7.2.jar)

what jars do I need to deploy? log4j (including slf4j and commons bindings), commons logging (slf4j-api-1.7.2.jar) and slf4j api (slf4j-api-1.7.2.jar)

推荐答案


我要为哪个界面编程我的新课程? loag4j还是slf4j?

To which interface do I program my new classes? loag4j or slf4j?

如果您打算使用SLF4J,请编程到该界面。它为底层日志记录实现提供了最大的灵活性。 slf4j的目的是成为一个可以编程的接口,因此将来如果你决定切换到logback,你将不必重写代码。

If you're going to use SLF4J, program to that interface. It offers the most flexibility for underlying logging implementation. The point of slf4j is to be an interface that you can program to, so in the future if you decide to switch to, say, logback, you won't have to rewrite your code.


部署罐子的首选方法是什么?把它们放在我的应用程序大战中还是将它们放入tomcat库中?

What's the preferred way to deploy the jars? put them in my application war or do i put them into the tomcat libs?

将它们放入你的WAR中。

Put them in your WAR.

将JAR放入Tomcat libs目录的唯一原因(imo)是他们需要加载本机库。由于Java不允许您从两个不同的类加载器加载相同的本机库,因此您需要将其放在一个公共位置。但这不适用于此。

The only reason (imo) to put JARs in the Tomcat libs directory is if they need to load a native library. Since Java won't allow you to load the same native library from two different classloaders, you need to put then in a common location. But that doesn't apply here.

有些人认为lib目录是一种节省空间的方法。当服务器级机器具有1 GB的RAM时,这可能是有效的,但它已不再存在。并且避免 lib 意味着您可以避免大多数难以调试的类加载问题。

Some people think of the lib directory as a way to save space. That may have been valid when "server-class" machines had 1 GB of RAM, but it isn't any more. And avoiding lib means you avoid most of the hard-to-debug classloading issues.


我需要部署哪些罐子?

what jars do I need to deploy?




  • slf4j-api是基本API

  • slf4j- log4j12将实际日志记录路由到Log4J

  • jcl-over-slf4j拦截Commons Logging并将其路由到SLF4J(到Log4J)

  • log4j将是你的物理日志框架

    • slf4j-api is the basic API
    • slf4j-log4j12 routes the actual logging to Log4J
    • jcl-over-slf4j intercepts Commons Logging and routes it through SLF4J (to Log4J)
    • log4j will be your physical logging framework
    • 我假设您已经有Log4J的配置和/或很乐意编写该配置。如果没有,你所关心的只是处理内部使用Log4J的代码,那就是 log4j-over-slf4j ,它将拦截Log4J调用。然后你需要选择一个框架,比如Logback。

      I'm assuming that you already have configuration for Log4J and/or are comfortable writing that configuration. If not, and all you care about is dealing with code that uses Log4J internally, there's log4j-over-slf4j, which will intercept Log4J calls. Then you need to pick a framework, such as Logback.

      (注意:我最初添加了所有这些软件包的链接,但没有足够的代表来发布它们所以这里是指向Maven的单一链接显示所有SLF4J包的存储库)

      (note: I originally added links to all of these packages, but don't have enough rep to post them. So here's a single link to the Maven repository with all of the SLF4J packages highlighted)

      这篇关于如何使用log4j 2.0和slf4j以及Commons一起登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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