日志标签最多可以有 23 个字符

The logging tag can be at most 23 characters

自更新 AS 1.1 Preview 2 以来,我的所有 Log 消息下方都出现了红线

Log.d(TAG, "message");

带有消息:“日志标记最多可以包含 23 个字符..”。

除了 Android Studio 本身,我没有从根本上更新任何东西。这是一个错误吗?

这是最近的更改,在此版本中,它是一个新的 lint 检查。其中说,

检查传递给日志记录调用的标签(如果其值可以解析)最多 23 个字符长(根据日志记录 API 的要求)

有关详细信息,请阅读下面的第 3 点 link。

https://sites.google.com/a/android.com/tools/recent/androidstudio11preview2

如果您不想得到这个,请尽量减少 TAG 中的字符数,并确保它们的长度不会超过 23 个。

不,这不是错误。

来自 Android Studio's Recent Changes on 1.1 Preview 2

Checks that the tag passed to the logging calls, if its value can be resolved, is at most 23 characters long (as required by the Logging API.)

正如对最近更改的简短解释,这是由于 Log API 不允许超过 23 个字符的标签。

SLF4J Android对此有解释:

[...] the length of such tags is currently limited to 23 characters (23 = 32 - 8 for namespace prefix - 1 for C terminator)

匹配 Android's source code.

目前,唯一明确提及此异常的函数是 Log.isLoggable(),

...

Throws

IllegalArgumentException is thrown if the tag.length() > 23.

但是,根据评论,显然记录器确实在发布模式下抛出异常(在调试模式下被忽略)。

您可以按照 禁用 lint 检查,但您已收到警告。

您可以选择禁用它。

在 Android Studio 中,分析 -> 检查代码。

在 Inspection Profile 下,单击带有 3 个水平点的按钮。

以下 window 应该打开。搜索“日志”并取消选中“日志标签太长”。

更新: Android Studio 2.2,位于 Android Lint:正确性

补充@Terence 的回答

您还可以通过 gradle 在您的 build.gradle 文件中关闭特定检查:

lintOptions {
    disable 'LongLogTag'
}

或者通过使用 xml:

将 lint.xml 文件添加到您的项目
<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="LongLogTag" severity="ignore" />
</lint>

你永远不能忽视这个 lint 检查,它肯定会给你的发布版本带来意想不到的结果,因为它会抛出异常并停止执行(它不会让你的应用程序崩溃)。

我最近吸取了一个可怕的教训:在调试模式下没问题,但在发布版本上表现不同。

解释为什么会这样:

根据 AOSP source code 您可以使用任何您想要的标签登录。问题出在 Log.isLoggable.

Log.isLoggable 检查系统 属性 log.tag.<YOUR_TAG> 是否启用了您要记录的优先级。这是此机制的文档:

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.

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

低于API 26 (Oreo) 系统属性键的限制是31个字符。并且 "log.tag.".length() + 23 等于 31。如果您在 Android Oreo 下面调用 Log.isLoggable 并且标签长度超过 23 个字符,它将抛出,如 in the source code 所述。由于 Android O 此限制不再适用。

Lint 规则的存在只是为了保护您免受所有这些(通常)不必要的细节的影响。


Log.isLoggable 的文档还指出 IllegalArgumentException 不会 被抛出,因为 API 24,根据我的发现,是错误的。关注:https://issuetracker.google.com/issues/124593220

我为 node_module 库抛出此错误,抱怨单独的 node_mode 库。

我在 node_module 库的构建 gradle 文件中添加了这个 lint 选项 属性。

  android {
      lintOptions {
          abortOnError false
      }
  }

库是 aws-amplify 推送通知。

错误: Execution failed for task ':@aws-amplify/pushnotification:lint'

文件已更新: node_modules/@aws-amplify/pushnotification/android/build.gradle

解决。 2020年ver.

build.gradle(应用程序)

android {
    lintOptions {
        disable 'LongLogTag'
    } // put this. 
}