System.setOut 重定向 memcached 的输出

调用 memcached 的方法

pool = SockIOPool.getInstance(poolName); 

此代码并未抛出异常, 而是在后台打印了错误信息, 估计是 使用了 System.setOut。

进行捕捉也捕捉不到任何东西。。

怎么办呢?

System.setOut 重置 out 解决问题。但是, 怎么在System.setOut, 设置回来呢?

事先

        PrintStream err;
        PrintStream out = null;
        PrintStream www = null;
        PrintStream old = System.out;
        try {
            String fileName = "MemcachedUtil.ERR.txt";
            String fileName2 = "MemcachedUtil.OUT.txt";
            String fileName3 = "MemcachedUtil.www.txt";
            err = new PrintStream(fileName);
            out = new PrintStream(fileName2);
            www = new PrintStream(fileName3);
//            System.setErr(err );
            System.setOut(out );
            
//            System.out.println( 1/0 );
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        

然后再 System.setOut( System.out ); —— 可是这样就失效了,

 System.setOut(www) 则是可以的

System.setOut( old ); 也可以的

System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));  都是可以的!

看来要研究下里面的源码, 使用了很多开源的技术框架, 却不懂底层原理, 很多不懂, 怎么行? 

原文地址:https://www.cnblogs.com/FlyAway2013/p/5535448.html