23

I looked up the api about the logger class(here) and I was looking at the Logger.info method. 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.println when they can print out the same thing.

In Logger.

Logger.info("Hello")

Output:

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

In System.out.println

`System.out.println("Hello")

Output: Hello

RedRocket
  • 1,525
  • 4
  • 22
  • 38
  • 5
    Logger can be formatted, you can control the output (based on level), you can output to different locations (file, stdout, database, etc) – MadProgrammer Aug 07 '15 at 03:51
  • 1
    possible duplicate of [Logger vs. System.out.println](http://stackoverflow.com/questions/2750346/logger-vs-system-out-println) – TryinHard Aug 07 '15 at 03:54
  • 1
    possible duplicate of [log4j vs. System.out.println - logger advantages?](http://stackoverflow.com/questions/2727500/log4j-vs-system-out-println-logger-advantages) – Amit Bhati Aug 07 '15 at 03:56

4 Answers4

20

Usually, because a Logger can 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 logger would not display debug or info messages. It can include information such as the class that is writing, a line number, and a date and time (of the message).

Evgenij Reznik
  • 16,046
  • 33
  • 87
  • 157
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
  • Is there a performance difference? Because I see a lag between action taken on the browser and `syso` came late in console. – paul May 31 '19 at 07:29
15

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.

Nathan Hughes
  • 85,411
  • 19
  • 161
  • 250
7

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 project also you can find the Error or Exception if you have given the logger in catch{...} block.

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

Community
  • 1
  • 1
programmer
  • 472
  • 4
  • 5
3
  • 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.

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.

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

Hope I was clear. ;)

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

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

Community
  • 1
  • 1
Vero J
  • 231
  • 2
  • 6