The logging tag can be at most 23 characters

后端 未结 8 1519
-上瘾入骨i
-上瘾入骨i 2020-12-13 12:46

Since update AS 1.1 Preview 2, I\'m getting red lines under all my Log messages

Log.d(TAG, \"message\");

With message: \"

相关标签:
8条回答
  • 2020-12-13 13:27

    You can never ignore this lint check, it definitely could bring unexpected results on your release version since it throws exceptions and stops executing (it would not crash your app).

    I have had a terrible lesson learned recently: it's OK on debug mode, but behave differently on release version.

    0 讨论(0)
  • 2020-12-13 13:27

    To explain why this happens:

    According to AOSP source code you can log with any tag you want. The problem is in Log.isLoggable.

    Log.isLoggable checks the system property log.tag.<YOUR_TAG> if the priority you want to log is enabled. Here's documentation of this mechanism:

    public static boolean isLoggable (String tag, int level)

    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. ' 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.=' and place that in /data/local.prop.

    Source: https://developer.android.com/reference/android/util/Log#isLoggable(java.lang.String,%20int)

    Below API 26 (Oreo) the limit of system property keys was 31 characters. And "log.tag.".length() + 23 equals 31. If you call Log.isLoggable below Android Oreo with a tag longer than 23 characters it will throw, as described in the source code. Since Android O this limit no longer applies.

    The Lint rule exists just to shield you from all these (typically) unnecessary details.


    The documentation for Log.isLoggable also states the IllegalArgumentException will not be thrown since API 24, which according to my findings, is wrong. Follow: https://issuetracker.google.com/issues/124593220

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