java 使用 org.apache.commons.logging.LogFactory.getLog 的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1536651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:59:40  来源:igfitidea点击:

Best Practice of Using org.apache.commons.logging.LogFactory.getLog

javaconfigurationlogging

提问by Cheok Yan Cheng

May I know what is the best practice of using org.apache.commons.logging.LogFactory.getLog?

我可以知道使用 org.apache.commons.logging.LogFactory.getLog 的最佳实践是什么吗?

For me, I use it the following way :

对我来说,我按以下方式使用它:

public class A
{
    private static final Log log = LogFactory.getLog(A.class);
}

public class B
{
    private static final Log log = LogFactory.getLog(B.class);
}

So, if I have 100 of classes, 100 of static log object will be created?

那么,如果我有 100 个类,将创建 100 个静态日志对象?

Or is it best to do it this way?

或者最好这样做?

public class A
{
    private static final Log log = LogFactory.getLog(Main.class);
}

public class B
{
    private static final Log log = LogFactory.getLog(Main.class);
}

All the 100 classes are referring to a same single same log?

所有 100 个类都指的是同一个同一个日志?

Thank you.

谢谢你。

采纳答案by David Rabinowitz

The first option - have a logger per class (or functionality). As most logging systems - log4j, logback, java util logging, etc. have the notion of logger hierarchy, you can match that to your package hierarchy and have finer control of which functionality you want to be logged at which level. For example:

第一个选项 - 每个类(或功能)都有一个记录器。由于大多数日志记录系统 - log4j、logback、java util 日志记录等都有记录器层次结构的概念,您可以将其与包层次结构相匹配,并更好地控制要在哪个级别记录哪些功能。例如:

com.example=WARN       # global setting
com.example.web=INFO   # increase logging for the web controllers
com.example.dao=DEBUG  # trying to track bugs in the database layer

回答by Cheok Yan Cheng

Please remember the 'static problem', which most of java developer ignore and which affect log4j, java.util.Logger, and SLF4J as well. You can read about it in the Apache Commons Wiki. As reported, if you're developing a library, planning to release it in a container (such as a j2ee container), shared among many application, is better use something like

请记住“静态问题”,大多数 Java 开发人员都忽略了它,它也会影响 log4j、java.util.Logger 和 SLF4J。您可以在Apache Commons Wiki 中阅读它。据报道,如果您正在开发一个库,计划将它发布到一个容器(例如 j2ee 容器)中,在许多应用程序之间共享,最好使用类似的东西

private transient final Log logger = LogFactory.getLog( this.getClass() );

回答by Henning

In the vast majority of cases, your way is the way to go.

在绝大多数情况下,您的方式就是要走的路。

The main advantage is that there is a logger (or category, or whatever) for each class that can be configured individually, like

主要优点是每个类都有一个记录器(或类别,或其他)可以单独配置,例如

log4j.logger.org.springframework.transaction=DEBUG

(assuming log4j) which will set the log level just for that class or package. If you use only one logger, you cannot do that.

(假设 log4j),它将仅为该类或包设置日志级别。如果您只使用一个记录器,则不能这样做。