log4j vs. System.out.println - logger advantages?

Posted by wishi_ on Stack Overflow See other posts from Stack Overflow or by wishi_
Published on 2010-04-28T07:24:41Z Indexed on 2010/04/28 7:33 UTC
Read the original article Hit count: 394

Filed under:
|
|
|
|

Hi!

I'm newly using log4j in a project. A fellow programmer told me that using System.out.println is considered bas style and that log4j is something like standard for logging matters nowadays.

We do lots of JUnit testing - System.out stuff turns out to be harder to test.

Therefore I began utilizing log4j for a Console controller class, that's just handling commandline parameters.

// some logger config 
org.apache.log4j.BasicConfigurator.configure();
Logger logger = LoggerFactory.getLogger(Console.class);
Category cat = Category.getRoot(); 

Seems to work:

logger.debug("String");

Produces:

1 [main] DEBUG project.prototype.controller.Console  - String

I got two questions regarding this:

  1. From my basic understanding using this logger should provide me comfortable options to write a logfile with timestamps - instead of spamming the console - if debug mode is enabled at the logger?
  2. Why is System.out.println harder to test? I searched stackoverflow and found a testing recipe. So I wonder what kind of advantage I really get by using log4j.

© Stack Overflow or respective owner

Related posts about java

Related posts about logging