Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is System.out.println bad? [closed]

Tags:

pmd

When i run my PMD plugin they say System.out.println has been used. why is System.out.println bad to use and is it a defect when using PMD plugin? and what is the alternative way of over coming this?

like image 206
Hash Avatar asked Dec 01 '22 02:12

Hash


2 Answers

A logger can be turned ON/OFF using a configuration but System.out.println cannot be. Importantly loggers provide different levels of logging and again can be controlled through a configuration file.

Also using loggers you can configure rotation, purging etc but cannot do the same for sysout. This is useful especially in a production environment, executing a lot of code, and generating a lot of logging.

like image 161
Juned Ahsan Avatar answered Dec 12 '22 02:12

Juned Ahsan


System.out.println() is considered bad practice for Logging.
Because of

  • No ability to turn it (ON/OFF)
  • No ability to set output levels (TRACE, DEBUG, INFO, WARN, ERROR),
    without having to recompile your code

Another disadvantage is that the standard output of a program can be redirected, for instance and it's not always clear where the output is actually going, for instance if you do this:

java SomeClass > someFile

In this case the use of a logging API will help you.

But there are situations where you genuinely want to print something to the standard output too, for those occasions there is java.io.Console, which cannot be redirected, so if you're running a command line java program, it gives you confidence that the users are seeing the messages intended to them.

like image 41
chaos Avatar answered Dec 12 '22 02:12

chaos