2016-10-01 28 views
10

look at the image to get more idea當我只需登錄Android應用程序的消息,Android的監控打印太多額外的不必要的垃圾文字,如: 代碼:Log.d()或Log.e()在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明智。但在附圖中看到,仍然有些垃圾文本被打印在某些日誌中。

在此先感謝

+0

爲什麼不寫你自己的日誌文件?請參閱Android將日誌寫入文本文件:https://stackoverflow.com/questions/1756296/android-writing-logs-to-text-file –

+0

這可以在「Hello World」項目中重現嗎?如果你只是執行'Log.d(TAG,「From:」+「34369257099」);'? –

+0

你是否在不同的設備上重現了這一點? – smora

回答

0

它們是來自另一個進程的日誌。您無法防止它們發生,因此您必須過濾日誌輸出以僅顯示您感興趣的內容。

+0

它們不適用於其他進程,因爲您可以看到'TAG',但它們是用於應用程序中使用的其他庫代碼。 –

+0

如何擺脫這些日誌中的額外文本? –

+0

@ManavPatadia使用可用的過濾器https://developer.android.com/studio/debug/am-logcat.html – nandsito

3

由於不需要的行似乎都有'[',所以我們應該能夠創建一個排除這些行的過濾器&獲取您需要的內容(您將無法在任何日誌語句)。

創建使用^[^\[]+$作爲日誌消息濾波器(&確保正則表達式被選中)。您還必須設置日誌標記程序包名稱或者您從其他不包含'['的應用程序獲取所有內容。

感謝here尋求正則表達式的幫助。

screenshot of logcat Filter editor

1

看到圖像正確的右上角,我們必須像一個基本的過濾器。使用只顯示選定的應用程序將讓你只看到屬於你的應用程序。您可以使用編輯過濾器配置來使用更復雜的過濾器。

+1

我認爲這是正確的答案 – auval

-3

只是一個猜測:您正在使用的替代日誌類,增加了更多的產出。 要檢查此,請將Log.d()調用更改爲android.util.Log.d()調用。

1

您是否使用ndk librairies?

那些日誌類似於本地代碼輸出。

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設備的本地核心源。

1

有兩種方法可以做到這

  1. 首先下載Cygwin的終端,運行命令adb logcat | grep appname 這裏的應用程序名稱是在包名稱中使用您的應用程序名稱。注意這個命令只能在cygwin中運行。
  2. 篩選日誌輸出 日誌消息的標記是一個短字符串,指示消息從其發出的系統組件(例如,視圖系統的「視圖」)。 的重點是以下字符值之一,下令從最低到最高優先級: 五:詳細(最低優先級) d:調試 我:信息 W:警告 E:錯誤 F:致命 S:無聲(最高優先級,沒有打印任何內容) 您可以通過運行logcat並觀察每條消息的前兩列(以/表示)來獲得系統中使用的標記列表,包括優先級。

以下是使用logcat -v brief輸出命令獲得的簡短logcat輸出示例。這表明該消息涉及到優先級「I」和標籤「ActivityManager」:

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

要將日誌輸出降低到可控的水平,則可以使用過濾表達式限制日誌輸出。過濾器表達式可讓您向系統指示您感興趣的標籤優先級組合 - 系統會爲指定標籤抑制其他消息。

過濾表達式遵循以下格式tag:priority ...,其中tag表示感興趣的標記,priority表示要爲該標記報告的最低優先級別。該標記的消息等於或高於指定的優先級將寫入日誌。您可以在單個過濾器表達式中提供任意數量的標籤:優先級規格。該系列規範是以空格分隔的。

以下是一個篩選表達式的示例,該篩選表達式除了具有標記「ActivityManager」,優先級爲「Info」或更高的所有日誌消息,以及具有標記「MyApp」且優先級爲「Debug」或更高優先級的所有日誌消息:

adb logcat ActivityManager:I MyApp:D *:S 

在上述表達式中的最後一個元素,*:S,設置所有標記爲「沉默的」,因此僅確保與「ActivityManager」和「MyApp的」日誌消息被顯示在優先級。使用*:S是確保日誌輸出僅限於您明確指定的過濾器的絕佳方式 - 它可讓您的過濾器充當日誌輸出的「白名單」。

以下過濾表達式顯示優先級爲「警告」的所有日誌消息和更高,對所有標籤:

adb logcat *:W 

如果你從你的開發計算機上運行logcat的(相對於上一個遠程ADB運行它

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

注意ANDROID_LOG_TAGS過濾器不輸出到仿真器/設備實例,如果從遠程運行的logcat:殼),也可以通過導出用於環境變量ANDROID_LOG_TAGS的值設置默認過濾器表達式shell o使用adb shell logcat。