Logger 与 System.out.println

发布于 2024-08-31 15:41:52 字数 183 浏览 19 评论 0原文

我正在使用 Eclipse 的 PMD 插件,当使用 System.out.println() 时它给我一个错误,并解释:

使用了System.(out|err).print,请考虑使用记录器。

我的问题是 - 什么是记录器?如何使用它打印到屏幕上?为什么更好?

I'm using the PMD plugin for eclipse and it gives me an error when using System.out.println() with the explanation:

System.(out|err).print is used, consider using a logger.

My question is - What is a Logger? How is it used to print to the screen? Why is it better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倾城月光淡如水﹏ 2024-09-07 15:41:52

请参阅log4j 简短介绍

问题在于使用 System.out 打印调试或诊断信息。这是一种不好的做法,因为您无法轻松更改日志级别、关闭它、自定义它等。

但是,如果您合法地使用 System.out 向用户打印信息,那么您可以忽略此操作警告。

See this short introduction to log4j.

The issue is in using System.out to print debugging or diagnostic information. It is a bad practice because you cannot easily change log levels, turn it off, customize it, etc.

However if you are legitimately using System.out to print information to the user, then you can ignore this warning.

残花月 2024-09-07 15:41:52

如果您在应用程序的 main() 方法中使用 System.out|err.println(..) 在控制台上打印用户信息,那么您没有做错任何事情。您可以通过插入注释“//NOPMD”来删除该消息。

System.out.println("Fair use of System.out.println(..).");// NOPMD

为此目的,PMD 违规大纲中有一个“标记为已审核”选项。

当然,您可以使用以下代码片段欺骗 PMD:

PrintStream out=System.out;
out.println("I am fooling PMD.");  

在 main() 方法之外使用 Log-System,例如 Log4j。

更新:

您还可以修改 PMD 规则“SystemPrintln”以使用以下 XPath:

//MethodDeclaration[@MethodName!="main"]//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
] | //Initializer//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
]

这将忽略代码中名为“main”的任何方法中的 System.out.println 等,但检查 System初始化代码中的.out.println。
我喜欢这个,因为从我的角度来看,System.out.println 在方法“main(String args[])”中是安全的。但请谨慎使用,我必须检查 AST 中的何处也可能出现 System.out.println,并且必须调整 XPath。

If you are using System.out|err.println(..) to print out user-information on console in your application's main()-method, you do nothing wrong. You can get rid of the message via inserting a comment "//NOPMD".

System.out.println("Fair use of System.out.println(..).");// NOPMD

There is a "Mark as reviewed"-Option in the PMD-Violations Outline for this purpose.

Of course you can trick PMD with following code snippet:

PrintStream out=System.out;
out.println("I am fooling PMD.");  

Outside of your main()-Method use a Log-System like eg Log4j.

UPDATE:

You can also modify the PMD-Rule "SystemPrintln" to use the following XPath:

//MethodDeclaration[@MethodName!="main"]//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
] | //Initializer//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
]

This will ignore System.out.println etc in any method named 'main' in your code, but check for System.out.println in initializer code.
I like this, because from my point of view, System.out.println is safe in method 'main(String args[])'. But use with caution, I have to check, where in the AST a System.out.println can occur also and have to adapt the XPath.

痴意少年 2024-09-07 15:41:52

此链接提供了有关如何使用 Log4j 的更简洁信息: 不要使用 System.out.println! 然而它只有一个小缺陷,你不应该将库放在 /jre/lib/ext 中,而只是在应用程序的运行时类路径中并将其一起发送。

优点是您可以使用日志记录级别来指示信息的重要性,以便您可以从外部配置在输出中显示/隐藏哪些级别(这样您就不会因为无用的信息而烦恼) ,输出应该是什么样子(例如包括时间戳,线程ID,类名,方法名等)以及输出应该写入哪里(例如控制台,文件,电子邮件等)以及例如文件的情况以及如何创建它们(例如按年、月和/或日分组)。

有几种记录器实现,例如 Java SE 的内置 java.util.logging.Logger,方便的Apache Commons Logging< /a>,流行的 Apache Log4j,它的后继者 Logback等。可以使用Slf4j 作为额外的抽象层,可以在需要时在任何这些记录器之间进行切换。

This link provides more concise information about how to use Log4j: Don't use System.out.println! It has however only one little flaw, you should rather not put the library in /jre/lib/ext, but just in the runtime classpath of your application and ship it along.

The advantage is that you can use logging levels to indicate the importance of the information, so that you can configure externally which levels to show/hide in the output (so that you don't get annoyed by the -after all- useless information), how the output should look like (e.g. include a timestamp, thread ID, classname, methodname, etc) and where the output should be written to (e.g. the console, a file, an email, etc) and in case of for example files also how they should be created (e.g. group by year, month and/or day).

There are several logger implementations like the Java SE's builtin java.util.logging.Logger, the convenienced Apache Commons Logging, the popular Apache Log4j, its successor Logback, etc. You can use Slf4j as an extra abstraction layer to switch between any of those loggers whenever needed.

一杆小烟枪 2024-09-07 15:41:52

记录器有多个级别的记录。

如果我们正在编写一个真正的短程序,只是为了学习目的 System.out.println 就可以了,但是当我们开发一个高质量的软件项目时,我们应该使用专业的记录器,并且应该避免 SOP。

专业的记录器提供不同级别的记录和灵活性。我们可以得到相应的日志消息。例如,X 组消息应仅在 Production 上打印,Y 组消息应在 ERROR 上打印,等等。

我们在 System.out 中重定向消息的选项有限,但对于记录器你有提供许多选项的附加程序。我们甚至可以创建一个自定义输出选项并将其重定向到该选项。

Loggers has multiple levels for logging.

If we are writing a real short program, just for learning purposes System.out.println is fine but when we are developing a quality software project, we should use professional logger and SOPs should be avoided.

A professional loggers provides different levels for logging and flexibility. We can get the log message accordingly. For example, group X messages should be printed only on PRODUCTION, group Y messages should be printed on ERROR, etc.

We have limited option for redirecting the messages in System.out, but in case of a logger you have appenders which provides numbers of options. We can even create a custom output option and redirect it to that.

烂人 2024-09-07 15:41:52

PMD 似乎假设您调用 System.out.println() 是出于调试目的;像“我在你的方法中,执行你的代码”之类的东西。

如果您这样做,那么您将可以更好地向 Log4J,因为它将有多个流媒体选项,而不仅仅是屏幕。

但是,如果您正在执行控制台应用程序并在其中调用 System.out,请忽略该警告。

It appears that PMD is assuming that you are calling System.out.println() for debugging purposes; stuff like "Im in ur method, executing ur codez".

If you're doing that, you're going to have a much better time writing to a logger like Log4J, as it'll have multiple streaming options than just to screen.

If, however, you're doing a console application and are calling System.out as part of that, ignore the warning.

遗忘曾经 2024-09-07 15:41:52

System.out.println 不好用,因为它无法配置。相反,Logger 可以配置为在各个级别上登录。它还有很多其他功能。

System.out.println is not good to use as it cannot be configured. In stead, Logger can be configured to log on various levels. It has whole lot of other features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文