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?
-
possible duplicate of [Logger vs. System.out.println](http://stackoverflow.com/questions/2750346/logger-vs-system-out-println) – jontro Oct 29 '13 at 11:04
-
4How is it a defect? You need to be a little more specific. – Dennis Meng Oct 30 '13 at 06:09
-
It's a defect because you have a rule enabled that checks for `System.out.println()`. You can suppress the warnings in the places where you think you need output using the methods described on the following page: http://pmd.sourceforge.net/pmd-5.0.5/suppressing.html. – Mad Physicist Nov 27 '13 at 05:05
5 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.

- 67,789
- 12
- 98
- 136
-
1What about CLI apps? The usually output to stdout. That is the normal outout of those type of apps. – Tulains Córdova Jul 03 '17 at 18:22
-
@TulainsCórdova In CLI app u can either use macros or simple if/else to decide log or not to log according to a runtime param – Juned Ahsan Jul 04 '17 at 11:26
-
1I mean, if `System.out.println()` is outlawed, what would be the mean for CLI apps to write to the screen? – Tulains Córdova Jul 04 '17 at 11:49
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.

- 17,392
- 11
- 61
- 88

- 641
- 10
- 21
-
What about the normal output of a a CLI app? Those app usuarlly write to stdout and that is not considered logging. – Tulains Córdova Jul 03 '17 at 18:23
-
For that, use System.console(), as it allows you to figure out if you're in a terminal or not, as well as it's more fitting to be used by an user. – chaos Jul 19 '17 at 12:41
-
Ran `System.console().printf("Hello %s", "world");` in Eclipse and I get NullPointerException. Doc says _"If no console device is available then an invocation of that method will return null. "_ – Tulains Córdova Jul 19 '17 at 13:08
-
That is considered a bug in Eclipse: https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429 – chaos Jul 25 '17 at 14:07
System.out.println..
are usually intended for debugging purposes and can remain in the codebase even in production code. By using a logger one can enable/disable this behaviour at will (and by priority) and avoid clogging the Standard out log.
(from SourceMeter Java user guide under "Java Logging Rules")
Import needed (.jar)
Example:
import org.apache.log4j.Logger;
class Foo{
private static final Logger LOG = Logger.getLogger(Foo.class);
public void testA () {
System.out.println("Entering test");
// Better use this
LOG.info("Entering test");
}
}
-
17wow, you are asking a question and after some seconds answer to it? good – Admit Oct 29 '13 at 11:08
-
3Why you opened a bounty to the question when you have the accepted answer? Surprising ! – Rahul Tripathi Nov 24 '13 at 18:14
-
Wrap the LOG.info("xyz") line with if(LOG.isInfoEnabled()) { ... } so that any string building/appending within the LOG.info call is not processed if the info logging level is not enabled. – Jason Nov 26 '13 at 06:11
-
So is there a way to boost rep by doing this? I only ask because I think some of the other answers are better than yours. – Mad Physicist Nov 26 '13 at 16:14
-
-
@Admit It is just fine to ask and answer at exactly the same time. There's even an interface for it. But the question must still meet quality standards. – Andrew Barber Nov 27 '13 at 04:44
-
-
Printing excessive amounts of into to System.out can become a performance bottleneck because it is synchronized, code from PrintStream:
public void println(float x) {
synchronized (this) {
print(x);
newLine();
}
}

- 9,478
- 4
- 33
- 47
This is because, PMD defines a java logging rule named SystemPrintln
which detects System.out.println
in the code and consider as a defect.
Since: PMD 2.1
System.(out|err).print is used, consider using a logger.
This rule is defined by the following XPath expression:
//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
]
And you should be able to modify above XPath expression to override the behavior( I am not sure though) Below link should give you more insight,
http://pmd.sourceforge.net/pmd-4.2.6/rules/logging-java.html

- 3,621
- 3
- 27
- 46