The Android logging system provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in a series of circular buffers.
Img src
We can test app on Emulator and with the help of Logcat we can easily trace the Error details.
But if we are testing on Real Android Device and that is not connected with system then its hard to know where we are getting exception. So this tutorial aims to resolve this problem.
We all know how to view LogCat message in Eclipse but how to write the LogCat details into a file and save it in SD card?
Using Shell command :
<RelativeLayout
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" >
<Button
</RelativeLayout>
findViewById(R.id.btnWriteLogCat).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
protected void writeLogCat()
{
Img src
We can test app on Emulator and with the help of Logcat we can easily trace the Error details.
But if we are testing on Real Android Device and that is not connected with system then its hard to know where we are getting exception. So this tutorial aims to resolve this problem.
We all know how to view LogCat message in Eclipse but how to write the LogCat details into a file and save it in SD card?
Using Shell command :
To save LogCat to a text file open up a terminal window and type:
adb logcat -d > logcat.txt
The above command will create a file named "logcat.txt" in your current directory. The -d option indicates that you are dumping the current contents and then exiting.
Read the LogCat programmatically :
Permission in Manifest :
- <uses-permission android:name = "android.permission.READ_LOGS" />
- <uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
Step 1 : Create a new project by going to File ⇒ New Android Application Project. Fill all the details and name your activity as MainActivity. (eclipse)
Step 2 : Design the activity_main.xml layout for MainActivity under layout folder as shown below
activity_main.xml
<RelativeLayout
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" >
<Button
android:id = "@+id/btnWriteLogCat"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"
android:layout_height = "wrap_content"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"
android:text = "Write LogCat" />
</RelativeLayout>
Step 3 : Add the following code in onCreate() method of MainActivity.java under src folder.
MainActivity.java
writeLogCat();
protected void writeLogCat()
{
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);
log.append("\n");
}
//Convert log to string
final String logString = new String(log.toString());
//Create txt file in SD Card
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() +File.separator + "Log File");
if(!dir.exists())
{
dir.mkdirs();
}
File file = new File(dir, "logcat.txt");
//To write logcat in text file
FileOutputStream fout = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fout);
//Writing the string to file
osw.write(logString);
osw.flush();
osw.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
StringBuilder log = new StringBuilder();
String line;
while((line = bufferedReader.readLine()) != null)
{
log.append(line);
log.append("\n");
}
//Convert log to string
final String logString = new String(log.toString());
//Create txt file in SD Card
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() +File.separator + "Log File");
if(!dir.exists())
{
dir.mkdirs();
}
File file = new File(dir, "logcat.txt");
//To write logcat in text file
FileOutputStream fout = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fout);
//Writing the string to file
osw.write(logString);
osw.flush();
osw.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
Path : sdcard/Log File/logcat.txt
Source link : GreenMan
As always, Thanks a lot for reading...
As always, Thanks a lot for reading...
Don't forget to share this post if you found it interesting!
If you find something to add to this post? or any other quick thoughts/hints that you think people will find useful? Share it in the comments & feedback's are most welcome.
Hi I'm looking to write data displayed in the logcat to a text file in real time so that I can access the data via python as the data is sent. Is this possible?
ReplyDeleteTq for sharing valuable information with us about study bible
ReplyDeleteTo recover lost files click on: android data recovery
android data recovery apk without root
android data recovery software
android data recovery app
android data recovery free
android recovery
diskdigger for android
Great post.It seems to be interesting and gave me lot of information,Thank for sharing it.I am looking forward for new articles.keep it up.
ReplyDeleteWebsite Development Company in Bangalore | Website Design Company in Bangalore | Mobile App Development Companies in Bangalore | Game Development Companies in Bangalore
Best article, very useful and explanation. Your post is extremely incredible. Thank you very much for the new information.
ReplyDeleteAndroid Development Company in Texas
Thank you for taking the time to write such an informative post. Your blog is not only informative, but it is also very creative.
ReplyDeleteandroid app development company
オフショア アウトソーシングには、タイム ゾーンの異なる別の国からベンダーまたはシステム開発パートナーを雇うことも含まれます。gjnetwork は、日本で最高のオフショア システム開発会社です。
ReplyDeleteit オフショア と は