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

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.


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

Tags:

Java

Logging