71

I want to dump Android logcat in a file whenever user wants to collect logs. Through adb tools we can redirect logs to a file using adb logcat -f filename, but how can I do this programmatically?

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
Brijesh Masrani
  • 1,439
  • 3
  • 16
  • 27

4 Answers4

126

Here is an example of reading the logs.

You could change this to write to a file instead of to a TextView.

Need permission in AndroidManifest:

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

Code:

public class LogTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
      Process process = Runtime.getRuntime().exec("logcat -d");
      BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(process.getInputStream()));

      StringBuilder log = new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        log.append(line);
      }
      TextView tv = (TextView) findViewById(R.id.textView1);
      tv.setText(log.toString());
    } catch (IOException e) {
    }
  }
}
Shashanth
  • 4,995
  • 7
  • 41
  • 51
jkhouw1
  • 7,320
  • 3
  • 32
  • 24
  • 3
    @jkhouw1... +60 Best answer i ever seen and great way to post an answer, (also i learn from you) This answer will be always helpful even link got dead,all i need to add just Scroll View and i have done what i wanted, because of your answer, Cheers!! – swiftBoy Jul 03 '12 at 05:58
  • Great..But is there a way to filter these logs as per the Tags? – AbhishekB Aug 28 '12 at 10:55
  • 1
    i haven't tested this but i think you would change it to "logcat -d -s YourTagToFilterOn" – jkhouw1 Aug 28 '12 at 11:28
  • How can we put the package name filter. – kamal_tech_view Mar 12 '14 at 06:38
  • 4
    This permission was removed in Android 4.3, so apps can no longer use it beyond their process. Also, many devices have taken it a step further and this outputs nothing. I don't have a list because I see it fairly often. – Tom Sep 19 '14 at 20:32
  • @Tom: Do we have any other way to get logs and save theme in a file. My app have a option to report problem, where i want to upload that file to server. – Max Jun 05 '15 at 10:26
  • logcat command line options are described here https://developer.android.com/studio/command-line/logcat.html – jayeffkay Dec 07 '16 at 10:22
  • Very nice! This is exactly what I am looking for. I changed it slightly to dump the log output to a file whenever my application crashes. – Shadoninja Apr 07 '17 at 04:34
  • @Shadoninja hi, how you know your app crashes? where you add this code? – flankechen Aug 19 '20 at 09:25
  • @flankechen - I don't remember exactly how I did it, but I have some ideas for you. One way you could do this is to have your `main` function execute the rest of your program in a try/catch block and `catch` the generic `Exception` class. When you do this, any time your program crashes, you could output the exception to logcat. – Shadoninja Aug 23 '20 at 15:14
43

Logcat can write directly to a file:

public static void saveLogcatToFile(Context context) {    
    String fileName = "logcat_"+System.currentTimeMillis()+".txt";
    File outputFile = new File(context.getExternalCacheDir(),fileName);
    @SuppressWarnings("unused")
    Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
}

more info on logcat: see http://developer.android.com/tools/debugging/debugging-log.html

Stéphane
  • 6,920
  • 2
  • 43
  • 53
1

Or you can try this variant

try {
    final File path = new File(
            Environment.getExternalStorageDirectory(), "DBO_logs5");
    if (!path.exists()) {
        path.mkdir();
    }
    Runtime.getRuntime().exec(
            "logcat  -d -f " + path + File.separator
                    + "dbo_logcat"
                    + ".txt");
} catch (IOException e) {
    e.printStackTrace();
}
kenny_k
  • 3,831
  • 5
  • 30
  • 41
xoxol_89
  • 1,242
  • 11
  • 17
0
public static void writeLogToFile(Context context) {    
    String fileName = "logcat.txt";
    File file= new File(context.getExternalCacheDir(),fileName);
    if(!file.exists())
         file.createNewFile();
    String command = "logcat -f "+file.getAbsolutePath();
    Runtime.getRuntime().exec(command);
}

Above method will write all logs into the file. Also please add below permissions in Manifest file

<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Ijas Ahamed N
  • 5,632
  • 5
  • 31
  • 53