secret
Last active

Enable full httpclient logging using the normal java.util.logger within android

  • Download Gist
DebugLogConfig.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
 
import android.util.Log;
 
public class DebugLogConfig {
 
static DalvikLogHandler activeHandler;
 
protected static class DalvikLogHandler extends Handler {
 
private static final String LOG_TAG = "HttpClient";
 
@Override
public void close() {
// do nothing
}
 
@Override
public void flush() {
// do nothing
}
 
@Override
public void publish(LogRecord record) {
if (record.getLoggerName().startsWith("org.apache")) {
Log.d(LOG_TAG, record.getMessage());
}
}
}
 
public static void enable() {
try {
String config = "org.apache.http.impl.conn.level = FINEST\n"
+ "org.apache.http.impl.client.level = FINEST\n"
+ "org.apache.http.client.level = FINEST\n" + "org.apache.http.level = FINEST";
InputStream in = new ByteArrayInputStream(config.getBytes());
LogManager.getLogManager().readConfiguration(in);
} catch (IOException e) {
Log
.w(DebugLogConfig.class.getSimpleName(),
"Can't read configuration file for logging");
}
Logger rootLogger = LogManager.getLogManager().getLogger("");
activeHandler = new DalvikLogHandler();
activeHandler.setLevel(Level.ALL);
rootLogger.addHandler(activeHandler);
}
 
}
gistfile2.txt
1 2 3
usage:
 
DebugLogConfig.enable();

without body loggin:

public static class DebugLogConfig {

    static DalvikLogHandler activeHandler;

    protected static class DalvikLogHandler extends java.util.logging.Handler {

        private static final String LOG_TAG = "HttpClient";

        @Override
        public void close() {
            // do nothing
        }

        @Override
        public void flush() {
            // do nothing
        }

        @Override
        public void publish(LogRecord record) {
            if (record.getLoggerName().startsWith("org.apache")) {
                Log.i(LOG_TAG, record.getMessage());
            }
        }
    }

    public static void enable() {
        try {
            String config = "org.apache.http.level = FINEST\n"
                    + "org.apache.http.wire.level = SEVERE\n"   ;
            InputStream in = new ByteArrayInputStream(config.getBytes());
            LogManager.getLogManager().readConfiguration(in);
        } catch (IOException e) {
            Log
                    .w(DebugLogConfig.class.getSimpleName(),
                            "Can't read configuration file for logging");
        }
        Logger rootLogger = LogManager.getLogManager().getLogger("");
        activeHandler = new DalvikLogHandler();
        activeHandler.setLevel(Level.ALL);
        rootLogger.addHandler(activeHandler);
    }

}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.