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
相关推荐
dualven_in_csdn4 分钟前
EMQX 开启 **MySQL + password_based** 认证
android·数据库·mysql
牢七8 分钟前
RuoYi-v4.2 前缀
android
光影少年10 分钟前
开发RN项目时,如何调试iOS真机、Android真机?常见调试问题排查?
android·前端·react native·react.js·ios
十六年开源服务商34 分钟前
WordPress并发量优化实战:2026运维指南
android·运维
黄林晴1 小时前
Compose跨平台新版本来了!测试 API 全废弃,iOS 崩溃集中修复
android
Kapaseker1 小时前
Compose 响应式布局的最后一块拼图—Grid
android·kotlin
我命由我123451 小时前
Android buildSrc 模块问题:Gradle 的类 DefaultProject 被错误地尝试转换成 Apache Ant 的 Project 类
android·java·java-ee·kotlin·android jetpack·android-studio·android runtime
张风捷特烈1 小时前
GetX 之死 | 8 年从未用过,以后将不会再用
android·前端·flutter
黑牛儿1 小时前
2026 MySQL 面试 100 题: 索引 / 事务 / 锁(答案 + 原理)
android·mysql·面试
励志的小陈1 小时前
数据结构--堆(C语言实现)
android·c语言·数据结构