Created
March 15, 2015 12:59
-
-
Save blueskyfish/11b1f6c93b6a12042374 to your computer and use it in GitHub Desktop.
Add the java logging to the Android platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kirchnerei.httpclient; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.util.Formatter; | |
import java.util.logging.Level; | |
import java.util.logging.LogRecord; | |
import java.util.logging.Logger; | |
public class MyHandlerHandler extends Handler implements DalvikLogHandler { | |
/** | |
* Holds the formatter for all Android log handlers. | |
*/ | |
private static final Formatter THE_FORMATTER = new Formatter() { | |
@Override | |
public String format(LogRecord r) { | |
Throwable thrown = r.getThrown(); | |
if (thrown != null) { | |
StringWriter sw = new StringWriter(); | |
PrintWriter pw = new FastPrintWriter(sw, false, 256); | |
sw.write(r.getMessage()); | |
sw.write("\n"); | |
thrown.printStackTrace(pw); | |
pw.flush(); | |
return sw.toString(); | |
} else { | |
return r.getMessage(); | |
} | |
} | |
}; | |
/** | |
* Constructs a new instance of the Android log handler. | |
*/ | |
public AndroidHandler() { | |
setFormatter(THE_FORMATTER); | |
} | |
@Override | |
public void close() { | |
// No need to close, but must implement abstract method. | |
} | |
@Override | |
public void flush() { | |
// No need to flush, but must implement abstract method. | |
} | |
@Override | |
public void publish(LogRecord record) { | |
int level = getAndroidLevel(record.getLevel()); | |
String tag = DalvikLogging.loggerNameToTag(record.getLoggerName()); | |
// if (!Log.isLoggable(tag, level)) { | |
// return; | |
//} | |
try { | |
String message = getFormatter().format(record); | |
Log.println(level, tag, message); | |
} catch (RuntimeException e) { | |
Log.e("AndroidHandler", "Error logging message.", e); | |
} | |
} | |
public void publish(Logger source, String tag, Level level, String message) { | |
// TODO: avoid ducking into native 2x; we aren't saving any formatter calls | |
int priority = getAndroidLevel(level); | |
try { | |
Log.println(priority, tag, message); | |
} catch (RuntimeException e) { | |
Log.e("AndroidHandler", "Error logging message.", e); | |
} | |
} | |
/** | |
* Converts a {@link java.util.logging.Logger} logging level into an Android one. | |
* | |
* @param level The {@link java.util.logging.Logger} logging level. | |
* | |
* @return The resulting Android logging level. | |
*/ | |
static int getAndroidLevel(Level level) { | |
int value = level.intValue(); | |
if (value >= 1000) { // SEVERE | |
return Log.ERROR; | |
} else if (value >= 900) { // WARNING | |
return Log.WARN; | |
} else if (value >= 800) { // INFO | |
return Log.INFO; | |
} else { | |
return Log.DEBUG; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment