Does Log.isLoggable returns wrong values?

前端 未结 4 1097
一向
一向 2020-12-20 12:06

When I was writing a log wrapper for my android application I noticed a strange behavior of androids Log.isLoggable method. Executing following code:

final S         


        
相关标签:
4条回答
  • 2020-12-20 12:38

    If you read the information on Log.isLoggable(), you'll notice that the default logging level is INFO. Anything less than that (DEBUG and VERBOSE) will cause this method to return false. This is why your resulting output shows these two as false.

    All Log.* calls are logged to the logcat. Calling Log.isLoggable() is simply a way for you to tune logging. It's not required. Typically, you'll call Log.isLoggable() prior to the actual Log.* call to determine whether to log it or not.

    You can tune your logging per TAG if you choose, either with a prop file or via adb. The nice thing about this is you can dynamically turn logging up/down/off for each individual TAG in your app without the need for manually commenting out Log lines, or implementing your own checks.

    0 讨论(0)
  • 2020-12-20 12:38

    LOG will always be print in the logcat no matter what Log.isloggable() return.

    0 讨论(0)
  • 2020-12-20 12:56

    You should use

    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "verbose is active: " + Log.isLoggable(TAG, Log.VERBOSE));
    }
    
    0 讨论(0)
  • 2020-12-20 12:57

    All log levels are written to logcat regardless of what the current log level is. The isLogabble() method can be used as an optimization for your apps to prevent sending unneeded log statements to logcat. You can also use the adb logcat command to filter a subset of the logging levels even if the logging is set to verbose (see https://developer.android.com/studio/debug/am-logcat.html).

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