com.yy.httpproxy.AndroidLoggingHandler Maven / Gradle / Ivy
package com.yy.httpproxy;
import android.util.Log;
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;
public class AndroidLoggingHandler extends Handler {
public static void reset(Handler rootHandler) {
Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers();
for (Handler handler : handlers) {
rootLogger.removeHandler(handler);
}
LogManager.getLogManager().getLogger("").addHandler(rootHandler);
}
@Override
public void close() {
}
@Override
public void flush() {
}
@Override
public void publish(LogRecord record) {
if (!super.isLoggable(record))
return;
String name = record.getLoggerName();
int maxLength = 30;
String tag = name.length() > maxLength ? name.substring(name.length() - maxLength) : name;
try {
int level = getAndroidLevel(record.getLevel());
if (level <= Log.DEBUG) {
com.yy.httpproxy.util.Log.d(tag, record.getMessage());
} else if (level <= Log.WARN) {
com.yy.httpproxy.util.Log.d(tag, record.getMessage());
} else if (level <= Log.ERROR) {
com.yy.httpproxy.util.Log.e(tag, record.getMessage(), record.getThrown());
}
} catch (RuntimeException e) {
Log.e("AndroidLoggingHandler", "Error logging message.", e);
}
}
static int getAndroidLevel(Level level) {
int value = level.intValue();
if (value >= 1000) {
return Log.ERROR;
} else if (value >= 900) {
return Log.WARN;
} else if (value >= 800) {
return Log.INFO;
} else {
return Log.DEBUG;
}
}
}