Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture javax.net.debug to file

Tags:

java

log4j

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);
            }
        };
    }
}
like image 816
Loomer Avatar asked Jan 04 '11 16:01

Loomer


1 Answers

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

like image 107
mtraut Avatar answered Sep 24 '22 00:09

mtraut