PrintStreamが提供するコンストラクターを見てください。ロギング フレームワーク OutputStream を直接 PrintStream に渡し、System.out と System.err を設定して PrintStream を使用することができます。
編集:これは簡単なテストケースです:
public class StreamTest
{
public static class MyOutputStream extends FilterOutputStream
{
public MyOutputStream(OutputStream out)
{
super(out);
}
@Override
public void write(byte[] b, int off, int len) throws IOException
{
byte[] text = "MyOutputStream called: ".getBytes();
super.write(text, 0, text.length);
super.write(b, off, len);
}
}
@Test
public void test()
{
//Any OutputStream-implementation will do for PrintStream, probably
//you'll want to use the one from the logging framework
PrintStream outStream = new PrintStream(new MyOutputStream(System.out), true); //Direct to MyOutputStream, autoflush
System.setOut(outStream);
System.out.println("");
System.out.print("TEST");
System.out.println("Another test");
}
}
出力は次のとおりです。
MyOutputStream called:
MyOutputStream called: TESTMyOutputStream called: Another testMyOutputStream called:
2 行目には「空」の呼び出しがあります ( MyOutputStream が呼び出されます: -output の後に何もありません)。これは、println がまず
Another テスト文字列を write-method に渡し、次に改行を付けて再度呼び出すためです。