Danger System.out.println in Java EE Application

When I started learning Java, I was told not to do System.out.println in a Java EE application. However, I really do not know what is the reason for not doing this.

I fully understand that if we really need to print important information, it must be logged using the logging framework.

What I really want to ask here: is there a real danger that System.out.println can do? Does this cause a performance problem?

+6
source share
3 answers

This is really a performance issue. If you select the JDK source code for System.out , you will eventually come across a synchronized block in the output stream.

This means that if you place enough println calls in your source code, the entire code base will work efficiently single-threaded because all threads are waiting for synchronization locks.

There are some statistics here, a single println call will usually not scan your entire application. The more println calls in your code, the more likely two or more threads will have to wait for each other.

+7
source

In any Java EE application, there are probably several applications running in the same JVM. There is only one System.out . If several applications try to write to System.out at the same time, this can cause an output conflict and can theoretically affect performance.

In addition, although this is not necessarily related to performance, it makes the output very dirty and difficult to read, especially if several applications write the output at the same time, without any indication as to which application the output is going to. (The same is true for multiple simultaneous requests within the same application.)

As you mentioned, using the right logging structure will fix both of these problems.

+3
source

Another point, regardless of what is written in sysout, will be at run time, you will not be able to control it, it will always print. Using the Logging framework, you can enable / disable recording.

0
source

Source: https://habr.com/ru/post/903181/


All Articles