Java Logger 与 System.out.println

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

Logger vs. System.out.println

javaeclipsepmdjava.util.logging

提问by Amir Rachum

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

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

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

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

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

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

采纳答案by matt b

See this short introduction to log4j.

请参阅log4j 的简短介绍

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

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

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

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

回答by Randolpho

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".

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

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.

如果你这样做,你将有更好的时间写入像Log4J这样的记录器,因为它有多个流选项而不仅仅是屏幕。

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

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

回答by fastcodejava

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

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

回答by BalusC

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.

此链接提供了有关如何使用 Log4j 的更简洁的信息:不要使用 System.out.println!然而,它只有一个小缺陷,您不应该将库放在/jre/lib/ext.

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).

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

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 Slf4jas an extra abstraction layer to switch between any of those loggers whenever needed.

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

回答by Michael Konietzka

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".

如果您在应用程序的 main() 方法中使用 System.out|err.println(..) 在控制台上打印用户信息,那么您没有做错任何事情。您可以通过插入注释“//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.

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

Of course you can trick PMD with following code snippet:

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

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

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

在你的 main()-Method 之外使用一个 Log-System,比如 Log4j。

UPDATE:

更新:

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

您还可以修改 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')
]

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.

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

回答by kalakanhu

Loggers has multiple levels for logging.

Loggers 有多个级别的日志记录。

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

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

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.

专业记录器提供不同级别的记录和灵活性。我们可以相应地获取日志消息。例如,X 组消息应该只打印在 PRODUCTION 上,Y 组消息应该打印在 ERROR 上,等等。

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.

我们在 中重定向消息的选项有限System.out,但在记录器的情况下,您有提供大量选项的附加程序。我们甚至可以创建自定义输出选项并将其重定向到该选项。