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?
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.
System.out.println()
is considered bad practice for Logging.
Because of
ON
/OFF
)TRACE
, DEBUG
, INFO
, WARN
, ERROR
),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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With