Search code examples
androidlogginglogcatbug-reporting

How to read my application logcat and write in to file?


I want to read my application log on my phone, and after that write it to file. i can see and check my log in eclipse like this :

12-26 11:53:07.456: I/power(188): *** set_screen_state 0
12-26 11:53:07.496: D/Sensors(188): Enable G-Sensor: en = 0
12-26 11:53:07.506: D/Sensors(188): mEnabled = 0x0
12-26 11:53:07.506: D/Sensors(188): /data/misc/AccPrmsF.ini does not exist
12-26 11:53:07.596: D/AccelerometerListener(340): enable(false)
12-26 11:53:07.686: V/TransportControlView(188): Create TCV com.android.internal.widget.TransportControlView@412f16a8
12-26 11:53:07.957: D/dalvikvm(188): GC_CONCURRENT freed 1852K, 52% free 11229K/23367K, paused 15ms+23ms
12-26 11:53:07.967: D/SurfaceFlinger(125): About to give-up screen, flinger = 0x15a1918
12-26 11:53:08.187: I/ActivityManager(188): Start proc com.google.android.gsf.login for service com.google.android.gsf.login/com.google.android.gsf.loginservice.GoogleLoginService: pid=25736 uid=10005 gids={3003, 1015, 1007, 2001, 3006}
12-26 11:53:08.207: D/PhoneStatusBar(27013): disable: < expand icons alerts ticker system_info back home RECENT* clock >
12-26 11:53:08.277: W/IInputConnectionWrapper(188): showStatusIcon on inactive InputConnection
12-26 11:53:08.297: D/PhoneStatusBar(27013): disable: < expand icons alerts ticker system_info BACK* HOME* RECENT CLOCK* >
12-26 11:54:16.604: W/ThrottleService(188): unable to find stats for iface rmnet0
12-26 12:00:24.983: D/dalvikvm(188): GC_EXPLICIT freed 1532K, 51% free 11674K/23367K, paused 6ms+22ms

but i want to read this on my phone. How can i do that?!


Solution

  • Writing logs into logcat from your application is very easy. Reading logs is usually used for bug report purposes.

    The following code reads logs and write it to file:

    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);
                }
    
                String logFilePath = Environment.getExternalStorageDirectory() + File.separator + "LogTest.txt";
    
                File logFile = new File(logFilePath);
                if (!logFile.exists())
                    logFile.createNewFile();
    
                FileOutputStream outStream = new FileOutputStream(logFile, true);
                byte[] buffer = log.toString().getBytes();
    
                outStream.write(buffer);
                outStream.close();
            } catch (Exception ex) {
                ex.printStackTrace();
    
            }
        }
    }
    

    And also you needs two permission for reading logs and write file to external storage:

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

    You can add any more parameters to the logcat command string.More info in android developer.

    When implementing a bug report feature, with sending logs to yourself, don't forget that many applications outputs personal information into the logcat, so a policy about them is definitely needed.

    I hope this post is useful ;-)