java 将 javax.net.debug 捕获到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4595950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:07:59  来源:igfitidea点击:

Capture javax.net.debug to file

javalog4j

提问by Loomer

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.

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

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);
            }
        };
    }
}

采纳答案by mtraut

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

也许子系统会复制这些值,而您在切换时为时已晚。尝试首先在您的主要内容中执行此操作。

EDIT

编辑

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.

好的 - 我完全错过了你的成语。我认为你不应该使用这个内部类。您应该在 OutputStream 上定义一个 PrintStream 实例,该实例在每个“\n”上创建一个新的日志条目。你现在这样做的方式错过了很多“打印”你的实例的可能性。


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);
        }
    }

}


and then do

然后做

    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 :-)

您可能仍然希望包含对上一个流的引用 - 留作练习 :-)

回答by Matt Solnit

This is a long shot, but it's possible that overriding print(String)is not enough. For example, there is also print(Object), etc., not to mention the various append()and format()methods.

这是一个远景,但覆盖可能print(String)还不够。比如还有print(Object),等等,更不用说各种append()format()方法了。

回答by Mat B.

You need to have log4j.properties file in class-path (/WEB-INF/classes/) with following content:

您需要在类路径 (/WEB-INF/classes/) 中有 log4j.properties 文件,其中包含以下内容:

log4j.properties file

log4j.properties 文件

datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
roll.pattern.hourly=.MM-dd-yyyy.HH
roll.pattern.daily=.MM-dd-yyyy

log4j.rootLogger=INFO, Console
log4j.logger=INFO, Console

log4j.appender.Console=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Console.DatePattern=${roll.pattern.daily}
log4j.appender.Console.file=${catalina.home}/logs/Console.log
log4j.appender.Console.MaxFileSize=800KB
log4j.appender.Console.MaxBackupIndex=5
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %m%n

log4j.appender.custom=org.apache.log4j.DailyRollingFileAppender
log4j.appender.custom.DatePattern=${roll.pattern.daily}
log4j.appender.custom.File=${catalina.home}/logs/custom.log
log4j.appender.custom.MaxFileSize=800KB
log4j.appender.custom.MaxBackupIndex=5
log4j.appender.custom.layout=org.apache.log4j.PatternLayout
log4j.appender.custom.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %m%n

log4j.logger.net.javax=DEBUG, custom

This will write your log entries into tomcat home directory /logs/custom.log

这会将您的日志条目写入 tomcat 主目录 /logs/custom.log

Hope this will help you.

希望这会帮助你。