tags:

views:

487

answers:

5

Hi all,

I'm using the PMD plugin for eclipse and it gives me an error when using System.out.println() with the explanation:

System.(out|err).print is used, consider using a logger.

My question is - What is a Logger? How is it used to print to the screen? Why is it better?

Thanks.

+4  A: 

See this short introduction to log4j.

The issue is in using System.out to print debugging or diagnostic information. It is a bad practice because you cannot easily change log levels, turn it off, customize it, etc.

However if you are legitimately using System.out to print information to the user, then you can ignore this warning.

matt b
+2  A: 

It appears that PMD is assuming that you are calling System.out.println() for debugging purposes; stuff like "Im in ur method, executing ur codez".

If you're doing that, you're going to have a much better time writing to a logger like Log4J, as it'll have multiple streaming options than just to screen.

If, however, you're doing a console application and are calling System.out as part of that, ignore the warning.

Randolpho
+2  A: 

System.out.println is not good to use as it cannot be configured. In stead, Logger can be configured to log on various levels. It has whole lot of other features.

fastcodejava
+1  A: 

This link provides more concise information about how to use Log4j: Don't use System.out.println! It has however only one little flaw, you should rather not put the library in /jre/lib/ext, but just in the runtime classpath of your application and ship it along.

The advantage is that you can use logging levels to indicate the importance of the information, so that you can configure externally which levels to show/hide in the output (so that you don't get annoyed by the -after all- useless information), how the output should look like (e.g. include a timestamp, thread ID, classname, methodname, etc) and where the output should be written to (e.g. the console, a file, an email, etc) and in case of for example files also how they should be created (e.g. group by year, month and/or day).

There are several logger implementations like the Java SE's builtin java.util.logging.Logger, the convenienced Apache Commons Logging, the popular Apache Log4j, its predecesor Logback, etc. You can use Slf4j as an extra abstraction layer to switch between any of those loggers whenever needed.

BalusC
+2  A: 

If you are using System.out|err.println(..) to print out user-information on console in your application's main()-method, you do nothing wrong. You can get rid of the message via inserting a comment "//NOPMD".

System.out.println("Fair use of System.out.println(..).");// NOPMD

There is a "Mark as reviewed"-Option in the PMD-Violations Outline for this purpose.

Of course you can trick PMD with following code snippet:

PrintStream out=System.out;
out.println("I am fooling PMD.");  

Outside of your main()-Method use a Log-System like eg Log4j.

UPDATE:

You can also modify the PMD-Rule "SystemPrintln" to use the following XPath:

//MethodDeclaration[@MethodName!="main"]//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
] | //Initializer//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
]

This will ignore System.out.println etc in any method named 'main' in your code, but check for System.out.println in initializer code. I like this, because from my point of view, System.out.println is safe in method 'main(String args[])'. But use with caution, I have to check, where in the AST a System.out.println can occur also and have to adapt the XPath.

Michael Konietzka