2010-05-21 55 views

回答

95

Logcollector是一個不錯的選擇,但你必須先安裝它。

當我想要得到的日誌文件通過郵件發送,我通常會做到以下幾點:

+0

這個命令行不適合我,我得到這個輸出。 logcat讀取:無效參數 – neoneye 2011-08-24 09:23:33

+1

in win:adb.exe – Macarse 2011-08-24 11:38:58

+5

請注意,logcollector不適用於4.1以上版本的Android,因爲應用程序現在只允許讀取自己的日誌條目。 (https://groups.google.com/forum/#!topic/android-log-collector/vgYXYbg-O1U) – 2013-07-01 12:19:51

10

編輯:

內部日誌是內存中的循環緩衝區。實際上有幾個這樣的循環緩衝器用於無線電,事件,主要。默認是主。

爲了得到緩衝的副本,一種技術涉及到設備上執行的命令,並獲得輸出作爲一個字符串變量。

SendLog是一個開源應用,該應用不只是這樣的:http://www.l6n.org/android/sendlog.shtml

的關鍵是在嵌入式OS的設備上運行logcat。它不像聽起來那麼難,只需查看鏈接中的開源應用即可。

+0

這轉儲輸出到屏幕上,我需要它在一個文件中。 – Pentium10 2010-05-21 14:45:35

+0

您可以使用aLogCat發送日誌。或者直接從DDMS複製粘貼:P – 2010-05-21 18:33:55

+0

@molnarm我承認我的答案稍微過度設計,如果這就是他需要做的=) – 2010-05-21 18:55:49

4

一個簡單的方法就是讓自己的日誌收集方法,甚至只是一個現有的從市場上收集日誌應用。

對於我的應用我做它發送日誌到我的電子郵件報告功能(甚至到另一個地方 - 一旦你的日誌,你可以做你是否想用它)。

下面是有關如何從設備獲取日誌文件一個簡單的例子:

7

我經常遇到錯誤 「logcat read: Invalid argument」。在閱讀日誌之前,我必須清除日誌。

我這樣做:

prompt> cd ~/Desktop 
prompt> adb logcat -c 
prompt> adb logcat | tee log.txt 
+0

謝謝!我長時間有這個錯誤,logcat -c立即修復它! – Jords 2012-01-31 01:16:32

21

我會用這種東西:

$adb logcat -d > logcat.txt 

-d選項轉儲整個循環緩衝區的文本文件,如果你正在尋找特定的動作/意圖嘗試

$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt 

希望這有助於:)

27

我希望這個代碼將會幫助別人。我花了2天弄清楚如何從設備登錄,然後將其過濾:

public File extractLogToFileAndWeb(){ 
     //set a file 
     Date datum = new Date(); 
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALY); 
     String fullName = df.format(datum)+"appLog.log"; 
     File file = new File (Environment.getExternalStorageDirectory(), fullName); 

     //clears a file 
     if(file.exists()){ 
      file.delete(); 
     } 


     //write log to file 
     int pid = android.os.Process.myPid(); 
     try { 
      String command = String.format("logcat -d -v threadtime *:*");   
      Process process = Runtime.getRuntime().exec(command); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
      StringBuilder result = new StringBuilder(); 
      String currentLine = null; 

      while ((currentLine = reader.readLine()) != null) { 
        if (currentLine != null && currentLine.contains(String.valueOf(pid))) { 
         result.append(currentLine); 
         result.append("\n"); 
        } 
      } 

      FileWriter out = new FileWriter(file); 
      out.write(result.toString()); 
      out.close(); 

      //Runtime.getRuntime().exec("logcat -d -v time -f "+file.getAbsolutePath()); 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show(); 
     } 


     //clear the log 
     try { 
      Runtime.getRuntime().exec("logcat -c"); 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show(); 
     } 

     return file; 
    } 

通過@mehdok

爲指向的權限添加到清單讀取日誌

<uses-permission android:name="android.permission.READ_LOGS" /> 
+1

這工作正常,但要在你需要的每個設備上工作'<使用權限android:name =「android.permission.READ_LOGS」/>' – mehdok 2014-07-19 11:43:58

+0

對我來說不起作用 – NinjaCoder 2017-10-24 18:44:20

2

感謝user1354692我可以讓它更簡單,只有一行!他曾這樣評價了一句:

try { 
    File file = new File(Environment.getExternalStorageDirectory(), String.valueOf(System.currentTimeMillis())); 
    Runtime.getRuntime().exec("logcat -d -v time -f " + file.getAbsolutePath());}catch (IOException e){} 
+0

哪裏是放這段代碼的最佳位置?在'OnCreate'? – Youngjae 2014-03-31 01:15:31

+0

當你想要的日誌:P – 2015-01-12 13:30:33

0

首先要確保亞行命令是通過設置PATH到Android SDK平臺的工具可執行文件:

export PATH=/Users/espireinfolabs/Desktop/soft/android-sdk-mac_x86/platform-tools:$PATH 

然後運行:

adb shell logcat > log.txt 

OR第一招to adb platform-tools:

cd /Users/user/Android/Tools/android-sdk-macosx/platform-tools 

然後運行

./adb shell logcat > log.txt 
2

我創建了一個小型圖書館(.aar)通過電子郵件檢索日誌。您可以在Gmail帳戶中使用它。這很簡單,但工作。你可以得到一份副本from here

該網站是西班牙文,但有一個PDF格式與英文版的產品說明。

我希望它能提供幫助。

+0

偉大的,這是非常有用的謝謝! – SalvoC 2015-08-01 13:23:04

2

簡單隻需運行以下命令來獲取輸出到終端:

adb shell logcat 
9

對於那些USB調試不感興趣或使用adb還有一個更簡單的解決方案。在Android 6(不知道以前的版本),有開發工具下的選項:採取錯誤報告

點擊此選項將準備一個錯誤報告,並提示您將其保存到驅動器或通過電子郵件發送。

我發現這是獲取日誌的最簡單方法。我不喜歡打開USB調試。

+1

正是我在找的東西! – YYamil 2016-07-05 19:23:30

+1

我在哪裏可以找到它? – gurehbgui 2017-04-24 10:07:59

1

兩個步驟:

  • 生成日誌
  • 負載Gmail發送日誌

  1. 生成日誌

    File generateLog() { 
        File logFolder = new File(Environment.getExternalStorageDirectory(), "MyFolder"); 
        if (!logFolder.exists()) { 
         logFolder.mkdir(); 
        } 
        String filename = "myapp_log_" + new Date().getTime() + ".log"; 
    
        File logFile = new File(logFolder, filename); 
    
        try { 
         String[] cmd = new String[] { "logcat", "-f", logFile.getAbsolutePath(), "-v", "time", "ActivityManager:W", "myapp:D" }; 
         Runtime.getRuntime().exec(cmd); 
         Toaster.shortDebug("Log generated to: " + filename); 
         return logFile; 
        } 
        catch (IOException ioEx) { 
         ioEx.printStackTrace(); 
        } 
    
        return null; 
    } 
    
  2. 載入Gmail的日誌發送

    File logFile = generateLog(); 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(logFile)); 
    intent.setType("multipart/"); 
    startActivity(intent); 
    

參考#1

~~

#2 - 有許多不同的答案在那裏爲如何加載日誌文件查看和發送。最後,這裏的解決方案實際工作既:

  • 負載Gmail作爲一個選項
  • 附加文件成功

非常感謝https://stackoverflow.com/a/22367055/2162226的正常工作答案

相關問題