I strongly discourage utilizing System.out.println() or System.err.println() strategies for printing something in manufacturing high quality code, its merely not acceptable, and you’ll be taught why you must favor to make use of Log4j over System.out.println() on this submit.
By the best way, in case you are utilizing fashionable IDE like Eclipse or Netbeans, you could get a warning whereas utilizing System.out.println() for logging functions, which is nice, in any other case, you could find yourself with a state of affairs the place some messages are going to log file whereas different going someplace else, in all probability on a file the place the console is redirected.
It is also a finest apply to make use of the Logging framework whereas coding in Java, even books like Clear Code additionally recommend studying Log4j for logging.
Why favor Log4j over System.out.println
Many Java programmers have their very own motive for utilizing a logging framework like Log4j, java.util.Logger, SL4j, and even Apache Commons logging, however all of them are higher than utilizing the System.out.println(). Although my recommendation is to favor SLF4j over Log4j for logging in Java as mentioned in that article.
Anyway, listed below are among the causes, which I feel are sufficient to cease you utilizing System.out.println() statements in manufacturing code:
1. Data segregation utilizing Log Level
2. Efficiency and Flexibility
The second level, which is said to the earlier one is that the logging framework permits you to suppress messages from one other logging degree, for instance, you possibly can run your software in DEBUG mode within the check surroundings, however on ERROR mode in a manufacturing surroundings.
This isn’t doable with System.out.println() statements which can’t be managed by any configuration or dynamically at runtime utilizing JMX or any watchdog facility.
However, you possibly can change the log degree for log4j primarily based logger with out restarting your software through the use of a watchdog thread, which displays a location for up to date log4j.xml, the configuration file for Apache Log4j. The java.util.Logger additionally permits you to change the logging degree utilizing JMX with out restarting the server.
3. Higher Log Messages
All of this info may be actually helpful whereas debugging concurrent functions the place the output from a number of threads is overlapping. You aren’t getting these amenities once you use the System.out.println() statements for logging messages.
Utilizing System.out or System.err relatively than a devoted logging facility makes it troublesome to observe the conduct of this system.
Instance 1: The primary Java program {that a} developer learns to jot down usually seems to be like this:
public class MyClass public static void predominant(String[] args) { System.out.println("whats up world"); } }
Whereas most programmers go on to be taught many nuances and subtleties about Java, a stunning quantity hold on to this primary lesson and by no means hand over on writing messages to plain output utilizing System.out.println() assertion.
Effectively, that is Okay in case you are writing small check applications nevertheless it definitely not proper should you coding on a manufacturing system that lives with thousands and thousands of customers or trades.
Logging additionally impacts efficiency in an enormous method, I’ve seen a dwell instance the place the help workforce left the manufacturing system on DEBUG degree solely to search out the latency shoots up by 500 ms and customers complaining about delayed execution.
That is all about why a Java programmer ought to use a Logging framework over System.out.println() assertion in manufacturing code. As I stated, it is completely okay in case you are writing check applications and small functions nevertheless it’s not Okay in your real-world Java software.
You may know that you may additionally use Log4j with out XML configuration or property information if that may encourage you to make use of it overprint() statements. Although, formatted output with metadata, controlling log degree at runtime are among the options you definitely do not wish to ignore.
P.S. – If you wish to be taught extra finest practices, you may as well examine my submit on 10 Java Concurrency Finest Practices, each Java developer ought to observe.