如何在 Android 上配置 java.util.logging? [英] How to configure java.util.logging on Android?

查看:18
本文介绍了如何在 Android 上配置 java.util.logging?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Android 上使用 java.util.logging.我想用 logging.properties 配置日志系统.但是我如何使用特定的配置文件告诉 Android 呢?例如,我将 logging.properties 放在应用程序的类路径根目录中.Android 如何知道 logging.properties 的位置.

I want to use java.util.logging on Android. I want to configure the logging system with logging.properties. But how can I tell Android using the specific configure file? For example, I placed the logging.properties in the classpath root of the application. How Android knows the location of logging.properties.

谢谢

推荐答案

这是我的一个项目的常见问题解答,希望更多人能在这里找到这个:java.util.logging 在 Android 上工作正常.请不要在您的代码中使用任何其他内容,日志框架就像 Java 世界中的害虫.

This is now an FAQ for one of my projects, hopefully more people will find this here: java.util.logging works fine on Android. Please don't use anything else in your code, logging frameworks are like a pest in the Java world.

坏的是 Android 附带的默认日志处理程序,它会忽略级别比 INFO 更精细的任何日志消息.您看不到 DEBUG 等消息.

What is broken is the default logging handler shipped with Android, it ignores any log messages with level finer than INFO. You don't see DEBUG etc. messages.

原因是AndroidHandler.java中对Log.isLoggable()的调用:

The reason is the call to Log.isLoggable() in AndroidHandler.java:

https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/logging/AndroidHandler.java

解决方法如下:

import android.util.Log;
import java.util.logging.*;

/**
 * Make JUL work on Android.
 */
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);
        }
        rootLogger.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());
            Log.println(level, tag, record.getMessage());
            if (record.getThrown() != null) {
                Log.println(level, tag, Log.getStackTraceString(record.getThrown()));
            }
        } catch (RuntimeException e) {
            Log.e("AndroidLoggingHandler", "Error logging message.", e);
        }
    }

    static int getAndroidLevel(Level level) {
        int value = level.intValue();

        if (value >= Level.SEVERE.intValue()) {
            return Log.ERROR;
        } else if (value >= Level.WARNING.intValue()) {
            return Log.WARN;
        } else if (value >= Level.INFO.intValue()) {
            return Log.INFO;
        } else {
            return Log.DEBUG;
        }
    }
}

在应用程序的主要活动/初始化代码中:

In the main activity/initialization code of your application:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidLoggingHandler.reset(new AndroidLoggingHandler());
    java.util.logging.Logger.getLogger("my.category").setLevel(Level.FINEST);
...

TL;DR:是的,您可以使用一些魔法属性或 adb shell 命令,甚至了解愚蠢的内置日志处理程序的 DalvikLogging.loggerNameToTag 如何转换标签的类别名称(您必须为那些魔法属性和 shell 命令做这些),但为什么要麻烦呢?伐木还不够痛苦吗?

TL;DR: Yes, you could use some magic properties, or adb shell command, or even learn how the stupid built-in logging handler's DalvikLogging.loggerNameToTag converts category names to tags (which you would have to do for those magic properties and shell commands), but why bother? Isn't logging painful enough?

这篇关于如何在 Android 上配置 java.util.logging?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆