【问题标题】:Log.d() or Log.e() in android prints unwanted extra textsandroid 中的 Log.d() 或 Log.e() 打印不需要的额外文本
【发布时间】:2016-10-01 08:53:12
【问题描述】:

当我在 Android 应用程序中简单地记录一条消息时,Android 监视器会打印太多多余的不需要的垃圾文本,例如: 代码:

Log.d(TAG, "From: " + "34369257099");
Log.d(TAG, "Message Notification Body: " + "Hello");

android 监视器中的输出:

10-01 14:15:33.262 18193-23771/com.xxxxx.xxxxxx D/MyFirebaseMsgService: From: 34369257099
10-01 14:15:33.262 18193-23771/com.xxxxx.xxxxxx D/MyFirebaseMsgService: Message Notification Body: Hello

[ 10-01 14:15:33.490 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9871) p->msg.arg1(0) OPEN


[ 10-01 14:15:33.490 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10206

[ 10-01 14:15:33.527 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:33.686 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9872) p->msg.arg1(0) OPEN


[ 10-01 14:15:33.686 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/18193/stat

[ 10-01 14:15:33.701 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:33.723 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9873) p->msg.arg1(0) OPEN


[ 10-01 14:15:33.723 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/stat

[ 10-01 14:15:33.740 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:34.051 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9874) p->msg.arg1(0) OPEN


[ 10-01 14:15:34.051 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10206

[ 10-01 14:15:34.086 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:34.258 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9875) p->msg.arg1(0) OPEN


[ 10-01 14:15:34.258 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/18193/stat

[ 10-01 14:15:34.274 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:34.294 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9876) p->msg.arg1(0) OPEN


[ 10-01 14:15:34.294 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/stat

[ 10-01 14:15:34.315 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:34.613 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9877) p->msg.arg1(0) OPEN


[ 10-01 14:15:34.613 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10206

[ 10-01 14:15:34.648 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:34.831 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9878) p->msg.arg1(0) OPEN


[ 10-01 14:15:34.831 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/18193/stat

[ 10-01 14:15:34.849 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:34.867 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9879) p->msg.arg1(0) OPEN


[ 10-01 14:15:34.867 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/stat

[ 10-01 14:15:34.885 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:35.176 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9880) p->msg.arg1(0) OPEN


[ 10-01 14:15:35.176 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10206

[ 10-01 14:15:35.210 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:35.404 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9881) p->msg.arg1(0) OPEN


[ 10-01 14:15:35.404 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/18193/stat

[ 10-01 14:15:35.421 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:35.439 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9882) p->msg.arg1(0) OPEN


[ 10-01 14:15:35.439 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/stat

[ 10-01 14:15:35.457 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:35.738 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9883) p->msg.arg1(0) OPEN


[ 10-01 14:15:35.738 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10206

[ 10-01 14:15:35.764 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:35.974 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9884) p->msg.arg1(0) OPEN


[ 10-01 14:15:35.974 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/18193/stat

[ 10-01 14:15:35.990 19060:19060 E/         ]
[adb] cuurent command is A_CLSE 


[ 10-01 14:15:36.009 19060:19060 E/         ]
[adb] handle_packet() t->online(1) p->msg.arg0(9885) p->msg.arg1(0) OPEN


[ 10-01 14:15:36.009 19060:19060 E/         ]
[adb] handle the adb command, and the command = adb shell:cat /proc/stat

你能帮我解决这个问题吗?我的意思是为什么要打印这个额外的文本?我该如何摆脱这个? 我使用过滤器仅显示与包相关的日志,并尝试过滤 TAG 明智的。但是在附加的图片中看到仍然在一些日志中打印了垃圾文本。

提前致谢

【问题讨论】:

  • 为什么不自己写日志文件呢?请参阅 Android 将日志写入文本文件:- stackoverflow.com/questions/1756296/…
  • 这可以在“Hello World”项目中重现吗?如果你只执行Log.d(TAG, "From: " + "34369257099"); 会发生什么?
  • 你会在不同的设备上重现这个吗?
  • @Hugosama 这无济于事......这个垃圾被添加到他自己的日志条目的输出中。这也发生在我身上,非常烦人

标签: android logging


【解决方案1】:

由于不需要的行似乎都包含“[”,我们应该能够创建一个过滤器来排除这些行并获得您需要的内容(您将无法在任何日志中使用该字符声明)。

使用^[^\[]+$ 作为日志消息创建一个过滤器(并确保选择了Regex)。您还必须设置 Log TagPackage Name 或者您从其他应用程序中获取不包含 '[' 的所有内容。

感谢here 对正则表达式的帮助。

【讨论】:

    【解决方案2】:

    在右上角正确查看图像,我们有类似基本过滤器的东西。使用 Show Only Selected Application 将让您只看到属于您的应用程序的内容。您可以使用编辑过滤器配置来使用更复杂的过滤器。

    【讨论】:

      【解决方案3】:

      您使用 ndk 库吗?

      这些日志有点像本机代码输出。

      https://android.googlesource.com/platform/system/core/+/android-4.4_r1/adb/adb.c 有一些命令可以像你一样输出行:

      D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
                  ((char*) (&(p->msg.command)))[1],
                  ((char*) (&(p->msg.command)))[2],
                  ((char*) (&(p->msg.command)))[3]);
      

      恐怕您将无法控制这些输出,除非排除或重写您嵌入到项目中的本机代码。如果你没有嵌入特定的原生库,这可能是来自 os 设备的原生核心源。

      【讨论】:

        【解决方案4】:

        有两种方法可以做到这一点

        1. 首先下载cygwin终端并运行命令adb logcat | grep appname 这里应用程序名称是您的应用程序名称,用于包名称。请注意,此命令只会在 cygwin 中运行。
        2. 过滤日志输出 日志消息的标签是一个短字符串,指示消息源自的系统组件(例如,视图系统的“视图”)。 优先级是以下字符值之一,从最低到最高优先级排序: V:详细(最低优先级) D:调试 一:资料 W:警告 E:错误 F:致命的 S:静音(最高优先级,从未打印任何内容) 您可以通过运行 logcat 并观察每条消息的前两列(以 / 给出)来获取系统中使用的标签列表以及优先级。

        以下是使用 logcat -v brief output 命令获得的简要 logcat 输出示例。它表明该消息与优先级“I”和标签“ActivityManager”有关:

        I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}
        

        要将日志输出减少到可管理的水平,您可以使用过滤器表达式来限制日志输出。过滤器表达式让您可以向系统指示您感兴趣的标签优先级组合 - 系统会抑制指定标签的其他消息。

        过滤器表达式遵循以下格式 tag:priority ...,其中 tag 表示感兴趣的标签,priority 表示为该标签报告的最低优先级。处于或高于指定优先级的该标签的消息将写入日志。您可以在单个过滤器表达式中提供任意数量的 tag:priority 规范。这一系列规范以空格分隔。

        下面是一个过滤器表达式示例,它禁止所有日志消息,除了带有标签“ActivityManager”、优先级为“Info”或更高的日志消息,以及所有带有标签“MyApp”、优先级为“Debug”或更高的日志消息:

        adb logcat ActivityManager:I MyApp:D *:S
        

        上述表达式中的最后一个元素 *:S 将所有标签的优先级设置为“静默”,从而确保仅显示带有“ActivityManager”和“MyApp”的日志消息。使用 *:S 是确保日志输出仅限于您明确指定的过滤器的绝佳方式——它可以让您的过滤器充当日志输出的“白名单”。

        以下过滤器表达式显示所有标签上优先级为“警告”或更高的所有日志消息:

         adb logcat *:W
        

        如果您在开发计算机上运行 logcat(而不是在远程 adb shell 上运行它),您还可以通过导出环境变量 ANDROID_LOG_TAGS 的值来设置默认过滤器表达式:

        export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"
        

        请注意,如果您从远程 shell 运行 logcat 或使用 adb shell logcat,ANDROID_LOG_TAGS 过滤器不会导出到模拟器/设备实例。

        【讨论】:

          【解决方案5】:

          它们是来自另一个进程的日志。您无法阻止它们发生,因此您必须过滤日志输出以仅显示您感兴趣的内容。

          【讨论】:

          • 它们不用于其他进程,如您所见TAG,但它们用于应用程序中使用的其他库代码。
          • 我怎样才能去掉日志中这些多余的文字?
          • @ManavPatadia 使用可用的过滤器developer.android.com/studio/debug/am-logcat.html
          • 我已经使用了可用的过滤器。我在过滤器配置中提到了包名。 Logcat 仅显示与应用相关的日志,但日志消息包含大量垃圾文本以及实际文本。
          【解决方案6】:

          只是猜测:您正在使用可添加更多输出的替代 Log 类。 要检查这一点,请将 Log.d() 调用更改为 android.util.Log.d() 调用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-28
            • 2011-10-06
            • 1970-01-01
            • 2011-12-19
            • 2018-07-15
            相关资源
            最近更新 更多