O método abaixo é responsável por criar um arquivo de log na raiz do sdcard/ com a data do dia mais o appName passado por parametro. Após gerar o arquivo ele limpa o log do sistema, pois se chamado novamente ele escreve as novas informações no final do arquivo.
public class LogCat { private static final String LOG = "LogCat"; public static File extractLogToFile(Context context, String appName) { //set a file SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH); String fullName = appName.toUpperCase() + "_" + df.format(new Date()) + ".log"; File file = new File(Environment.getExternalStorageDirectory(), fullName); try { if (!file.exists()) file.createNewFile(); String command = String.format("logcat -d -v threadtime " + context.getPackageName() + "*:*"); 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) { result.append(currentLine); result.append("\n"); } FileWriter out = new FileWriter(file, true); out.append(result.toString()); out.flush(); out.close(); //clear the log try { Runtime.getRuntime().exec("logcat -c"); } catch (IOException e) { } } catch (IOException e) { Log.e(LOG, "extractLog", e); file = null; } return file; } }
Android – Salvar Logcat em arquivo (Save LogCat To A Text File)