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

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

In Logger.

Logger.info("Hello")

Exit:

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

In System.out.println

`System.out.println (" Hello ")

Conclusion: Hello

+8
java logging
source share
4 answers

Usually, since Logger can be configured to write to a file (and the console). It can also be configured with more (or lower) granularity regarding messaging. For example, you can configure (at run time) the warning level. In this case, this looger will not display debugging or informational messages. It may include information such as a class that writes, line number, and date and time (messages).

+9
source share

Using a recorder allows you to abstract from many details and do much more than you could write to standard output.

  • You can specify different destinations for recording. Various applications write to a file, collapse the file for specified periods of time, write to a queue or database, etc.

  • You can specify a consistent format for log messages, rather than adding it to every line that you write to standard output.

  • You can select an application that buffers output so that multiple threads can register without having threads to lock on the console object.

  • You can do a lot with filtering by category (usually with a package and class name) and log level (trace, debugging, information, error, fatal) to make it easier to configure which log messages you want to see and which you want to ignore. Using the log, you can change the configuration in the log properties or include the page in the application to change what is filtered on the fly.

  • You can mix and match this stuff, for example, configure a specific smtp-appender for email log messages for a log level of error level or higher, in addition to writing messages in a sliding file or whatever.

+5
source share

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

Logger is useful when you go to any LIVE projects.
Because if any project is being developed and deployed, you cannot check the console. At that time, Logger will be useful for tracking the flow of your project , you can also find Error or Exception if you gave the logger in the catch block {...}.

Also go through Logger vs. System.out.println

+2
source share
  • But when we use some kind of logging mechanism (log4j, slf4j, logback, etc.), we configure the applications and the corresponding target log files for each package. By default, the console appender is disabled if you explicitly configure it to enter the destination.
  • System.out.println always logs a message in the appender console. Therefore, it should be used only when we are sure that the console appender is configured in the registrar configuration file. Otherwise, we end up with the logs logged on the server console incorrect. Any log from within the application should go to the appropriate application log and not the server log.

Let me explain an example.

  • If we create an application called Tracker using the logging mechanism to run in the tomcat container, and we configured appenders to register applications with the 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 the tomcat server server log, which is incorrect, since the server log should only contain information about the server, and not about the application.

But if we created a console appender with a target log file, it will be correctly registered.

I hope I understand.;)

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

For the differences between the two, see Logger vs. System.out.println

+1
source share

All Articles