FileDocCategorySizeDatePackage
AndroidHandler.javaAPI DocAndroid 1.5 API4695Wed May 06 22:41:56 BST 2009com.android.internal.logging

AndroidHandler

public class AndroidHandler extends Handler
Implements a {@link java.util.logging.Logger} handler that writes to the Android log. The implementation is rather straightforward. The name of the logger serves as the log tag. Only the log levels need to be converted appropriately. For this purpose, the following mapping is being used:
logger level Android level
SEVERE ERROR
WARNING WARN
INFO INFO
CONFIG DEBUG
FINE, FINER, FINEST VERBOSE

Fields Summary
private static final Formatter
THE_FORMATTER
Holds the formatter for all Android log handlers.
Constructors Summary
public AndroidHandler()
Constructs a new instance of the Android log handler.


                  
      
        setFormatter(THE_FORMATTER);
    
Methods Summary
public voidclose()

        // No need to close, but must implement abstract method.
    
public voidflush()

        // No need to flush, but must implement abstract method.
    
static intgetAndroidLevel(java.util.logging.Level level)
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.

        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;
        }
    
public voidpublish(java.util.logging.LogRecord record)

        try {
            int level = getAndroidLevel(record.getLevel());
            String tag = record.getLoggerName();

            if (tag == null) {
                // Anonymous logger.
                tag = "null";    
            } else {
                // Tags must be <= 23 characters.
                int length = tag.length();
                if (length > 23) {
                    // Most loggers use the full class name. Try dropping the
                    // package.
                    int lastPeriod = tag.lastIndexOf(".");
                    if (length - lastPeriod - 1 <= 23) {
                        tag = tag.substring(lastPeriod + 1);
                    } else {
                        // Use last 23 chars.
                        tag = tag.substring(tag.length() - 23);
                    }
                }
            }

            if (!Log.isLoggable(tag, level)) {
                return;
            }

            String message = getFormatter().format(record);
            Log.println(level, tag, message);
        } catch (RuntimeException e) {
            Log.e("AndroidHandler", "Error logging message.", e);
        }