回答:
Logcollectorは適切なオプションですが、最初にインストールする必要があります。
メールで送信するログファイルを取得したいときは、通常次のようにします。
adb shell logcat > log.txtadb。通常はC:\Users\[your username]\AppData\Local\Android\Sdk\platform-tools\
このコードが誰かを助けることを願っています。デバイスからログを記録し、それをフィルタリングする方法を見つけるのに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" />
<uses-permission android:name="android.permission.READ_LOGS" />
USBのデバッグや使用adbに興味がない場合は、より簡単な解決策があります。Android 6(以前のバージョンについては不明)には、開発者ツールの下にオプションがあります:バグレポートを取得
このオプションをクリックすると、バグレポートが準備され、ドライブに保存するか、電子メールで送信するように求められます。
これがログを取得する最も簡単な方法であることがわかりました。USBデバッグをオンにしたくない。
編集:
内部ログはメモリ内の循環バッファです。実際には、ラジオ、イベント、メインのそれぞれに、このような循環バッファーがいくつかあります。デフォルトはmainです。
バッファのコピーを取得するには、デバイスでコマンドを実行し、出力を文字列変数として取得する方法が1つあります。
SendLogはこれを行うオープンソースのアプリです:http ://www.l6n.org/android/sendlog.shtml
重要なのはlogcat、組み込みOSのデバイスで実行することです。それほど難しくはありません。リンクからオープンソースアプリをチェックしてください。
多くの場合、エラー「logcat read: Invalid argument」が発生します。ログから読み取る前に、ログをクリアする必要がありました。
私はこれが好きです:
prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
簡単な方法は、独自のログコレクターメソッドを作成するか、市場から既存のログコレクターアプリを作成することです。
私のアプリでは、ログを自分の電子メール(または別の場所)に送信するレポート機能を作成しました。ログを取得すると、それを使用するかどうかを確認できます。
次に、デバイスからログファイルを取得する簡単な例を示します。
古い質問であることは承知していますが、2018年でもまだ有効だと思います。
すべてのAndroidデバイスの開発者向けオプションに隠されているバグレポートを取得するオプションがあります。
注:これはシステムログ全体をダンプします
開発者向けオプションを有効にする方法は?参照:https : //developer.android.com/studio/debug/dev-options
私のために働くもの:
これをどう読むか?バグレポートを開く-1960-01-01-hh-mm-ss.txt
あなたはおそらくこのようなものを探したいでしょう:
------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of crash
06-13 14:37:36.542 19294 19294 E AndroidRuntime: FATAL EXCEPTION: main
または:
------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of main
user1354692のおかげで、1行だけでもっと簡単にできました。彼がコメントしたもの:
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?
2つのステップ:
- ログを生成する
- 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に感謝
まず、PATHをandroid sdk platform-toolsに設定して、adbコマンドが実行可能であることを確認します。
export PATH=/Users/espireinfolabs/Desktop/soft/android-sdk-mac_x86/platform-tools:$PATH
次に実行します:
adb shell logcat > log.txt
または、最初にadb platform-toolsに移動します。
cd /Users/user/Android/Tools/android-sdk-macosx/platform-tools
次に実行します
./adb shell logcat > log.txt