Setting Android Log Levels

后端 未结 2 1422
忘了有多久
忘了有多久 2021-02-08 11:54

Is it possible to set the log levels on a device that is not rooted? so I want to change the device log level somehow to \"debug\". is this something that can be done?

相关标签:
2条回答
  • 2021-02-08 12:00

    Let me suggest a tiny replacement for the standard log class (I'm the author)

    https://github.com/zserge/log

    It's backwards compatible, so you only need to modify your imports. Then you can set the minimal log level for your app via Log.level(Log.D) or Log.level(Log.W) etc, or you can disable logs using Log.useLog(false). No need to modify your existing logging code.

    Despite of its small size this logger works with both, JVM and Android, allows you to skip the "tag" parameter, simplifies logging of multiple values separated by commas or using a format string. So it's really convenient, easy to migrate to, and only adds ~4 kilobytes to your APK size.

    0 讨论(0)
  • 2021-02-08 12:06

    setprop:

    • is temporary until you reboot your device, even on rooted phones.
    • you can persist properties through reboots if you write them into local.prop which is only possible on rooted phones.
    • some properties are read-only and can only be changed if you change some init files. That might be even impossible on rooted phones.
    • each device (or firmware) can have a different set of properties. A rooted phone wouldn't have automatically more.

    Loglevels:

    • If the code that prints the log says Log.d() then it will be on "debug" level and you can't change that unless you change the code and recompile it. There is nothing that hides log messages if you execute a Log.? regardless of level.
    • the Android framework hides some log messages if you have a release build of your firmware. To show those you need to recompile your firmware as debug build. No chance to get those to show on a rooted phone either.
    • some messages are controlled by a local variable in the code like if (LOCAL_LOGV) Log.v(... - you need to change the code here to see those too.
    • some messages are controlled by Config.LOGV (= always false) see Config. No way to change the broken behaviour here either. You need to recompile.
    • some other logmessages are hidden until you enable a property:

    example

    public static final boolean DEBUG_SQL_CACHE =
    Log.isLoggable("SQLiteCompiledSql", Log.VERBOSE); 
    
    // somewhere in code
    if (SQLiteDebug.DEBUG_SQL_CACHE) {
        Log.d(TAG, "secret message!");
    }
    

    if you do adb shell setprop log.tag.SQLiteCompiledSql VERBOSE you should see those messages popping up. Log#isLoggable()

    There is no global loglevel I know of.

    0 讨论(0)
提交回复
热议问题