Java Logger 和 System.out.println 有什么区别

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

What is the difference between Java Logger and System.out.println

javalogging

提问by RedRocket

I looked up the api about the logger class(here) and I was looking at the Logger.infomethod. I was confused when I saw its perimeter as a message displayed as a string public void info(String msg)which is same as System.out.println(). I am wondering what is the different between these two, and why do we use Logger instead of System.out.printlnwhen they can print out the same thing.

我查找了有关记录器类的 api(这里),我正在查看该Logger.info方法。当我看到它的周长显示为public void info(String msg)System.out.println(). 我想知道这两者之间有什么不同,为什么我们使用 Logger 而不是System.out.println它们何时可以打印出相同的东西。

In Logger.

在记录器中。

Logger.info("Hello")

Logger.info("Hello")

Output:

输出:

[INFO ] 2015-08-07 11:18:46.140 [main] ClassName Hello

In System.out.println

System.out.println

`System.out.println("Hello")

`System.out.println("你好")

Output: Hello

输出:你好

采纳答案by Elliott Frisch

Usually, because a Loggercan be configured to write to a file (and the console). It might also be configured at higher (or lower) granularity as to messaging. For example, you might configure (at runtime) for level of warn. In which case, that looger would not display debugor infomessages. It can include information such as the class that is writing, a line number, and a date and time (of the message).

通常,因为 aLogger可以配置为写入文件(和控制台)。它还可以在更高(或更低)的消息传递粒度上进行配置。例如,您可以(在运行时)配置warn级别。在这种情况下,该 looger 不会显示调试信息消息。它可以包括信息,例如正在编写的类、行号以及(消息的)日期和时间。

回答by Nathan Hughes

Using a logger allows you to abstract out a lot of details and do a lot more than you could writing to stdout.

使用记录器可以让您抽象出很多细节,并且做的事情比写入标准输出要多得多。

  • You can specify different destinations to write to. Different appenders write to a file, roll the file for given time periods, write to a queue or database, etc.

  • You can specify a consistent format for log messages instead of having to add it to every line you write to stdout.

  • You can choose an appender that buffers the output so that multiple threads can log without having the threads contend for the lock on the console object.

  • You can do a lot with filtering by category (typically package and classname) and log level (trace, debug, info, error, fatal), to make it easy to configure what log messages you want to see and which you want to ignore. With logging you can change the configuration in the logger properties or include a page in your application to change what gets filtered on the fly.

  • You can mix and match this stuff, for instance, setting up a specific smtp appender to email log messages for logging level of error or higher, in addition to writing the messages to a rolling file or whatever.

  • 您可以指定要写入的不同目标。不同的 appender 写入文件,在给定的时间段内滚动文件,写入队列或数据库等。

  • 您可以为日志消息指定一致的格式,而不必将其添加到写入 stdout 的每一行中。

  • 您可以选择缓冲输出的附加程序,以便多个线程可以记录日志,而无需线程争用控制台对象上的锁。

  • 您可以通过按类别(通常是包和类名)和日志级别(跟踪、调试、信息、错误、致命)过滤来做很多事情,以便轻松配置要查看的日志消息和要忽略的日志消息。通过日志记录,您可以更改记录器属性中的配置或在应用程序中包含一个页面来更改动态过滤的内容。

  • 您可以混合和匹配这些东西,例如,除了将消息写入滚动文件或其他内容之外,还可以设置特定的 smtp appender 以通过电子邮件发送日志消息以记录错误级别或更高级别。

回答by programmer

The main difference between a Logger and System.out.println is
Logger:Prints the text in a file(text file)
System.out.println:Prints the output in console

Logger is useful when you are going for any LIVE projects.
Because if any project is developed and deployed, then you cannot check the console. At that time Logger will be useful to track the flow of your projectalso you can find the Error or Exception if you have given the logger in catch{...} block.

Logger 和 System.out.println 之间的主要区别在于
Logger:打印文件中的文本(文本文件)
System.out.println:在控制台中打印输出

Logger 在您进行任何 LIVE 项目时非常有用。
因为如果有任何项目开发和部署,那么你不能检查控制台。那时 Logger 将有助于跟踪您的项目流程,如果您在 catch{...} 块中提供了记录器,您也可以找到错误或异常。

Also go through this Logger vs. System.out.println

还要通过这个Logger vs. System.out.println

回答by Vero J

  • But when we use any logging mechanism(log4j, slf4j, logback etc) we configure appenders and corresponding target log files for each package. By default console appender is turned off unless you explicitly configure it to log to a destination.
  • The System.out.println always logs the message to a console appender. So, it should be used only when we are sure that the console appender is configured in the logger configuration file. Otherwise we end up having logs logged on server console which is incorrect. Any log from inside the application should go to the corresponding application log and not the server log.
  • 但是当我们使用任何日志机制(log4j、slf4j、logback 等)时,我们会为每个包配置 appender 和相应的目标日志文件。默认情况下,控制台 appender 是关闭的,除非您明确将其配置为登录到目标。
  • System.out.println 始终将消息记录到控制台附加程序。因此,只有当我们确定在 logger 配置文件中配置了控制台 appender 时才应该使用它。否则我们最终会在服务器控制台上记录不正确的日志。应用程序内部的任何日志都应该转到相应的应用程序日志,而不是服务器日志。

Let me explain with an example.

让我用一个例子来解释。

  • If we are building an application called Tracker using a logging mechanism to run in a tomcat container, and we configured appenders for application logging with destination log file as tracker-application.log and we did not configure the console appender.
  • Then if the System.out.println is encountered by the JVM then the log will go to server log of the tomcat server which is wrong because server log should only have information about the server and not the application.
  • 如果我们正在使用日志记录机制构建一个名为 Tracker 的应用程序以在 tomcat 容器中运行,并且我们配置了应用程序日志记录的 appender,目标日志文件为 tracker-application.log 并且我们没有配置控制台 appender。
  • 然后,如果 JVM 遇到 System.out.println,那么日志将转到 tomcat 服务器的服务器日志,这是错误的,因为服务器日志应该只包含有关服务器的信息而不是应用程序的信息。

But if we created a console appender with a target log file then it will be logged properly.

但是,如果我们创建了一个带有目标日志文件的控制台 appender,那么它将被正确记录。

Hope I was clear. ;)

希望我很清楚。;)

For best practices refer Do not use System.out.println in server side codeor http://www.vipan.com/htdocs/log4jhelp.html

有关最佳实践,请参阅不要在服务器端代码中使用 System.out.printlnhttp://www.vipan.com/htdocs/log4jhelp.html

For differences between both of them please refer Logger vs. System.out.println

有关两者之间的差异,请参阅 Logger 与 System.out.println