java Logger.getLogger(className) 和 LogFactory.getLog(className) 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10246708/
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-31 00:12:34  来源:igfitidea点击:

Difference between Logger.getLogger(className) and LogFactory.getLog(className )?

javalogginglog4j

提问by Shashank Kadne

I know its a package difference

我知道它的包装差异

1) org.apache.log4j.Logger logger = Logger.getLogger(clazz);

1) org.apache.log4j.Logger logger = Logger.getLogger(clazz);

2) org.apache.commons.logging.Log log = LogFactory.getLog(clazz);

2) org.apache.commons.logging.Log log = LogFactory.getLog(clazz);

The first one uses loggers via log4jand the second one uses commons.logging. We have a huge project where in some classes loggers are configured using log4jand in some cases its commons.logging.

第一个使用记录器 via log4j,第二个使用commons.logging. 我们有一个庞大的项目,其中在某些类中使用配置记录器log4j,在某些情况下使用它的commons.logging.

I did find a log4j property file though.Is there a similar property file for commons.logging? Where do I configure for commons-logging ?. I am unable to see the logs generated by commons-logging.

我确实找到了一个 log4j 属性文件。是否有类似的属性文件commons.logging?我在哪里配置 commons-logging ?。我无法看到由commons-logging.

Any help is appreciated.

任何帮助表示赞赏。

采纳答案by Tomasz Nurkiewicz

Yes, commons-logging is a facade API that was suppose to abstract you from underlying logging framework (in practice there was a choice between log4jand java.util.logging) so that you could switch from one to another without touching the code - just by switching libraries available on the CLASSPATH.

是的,commons-logging 是一个门面 API,它应该将你从底层日志框架中抽象出来(实际上在log4j和之间有一个选择java.util.logging),这样你就可以在不接触代码的情况下从一个切换到另一个 - 只需切换可用的库类路径。

Unfortunately due to some design mistakes it had issues with complex class-loading environments, like application servers. Currently it is effectively superseded by slf4j.

不幸的是,由于一些设计错误,它在复杂的类加载环境中遇到了问题,比如应用程序服务器。目前它已被slf4j有效取代。

In your case I would recommend sticking with one API - either Log4J or commons-logging, even though commons-logging will (most likely) delegate to log4J. You can also migrate to using SLF4J and install bridging APIs, but this is slightly more advanced.

在您的情况下,我建议坚持使用一个 API - Log4J 或 commons-logging,即使 commons-logging 将(很可能)委托给 log4J。您还可以迁移到使用 SLF4J 并安装桥接 API,但这稍微高级一些。