[英]What's the best approach in jar/library creation?
I need to create jar/library that does some stuff and writes some logs.我需要创建 jar/library 来做一些事情并写一些日志。 For logs I was thinking about java.util.logging.Logger vs Logback.对于日志,我在考虑 java.util.logging.Logger 与 Logback。 So the question is: Should I prefer using java out-of-the-box logging or it's totally fine to include some third-party tools?所以问题是:我应该更喜欢使用 java 开箱即用的日志记录还是包含一些第三方工具完全没问题?
Or it should be purely the matter of my needs and which one to use doesn't really matter?或者它应该纯粹是我的需求问题,使用哪一个并不重要?
Use a facade, like slf4j.使用外观,例如 slf4j。 Think of it from the point of view of the person using your jar in their application.从在他们的应用程序中使用您的 jar 的人的角度来考虑它。 They will want all the logging going to one place, whether it is logging for your jar or for the rest of the application, they will not want to configure your library's logging separately.他们希望所有日志记录都集中到一个地方,无论是为您的 jar 还是应用程序的 rest 进行日志记录,他们都不想单独配置库的日志记录。
Using a logging facade means your library writes to whatever log the rest of the application uses.使用日志外观意味着您的库写入应用程序的 rest 使用的任何日志。 The application should be in charge of logging, not your jar.应用程序应该负责记录,而不是您的 jar。
For which facade to use, slf4j seems like a better choice, commons logging can get in trouble where multiple classloaders are involved.对于使用哪个门面,slf4j 似乎是一个更好的选择,如果涉及多个类加载器,公共日志记录可能会遇到麻烦。 See Difference between Simple Logging Facade for Java and Apache Commons Logging请参阅Java 和 Apache Commons Logging 的 Simple Logging Facade 之间的区别
One can use System.Logger
一个可以使用System.Logger
System.Logger instances log messages that will be routed to the underlying logging framework the LoggerFinder uses. System.Logger 实例记录将被路由到 LoggerFinder 使用的底层日志框架的消息。 System.Logger instances are typically obtained from the System class, by calling System.getLogger(loggerName) or System.getLogger(loggerName, bundle). System.Logger 实例通常通过调用 System.getLogger(loggerName) 或 System.getLogger(loggerName, bundle) 从 System class 获取。
Loggers across libraries always were a pain.跨库的记录器总是很痛苦。 Java.util.logging incorporated Logging into the Standard Edition. Java.util.logging 将 Logging 纳入标准版。 Since Java 9 there is a Standard Edition way to integrate loggers.从 Java 9 开始,有一种集成记录器的标准版方法。
This leaves the choice to the library user.这将选择权留给图书馆用户。 No longer a mix, version blues, etcetera.不再是混合,版本布鲁斯,等等。 The exact logger in a straight application could be java util Logger.直接应用程序中的确切记录器可能是 java util Logger。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.