简体   繁体   English

在java的Utility类中使用logger

[英]Use of logger in Utility classes of java

i am just wondering 'bout use of Logger API in Utility classes while writing java code. 我只是想知道在编写java代码时,在实用程序类中使用Logger API。 we can always use logger where we will be using our utility class to log a proper message BUT what i want to ask is-- 我们总是可以使用logger,我们将使用我们的实用程序类来记录正确的消息但是我想问的是 -

Is it a good practice to use Logger in utility classes? 在实用程序类中使用Logger是一个好习惯吗?

I assume you are writing your own *Util code. 我假设您正在编写自己的* Util代码。

Personally I avoid using Logger in utility classes, because of the "noise" in the log files. 我个人避免在实用程序类中使用Logger,因为日志文件中存在“噪音”。 If your utility class is well tested, you may remove the log statements. 如果您的实用程序类经过了充分测试,则可以删除日志语句。

Just remember to only log, and not perform any business logic in the log statement (whether utility class or not). 只记得只记录,而不是在日志语句中执行任何业务逻辑(无论是否为实用程序类)。

Eg I saw below in one of my projects, and this is not good practice. 我在下面的一个项目中看到过,这不是一个好习惯。

log.info("Added = " + list.add("someString"));
  1. The whole objective of logging in the code is that you should be able debug or look for any abnormal application behavior just by looking into the logs. 登录代码的整个目标是,您应该只需查看日志就可以调试或查找任何异常的应用程序行为。
  2. So if the utility class you referred hosts an important logic/algorithm which you think logger here will help you debug any issue in future then you should log it. 因此,如果您引用的实用程序类拥有一个重要的逻辑/算法,您认为此处的logger将帮助您将来调试任何问题,那么您应该记录它。
  3. But if your util class contains concise utility functions where hardly any complex logic is involved then logging should be avoided there. 但是如果你的util类包含简洁的实用程序函数,几乎不涉及任何复杂的逻辑,那么应该避免使用日志记录。

It is a great Idea to use java.util.logging.Logger in all utility classes. 在所有实用程序类中使用java.util.logging.Logger是一个很好的主意。 The most Java logging frameworks are able to collect Logs from this logger and you won't need to bundle any libraries, because it comes along with the jre. 大多数Java日志记录框架都能够从此记录器收集日志,您不需要捆绑任何库,因为它随jre一起提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM