Java 库的记录器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9740569/
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
Logger for Java library
提问by Jo?o Daniel
I'm writing a library that gathers various functions that I'll be using in different applications. I want it to generate log statements visible for the user of the library, i.e., if I'm building an application and I'm using the library, I want the library to generate log statements visible to me. How do I do that? Since the log file will be configured by the developer of the application, how will my library know how to log?
我正在编写一个库,该库收集了我将在不同应用程序中使用的各种功能。我希望它生成对库用户可见的日志语句,即,如果我正在构建一个应用程序并且我正在使用该库,我希望该库生成对我可见的日志语句。我怎么做?由于日志文件将由应用程序的开发人员配置,我的库如何知道如何记录?
采纳答案by Joe23
If you are developing a librarythe others will include in their application you should use a logging facade. Otherwise you force the users of your library to configure and include the logging framework you have choosen in addition to the frameworkthey chose for their application.
如果你正在开发一个库,其他人将在他们的应用程序中包含你应该使用日志外观。否则,除了他们为其应用程序选择的框架之外,您还强制库的用户配置并包含您选择的日志记录框架。
For instance if you use log4jbut the developer using your library uses logbackhe will have to include a log4j configuration file and the log4j jar (or take other measures) to make your library happy.
例如,如果您使用log4j,但使用您的库的开发人员使用logback,他将必须包含一个 log4j 配置文件和 log4j jar(或采取其他措施)以使您的库满意。
Logging Facades solve this problem (from Apache Commons Logging):
Logging Facades 解决了这个问题(来自 Apache Commons Logging):
When writing a library it is very useful to log information. However there are many logging implementations out there, and a library cannot impose the use of a particular one on the overall application that the library is a part of.
The Logging package is an ultra-thin bridge between different logging implementations. A library that uses the commons-logging API can be used with any logging implementation at runtime. Commons-logging comes with support for a number of popular logging implementations, and writing adapters for others is a reasonably simple task.
在编写库时,记录信息非常有用。然而,有许多日志实现,并且库不能将特定的使用强加于库所属的整个应用程序。
Logging 包是不同日志实现之间的超薄桥梁。使用 commons-logging API 的库可以在运行时与任何日志记录实现一起使用。Commons-logging 支持许多流行的日志记录实现,为其他人编写适配器是一项相当简单的任务。
Or the reasoning from SLF4J:
或者来自 SLF4J 的推理:
The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deploymenttime.
Simple Logging Facade for Java 或 (SLF4J) 充当各种日志框架的简单外观或抽象,例如 java.util.logging、log4j 和 logback,允许最终用户在部署时插入所需的日志框架。
Candidates for logging facades are:
记录门面的候选人是:
回答by Rostislav Matl
The location of your log file is set outside the package, in a logging framework configuration. The config file is usually on classpath. How it looks depends on what logging framework you use - I recommend to go with SLF4Jand Logback.
日志文件的位置设置在包外的日志框架配置中。配置文件通常在类路径上。它的外观取决于您使用的日志记录框架 - 我建议使用SLF4J和Logback。
回答by nwinkler
The standard solutions for this are:
对此的标准解决方案是:
- Log4J: http://logging.apache.org/log4j/
- SLF4J: http://www.slf4j.org/
- JDK Built-in Logger: https://docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html
- Log4J:http: //logging.apache.org/log4j/
- SLF4J:http://www.slf4j.org/
- JDK 内置记录器:https: //docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html
All of them will allow you to configure the log file location through configuration.
所有这些都将允许您通过配置来配置日志文件位置。