Question

When debugging code I tend to use System.out & System.err quite a lot. The problem is when an exception is ocurring and i use System.out the same time the outcome doesn't look very organized.

[CLIENT]:postinit: vic.rpg.gui.GuiMain.init()
java.net.ConnectException: Connection refused: connect
[CLIENT]:Destroying Network Thread...
[CLIENT]:done!
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
...
[CLIENT]:Starting Server...

It's not very severe but it makes reading logs a little bit difficult sometimes.

Was it helpful?

Solution

You can avoid this issue by calling flush each time you write to either stream. e.g.:

System.out.println ("Writing to *out*");
System.out.flush ();

If you are logging from different threads, then your logging messages will naturally interleave. If you want to cleanly separate the output by threads, I'd suggest using a logging library such as log4j.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top