Javaでのシステム出力のリダイレクト/複製

それから最近、インターネット上のリンクに出くわしました。これは、システム出力をJavaのファイル、つまりSystem.outとSystem.errにリダイレクトする方法を説明しています。 クラスを1つ追加するだけでコードを数行追加するだけで、優れたロガーを取得できます。 すべてが非常に単純なので言葉はありませんが、以前は知りませんでした...私たちは見ます!

ファイルへのリダイレクト (同じ場合もあります

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
こんにちは!

err.log
スレッド「メイン」の例外java.lang.ArrayIndexOutOfBoundsException:5
systemout.Main.main(Main.java:38)で



ファイルおよびIDEコンソールへのリダイレクト

出力をコンソールとファイルの両方にリダイレクトするには、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 .


ここで、別のPrintStream型フィールドを追加してPrintStreamクラスを拡張し、2つのメソッドを再定義しました。

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
こんにちは!

err.log
スレッド「メイン」の例外java.lang.ArrayIndexOutOfBoundsException:5
systemout.Main.main(Main.java:38)で


コンソール
こんにちは!
スレッド「メイン」の例外java.lang.ArrayIndexOutOfBoundsException:5
systemout.Main.main(Main.java:38)で



これにより、さまざまなタスクに適したシンプルなロガーを取得できます。

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


All Articles