Skip to content

Instantly share code, notes, and snippets.

@charroch
Created April 28, 2010 15:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save charroch/cf23c4e184228a132390 to your computer and use it in GitHub Desktop.
Save charroch/cf23c4e184228a132390 to your computer and use it in GitHub Desktop.
Enable full httpclient logging using the normal java.util.logger within android
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);
}
}
usage:
DebugLogConfig.enable();
@charroch
Copy link
Author

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

}

@faizalumer
Copy link

Shouldn't DebugLogConfig be a static class if the usage is DebugLogConfig.enable() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment