Android isLoggable定制属于自己的log

Android原生自带的 android.util.Log,其中有一个 isLoggable 方法的运用

java 复制代码
    /** 
     * Checks to see whether or not a log for the specified tag is loggable at the specified level.
     *
     *  The default level of any tag is set to INFO. This means that any level above and including
     *  INFO will be logged. Before you make any calls to a logging method you should check to see
     *  if your tag should be logged. You can change the default level by setting a system property:
     *      'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>'
     *  Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
     *  turn off all logging for your tag. You can also create a local.prop file that with the
     *  following in it:
     *      'log.tag.<YOUR_LOG_TAG>=<LEVEL>'
     *  and place that in /data/local.prop.
     *
     * @param tag The tag to check.
     * @param level The level to check.
     * @return Whether or not that this is allowed to be logged.
     * @throws IllegalArgumentException is thrown if the tag.length() > 23
     *         for Nougat (7.0) releases (API <= 23) and prior, there is no
     *         tag limit of concern after this API level.
     */
    public static native boolean isLoggable(String tag, int level);

使用方法

java 复制代码
private static final String TAG = "MY_TAG"; // 定义自己的日志TAG
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); // 日志开关

void testLog(){
    // 通过开关日志输出
    if (DEBUG) {
        Log.d(TAG, "测试日志输出");
    }
}

需要打开日志开关的时候

复制代码
# 打开日志输出
adb shell setprop log.tag.MY_TAG DEBUG

# 关闭日志输出,这里 INFO > DEBUG,会导致isLoggable返回false
adb shell setprop log.tag.MY_TAG INFO
相关推荐
.Bryce.6 小时前
在智能体中使用A2A工具:让大模型自己决定“该找谁帮忙“
android·java·服务器·redis·后端·agent·a2a
方舟小孩8 小时前
开源办公软件海豹办公 1.9.15 实战 从模板异常到 Android WebView 资源回归
android
用户2181697049308 小时前
Kotlin 语法四 协程
android
出师牛8 小时前
Android - 再探Handler
android
高晶8 小时前
Android 物联网组网实战:NFC 从零到落地
android·java
stevenzqzq8 小时前
android 维度和渠道
android
古法安卓8 小时前
Android-SystemUI ANR协程相关问题排查
android·java·android studio
xiaogai_gai9 小时前
生产汇报单查询同步实战:从金蝶云星空拉取到轻易云数据集成平台的工程化做法
android
释厄62310 小时前
错误无误公理——错误正确本同源都迭代定律——逆转有价值
android·开发语言·kotlin
wno70410 小时前
MYSQL 常用合并函数
android·数据库·mysql