Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable System.out for speed in Java

I'm writing a program in Java that simulates gravity, and in it I have a bunch of log statements (to System.out). My program is running really slowly, and I think the logging might be part of the reason. Is there any way to disable System.out, so that my program doesn't slow down when printing, or do I have to manually go through and comment/uncomment each one to enable/disable debug statements?

like image 314
Gavin S. Yancey Avatar asked Mar 27 '12 02:03

Gavin S. Yancey


People also ask

How do I disable system out Println in Java?

There is a setOut( PrintStream ) method in java. lang. System - if the security manager will let you, you can redirect System. out to any PrintStream - you could write to a file or make a custom PrintStream to hold the last N outputs, or just make a bit-bucket that discards all output.

How do I get out of system in Java?

You can use System. setOut() to redirect the System. out.

What is the use of system out in Java?

Java System. out. println() is used to print an argument that is passed to it.


1 Answers

Again an output stream gobbler could work, something perhaps like...

System.setOut(new PrintStream(new OutputStream() {
    @Override
    public void write(int arg0) throws IOException {

    }
}));

But a better solution is to use a formal logging utility as has already been mentioned.

like image 63
Hovercraft Full Of Eels Avatar answered Oct 12 '22 14:10

Hovercraft Full Of Eels