Capture javax.net.debug to file

2019-04-22 13:58发布

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

标签: java log4j
3条回答
\"骚年 ilove
2楼-- · 2019-04-22 14:34

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.

查看更多
成全新的幸福
3楼-- · 2019-04-22 14:36

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.


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

查看更多
叛逆
4楼-- · 2019-04-22 14:36

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

log4j.properties file

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

Hope this will help you.

查看更多
登录 后发表回答