📜 ⬆️ ⬇️

Redirect / duplicate system output in java

Here recently I came across a link in the internet, where it was described how to redirect system output to a file in Java, meaning System.out and System.err. With just one additional class + a few more lines of code, and we can get a good logger. Everything is so simple that there are no words, but did not know before ... We look!

Redirection to files (can be the same)

Main.java
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
System.setOut( out );
System.setErr(err);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]); //

}


* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
System.setOut( out );
System.setErr(err);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]); //

}


* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {
PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
System.setOut( out );
System.setErr(err);
System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]); //

}


* This source code was highlighted with Source Code Highlighter .


out.log
Hello!

err.log
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at systemout.Main.main (Main.java:38)


')
Redirection to files and IDE console

To redirect output to both the console and the file, we need a class that extends PrintStream:

DualStream.java
public class DualStream extends PrintStream {

PrintStream out ;

public DualStream(PrintStream out1, PrintStream out2) {
super(out1);
this . out = out2;
}

public void write( byte buf[], int off, int len) {
try {
super.write(buf, off, len);
out .write(buf, off, len);
} catch (Exception e) {
e.printStackTrace();
}
}

public void flush() {
super.flush();
out .flush();
}
}


* This source code was highlighted with Source Code Highlighter .
public class DualStream extends PrintStream {

PrintStream out ;

public DualStream(PrintStream out1, PrintStream out2) {
super(out1);
this . out = out2;
}

public void write( byte buf[], int off, int len) {
try {
super.write(buf, off, len);
out .write(buf, off, len);
} catch (Exception e) {
e.printStackTrace();
}
}

public void flush() {
super.flush();
out .flush();
}
}


* This source code was highlighted with Source Code Highlighter .
public class DualStream extends PrintStream {

PrintStream out ;

public DualStream(PrintStream out1, PrintStream out2) {
super(out1);
this . out = out2;
}

public void write( byte buf[], int off, int len) {
try {
super.write(buf, off, len);
out .write(buf, off, len);
} catch (Exception e) {
e.printStackTrace();
}
}

public void flush() {
super.flush();
out .flush();
}
}


* This source code was highlighted with Source Code Highlighter .


Here we expanded the PrintStream class by adding another field of type PrintStream, and also redefined two methods.

Main.java
public static void main( String [] args) throws FileNotFoundException {

PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream dual = new DualStream(System. out , out );
System.setOut(dual);

PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
dual= new DualStream(System.err, err);
System.setErr(dual);

System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]);

}


* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {

PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream dual = new DualStream(System. out , out );
System.setOut(dual);

PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
dual= new DualStream(System.err, err);
System.setErr(dual);

System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]);

}


* This source code was highlighted with Source Code Highlighter .
public static void main( String [] args) throws FileNotFoundException {

PrintStream out = new PrintStream( new FileOutputStream( "out.log" ));
PrintStream dual = new DualStream(System. out , out );
System.setOut(dual);

PrintStream err = new PrintStream( new FileOutputStream( "err.log" ));
dual= new DualStream(System.err, err);
System.setErr(dual);

System. out .println( "Hello!" );
String [] array = { "1" , "2" , "3" };
System. out .println(array[5]);

}


* This source code was highlighted with Source Code Highlighter .



out.log
Hello!

err.log
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at systemout.Main.main (Main.java:38)


console
Hello!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at systemout.Main.main (Main.java:38)



This is how you can get a simple logger that is suitable for various tasks.

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


All Articles