Listener to prevent System.out from appearing on the screen

I did my academic project, and when creating and testing I put a lot of println () statements.

But when I had to send all the prints, they should not be displayed.

Is it possible to implement something like a listener that will be called when trying to execute System.out and prevent it from being displayed. I don’t know how possible this is, but I just want to know if it is possible or not. I know that I could use a log file or write to a file, but again his thought only occurred to me if I need to disable SOP, how can I do it.

thanks

+4
source share
5 answers

use the function System.setOut (and setErr) The following program will print only 1: (not 2)

public static void main(String[] args) throws FileNotFoundException { System.out.println("1"); System.setOut(new PrintStream(new OutputStream() { @Override public void write(int arg0) throws IOException { // TODO Auto-generated method stub } })); System.out.println("2"); } 
+9
source

The right thing is to use flags in front of printlns or, even better, use Logger (many versions are available).

However, you can redirect all System.out. Search for "redirect system.out" and you will find many examples.

+7
source

Use log4j. This will allow you to do something like:

 if (log.isLoggingEnabled()) { log.debug("this will be printed at runtime with debugging enabled"); } 

You can then configure the application to not print debug statements after sending the code (usually this is done either in the .properties file or in the XML file). using a logger is a very common practice on Java-powered websites, and there is quite a lot of documentation on log4j there, in particular. In fact, sending code with System.out lines for debugging is considered bad form in most circles. Think of a person reading log files!

The user TINY has the performance for an additional method call with the help of a registrar, but at the same time it will make the serviceability more practical. You probably would have impressed your instructor too :)

+1
source

It is generally not recommended that you write the hard drive to System.out. As a quick fix, you can change all System.out references to your own static variable in one of your classes. This at least gives you the ability to change the stream you are writing. For instance.

 public static void main(String[] args) { static public PringStream out = System.out; void someMethod() { out.println("some logging message"); } } 

You can quickly replace all uses of System.out in your code with Myclass.out. After that, you can change the output stream according to the arguments or properties of the system. For instance.

 if (Boolean.getBoolean("debug")) out = System.out; else out = new PrintStream(new OutputStream() { public void write(int data) throws IOException {} }; 

Of course, that’s all for the pants and the distribution code.

A more reliable and flexible solution is to use the api log like slf4j .

+1
source

 PrintStream realOut = System.out; try { out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); System.out.println("bla"); } finally { System.setOut(realOut); } System.out.println(out); 
0
source

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


All Articles