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
相关推荐
JohnnyDeng941 小时前
【Android】Android 包体积优化:R8/ProGuard 深度配置全攻略
android·性能优化·kotlin·jetpack
故渊at1 小时前
第九板块:Android 多媒体体系 | 第二十四篇:Camera Service 与 HAL3 成像流水线
android·camera·多媒体体系·hal3
Jinkxs5 小时前
Python基础 - 初识内置函数 Python自带的便捷工具
android·java·python
私人珍藏库5 小时前
【Android】VLLO-韩国热门手机剪辑APP
android·app·工具·软件·多功能
Cloud_Shy6186 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第六章 Item 40 - 43)
android·开发语言·人工智能·笔记·python·学习方法
AFinalStone6 小时前
Android12 U盘插拔链路源码全解析(五):Framework层(下) StorageManagerService
android·frameworks
林九生8 小时前
【实用技巧】MySQL 绿色版一键路径更新脚本详解 —— update_path.bat 深度解析
android·数据库·mysql
故渊at9 小时前
第十三板块:Android 综合架构与未来演进 | 第三十一篇:Android 架构演进与 Fuchsia OS 的挑战
android·架构·宏内核·微内核·fuchsia·ipc 性能博弈
aqi009 小时前
一文速览 HarmonyOS 6.1.1 推出的十个新特性
android·华为·harmonyos·鸿蒙·harmony
matrixmind19 小时前
aiomysql:异步场景下的 MySQL 驱动
android·数据库·mysql·其他