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
相关推荐
驰骋工作流6 小时前
流程时效考核的表结构:从待办时限到 WF_CH 考核结果驰骋BPM驰骋低代码低代码工作流引擎
android·低代码·rxjava
传奇开心果编程7 小时前
【Flutter踩坑排雷】第7课:巨型 build 函数坑|全部 UI 写在同一个 build,维护难、隐性异常
android·flutter·ios·移动开发·跨平台·dart·声明式ui
方白羽11 小时前
Android 双开环境底层剖析与风控检测落地
android·安全·app
weixin_5633714711 小时前
Android Studio 2024 版 Device Explorer 打开方法
android·ide·android studio
AIsoft_868914 小时前
职场音频文件怎么整理?录音APP有哪些用法
android·音视频
booboosui14 小时前
第一批个人 Agent 用户正在卸载,问题不在模型,在它住在哪儿
android
洞见前行14 小时前
点击图标到内容出现,你的 App 在这几秒里做了什么:Android 冷启动全链路优化实战
android·性能优化·android studio
凯基迪科技14 小时前
芯片UID绑定修改 加密与逆向修改思路分析
android
Liben15 小时前
Weex开发踩坑记录
android·前端·ios
救救孩子把17 小时前
结构化输出为何“格式正确、答案错误”?JSON Schema 的能力边界
android·java·json·agent·agent服务