我想從一個設備日誌文件拉到我的電腦。我怎樣才能做到這一點?如何從Android設備獲取日誌文件?
回答
Logcollector是一個不錯的選擇,但你必須先安裝它。
當我想要得到的日誌文件通過郵件發送,我通常會做到以下幾點:
- 將設備連接到PC。
- Check that I already setup my os for that particular device。
- 打開一個終端
- 運行
adb shell logcat > log.txt
編輯:
內部日誌是內存中的循環緩衝區。實際上有幾個這樣的循環緩衝器用於無線電,事件,主要。默認是主。
爲了得到緩衝的副本,一種技術涉及到設備上執行的命令,並獲得輸出作爲一個字符串變量。
SendLog是一個開源應用,該應用不只是這樣的:http://www.l6n.org/android/sendlog.shtml
的關鍵是在嵌入式OS的設備上運行logcat
。它不像聽起來那麼難,只需查看鏈接中的開源應用即可。
這轉儲輸出到屏幕上,我需要它在一個文件中。 – Pentium10 2010-05-21 14:45:35
您可以使用aLogCat發送日誌。或者直接從DDMS複製粘貼:P – 2010-05-21 18:33:55
@molnarm我承認我的答案稍微過度設計,如果這就是他需要做的=) – 2010-05-21 18:55:49
一個簡單的方法就是讓自己的日誌收集方法,甚至只是一個現有的從市場上收集日誌應用。
對於我的應用我做它發送日誌到我的電子郵件報告功能(甚至到另一個地方 - 一旦你的日誌,你可以做你是否想用它)。
下面是有關如何從設備獲取日誌文件一個簡單的例子:
我經常遇到錯誤 「logcat read: Invalid argument
」。在閱讀日誌之前,我必須清除日誌。
我這樣做:
prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
謝謝!我長時間有這個錯誤,logcat -c立即修復它! – Jords 2012-01-31 01:16:32
我會用這種東西:
$adb logcat -d > logcat.txt
-d選項轉儲整個循環緩衝區的文本文件,如果你正在尋找特定的動作/意圖嘗試
$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
希望這有助於:)
我希望這個代碼將會幫助別人。我花了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" />
這工作正常,但要在你需要的每個設備上工作'<使用權限android:name =「android.permission.READ_LOGS」/>' – mehdok 2014-07-19 11:43:58
對我來說不起作用 – NinjaCoder 2017-10-24 18:44:20
感謝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){}
哪裏是放這段代碼的最佳位置?在'OnCreate'? – Youngjae 2014-03-31 01:15:31
當你想要的日誌:P – 2015-01-12 13:30:33
首先要確保亞行命令是通過設置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
簡單隻需運行以下命令來獲取輸出到終端:
adb shell logcat
兩個步驟:
- 生成日誌
- 負載Gmail發送日誌
。
生成日誌
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; }
載入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的正常工作答案
- 1. 如何獲取Android設備中的日誌文件位置?
- 2. 獲取設備日誌
- 3. 如何從.jar文件獲取日誌?
- 4. Android設備的日誌文件
- 5. 如何從Android設備讀取Pebble Watch應用程序日誌?
- 6. 如何從設備獲取完整的錯誤日誌
- 7. 如何從c中的ingenico設備獲取apdu命令日誌?
- 8. 如何從黑莓10設備中提取事件日誌?
- 9. 如何從android設備獲取LENS_FOCUS_DISTANCE
- 10. 如何從Android設備獲取錯誤?
- 11. 的Java的Android獲取日誌文件
- 12. 當問題導致用戶拉動電池時,如何從Android設備獲取日誌文件?
- 13. 如果設備凍結/掛起,如何獲取日誌信息?
- 14. 從設備獲取authorId - android
- 15. 從Microsoft.Practices.EnterpriseLibrary.Logging獲取app.config日誌文件名
- 16. 從wireshark pcap文件獲取日誌
- 17. Android:如果設備凍結/掛起,如何獲取日誌記錄信息?
- 18. 如何在日誌文件中獲取舊的日誌細節?
- 19. 如何在Android設備中獲取設備日曆事件列表?
- 20. 如何在Android設備上獲取日曆事件更新?
- 21. 可以從iOS設備獲取多少小時的日誌/控制檯日誌?
- 22. 如何讀取日誌從日誌文件,因爲它發生
- 23. 如果有日曆事件更新,如何從android設備獲取信息?
- 24. 保存日誌文本文件的設備時,關閉在Android
- 25. 如何從Spring.Net獲取日誌文件位置/名稱通用日誌記錄
- 26. Appium - 如何使用Java從iOS設備讀取日誌
- 27. iPhone:如何從設備讀取應用程序日誌
- 28. 通過代碼從設備獲取崩潰日誌iOS
- 29. 如何持續備份日誌文件?
- 30. 如何壓縮備份日誌文件?
這個命令行不適合我,我得到這個輸出。 logcat讀取:無效參數 – neoneye 2011-08-24 09:23:33
in win:adb.exe – Macarse 2011-08-24 11:38:58
請注意,logcollector不適用於4.1以上版本的Android,因爲應用程序現在只允許讀取自己的日誌條目。 (https://groups.google.com/forum/#!topic/android-log-collector/vgYXYbg-O1U) – 2013-07-01 12:19:51