Sunday, May 19, 2024
HomeJavaWhy use Log4j logging vs System.out.println in Java? Reply

Why use Log4j logging vs System.out.println in Java? Reply

Printing messages to the console is an integral a part of the event, testing, and debugging of a Java program. In case you are engaged on a Server-side software, the place you cannot see what is going on on contained in the server, your solely visibility instrument is a log file; with out logs, you cannot do any debugging or see what is going on on inside your software. Although, Java has a reasonably useful System.out.println() methodology to print one thing on the console, which may also be routed to log file however not enough for a real-world Java software. 
In case you are operating a Java program in Linux or any UNIX-based system, Log4j or SLF4j or every other logging framework provides much more options, flexibility, and enchancment on message high quality, which isn’t doable utilizing the System.out.println() assertion.

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 Leve

Any logging framework together with Log4j, SL4J, logback, and java.util.logger permits you to log debugging info with a log degree, later you need to use that degree as filtering standards, I imply, you possibly can disable messages belongs to at least one explicit log degree such as you could be extra involved to see WARN messages than DEBUG messages in manufacturing.

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 won’t solely generate fewer logs but in addition improves the efficiency of your software due to much less logging, see 10 recommendations on logging in Java to be taught extra about this.

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

Through the use of a Logging framework, you possibly can produce higher outputs and metadata, which is able to assist throughout troubleshooting and debug. Most logging framework e.g. Log4j permits you to print formatted output by specifying a formatting sample through the use of PatterLayout, which may embody a timestamp, identify of the category, the Thread which is executing code, and many others.

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. 

What extra, even Joshua Bloch has additionally prompt utilizing a correctly logging facility like java.util.Logger for logging in his basic ebook Efficient Java, one of many must-read books for skilled Java programmers.

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. 

Log4j in Java application

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.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments