将javax.net.debug捕获到文件 [英] Capture javax.net.debug to file

查看:91
本文介绍了将javax.net.debug捕获到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将javax.net.debug =创建的所有输出保存到文件中.我正在使用log4j,并尝试创建一个日志记录代理,如下面的代码示例所示;但是,它没有获取信息.我不确定javax.net.debug的打印位置.我试图用这种方式捕获system.out和system.err,但是都没有用.感谢您的帮助.

I need to save the javax.net.debug=all output that is created to a file. I'm using log4j and I tried creating a logging proxy as in the code example below; however, it is not picking up the info. I am not sure where the javax.net.debug is being printed to. I tried capturing system.out and system.err this way but neither worked. Thanks for your help.

public class StdOutErrLog {

    private static final Logger logger = Logger.getLogger(StdOutErrLog.class);

    public static void tieSystemOutAndErrToLog() {
        System.setOut(createLoggingProxy(System.out));
        System.setErr(createLoggingProxy(System.err));
    }

    public static PrintStream createLoggingProxy(final PrintStream realPrintStream) {
        return new PrintStream(realPrintStream) {
            public void print(final String string) {
                realPrintStream.print(string);
                logger.info(string);
            }
        };
    }
}

推荐答案

也许子系统会复制其值,并且切换时为时已晚.尝试先在您的主机中进行此操作.

Maybe the subsystem makes its copy of the values and you are too late when switching. Try doing this first in your main.

编辑

好的-我完全想念你的成语了.我认为您不应该使用此内部类.您应该在OutputStream上定义一个PrintStream实例,该实例在每个"\ n"上创建一个新的日志条目.现在,您执行此操作的方法会丢失很多打印"您的实例的可能性.

OK - i missed completely your idiom. I think you should not use this inner class. You should define a PrintStream instance on an OutputStream that creates a new log entry upon every "\n". The way you do it now misses a lot of possibilities to "print around" your instance.



package de.mit.stackoverflow;

import java.io.IOException;
import java.io.OutputStream;

public class LogOutputStream extends OutputStream {

    private StringBuilder sb = new StringBuilder();

    @Override
    public void write(int b) throws IOException {
        if (b == '\n') {
            log(sb.toString());
            sb.setLength(0);
        } else {
            sb.append((char) b);
        }
    }

}


然后做

    OutputStream os = new LogOutputStream();
    PrintStream ps = new PrintStream(os);
    System.setOut(ps);

您可能仍想包含对前一个流的引用-保留为练习:-)

You maybe still want to include a reference to the previous stream - left as excercise :-)

这篇关于将javax.net.debug捕获到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆