Capture Complete LOGS In Android

10,635

Solution 1

I tried this Works Real Well,Not sure How much battery it will consume..If your Application is in Testing Stage You can use this..Before Release you got to Remove this code and Publish..

private void writeADBLogs(){
     BufferedWriter bufferedWriter = null;

      try {
         final File file = new File(sdcardPath);
        bufferedWriter = new BufferedWriter(new FileWriter(file,true));
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()));

        String oneLine;
          while ((oneLine= bufferedReader.readLine()) != null) {
              bufferedWriter.write(oneLine);
              bufferedWriter.newLine();
          }
    } catch (IOException e) {
        e.printStackTrace();
    } 
}

Solution 2

The Android Log will be save only when Your application has Debugging = true in manifest (i.e when you are in Debug mode).

See Documentation at Turn off logging and debugging

So in that case if you want the log then you can implement Thread.setDefaultUncaughtExceptionHandler (Thread.UncaughtExceptionHandler handler)

This will be called always when your application is force closed due to Exception.

What you do is save the StackTrace in a file in append mode.

You can also use this in Debug Mode.

LogCat is a Queue so there are changes that you will miss your log (old Log will be automatically discarded).

I suggest you to implement setDefaultUncaughtExceptionHandler so you never miss any exception log. Also take care to delete the file after use or else your file will became very big in size by time.

Share:
10,635

Related videos on Youtube

NitZRobotKoder
Author by

NitZRobotKoder

Developer, mobile addicted, Android enthusiastic! An ambitious person who always loves to learn new stuff. “A real programmer is not a programmer! I call him as a perfect debugger.”! “Knowledge is power and the way to get power is by sharing knowledge.”!

Updated on June 25, 2022

Comments

  • NitZRobotKoder
    NitZRobotKoder almost 2 years

    I was wondering how do i take complete logs from android device (From the point of My application initialize to any crash or till force close of my application).

    Reason i am posting here is my application is crashed some point,but when i take logs using DDMS/Logcat my crash details are over written with new logs.


    How do i get my crashed reason logs..


    Specially looking to capture Native Code Crash.

    i.e I/DEBUG (21835): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000004...


    Will this adb logcat > crash.txt ensures me that write to file will happen forever?

  • NitZRobotKoder
    NitZRobotKoder about 12 years
    tanZ..I am aware of alogcat Application..But Disadvantage is it wont capture any Native code Crash details i.e I/DEBUG (21835): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000004
  • NitZRobotKoder
    NitZRobotKoder about 12 years
    Vivek native Code Crash can be collected i.e sigsegv segmentation fault
  • Vivek Khandelwal
    Vivek Khandelwal about 12 years
    I think native crash code will be handler. Because at last the Exception will be thrown to Java.