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
相关推荐
私人珍藏库5 小时前
【Android】Soul v5.86.0 内置模块版
android·app·工具·软件·多功能
千里马学框架6 小时前
aosp新增窗口层级 Type 完整实现方案(有源码)-wms需求和面试题
android·智能手机·架构·wms·aaos·车机
峥嵘life11 小时前
Android 蓝牙设备连接广播详解-2026
android·python·学习
MusingByte14 小时前
别再裸用 Claude Code 了!安卓开发者必装 13 个官方推荐插件,效率翻 3 倍省 70% token
android
_李小白14 小时前
【android opencv学习笔记】Day 29: 滤波算法之Sobel 边缘检测
android·opencv·学习
Dxy123931021615 小时前
Python 操作 MySQL 事务:从入门到避坑
android·python·mysql
峥嵘life17 小时前
Android getprop 属性限制详解:User 版本属性获取问题分析
android·开发语言·python·学习
一航jason18 小时前
Speed Tools:一套低侵入的 Android 插件化 + 动态换肤 + 字体切换框架
android·插件化·组件化·换肤
李斯维19 小时前
Jetpack 可观察数据容器 LiveData 的入门与基础使用
android·android jetpack
问心无愧051319 小时前
ctf show web入门261
android·前端·笔记