java - log4j vs. System.out.println - logger advantages? -
i'm using log4j first time in project. fellow programmer told me using system.out.println
considered bad style , log4j standard logging matters nowadays.
we lots of junit testing - system.out
stuff turns out harder test.
therefore began utilizing log4j console controller class, that's handling command-line parameters.
// log4j logger config org.apache.log4j.basicconfigurator.configure(); logger logger = loggerfactory.getlogger(console.class); category cat = category.getroot();
seems work:
logger.debug("string");
produces:
1 [main] debug project.prototype.controller.console - string
i got two questions regarding this:
- from basic understanding using logger should provide me comfortable options write logfile timestamps - instead of spamming console - if debug mode enabled @ logger?
- why system.out.println harder test? searched stackoverflow , found testing recipe. wonder kind of advantage using log4j.
the logger gives ability define different levels of importance of logged messages , ability use different sink output - console, file, etc.
also it's easy enable or disable type of message when using logger - example don't want see every debug message in production.
i don't think using loggers offers significant advantages in unit tests, i'd prefer there anyways. in unit tests asserts primary concern.
btw should consider using commons logging or slf4j log framework facade - it's bad style tie code specific logging framework. common logging , slf4j make easy switch logging frameworks if choose to.
Comments
Post a Comment