Sunday, September 21, 2014

Add Log file in SDCard android

Now a days some developer want to add error crash report to sdcard.

For those this is the best example for append crash log file to SD Card.

To add crash log file to the SD Card use below code snippet.

To add heap memory information to log file use this.

ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);

To get device information use this

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

Using above two class We can get all the information and print that all in the log file.
To create and write the log file use below piece of code.

File logFile = new File("sdcard/" + getResources().getString(R.string.app_name) + ".sys"));
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

Use below two methods for write the log file on your device SD Card.

This method is for device configuration and exceptions.
public void printDeviceConfig(Context context) {
StringBuilder stringBuilder = new StringBuilder();
try {
System.err.println("=============== HEAP INFO ===============================");
stringBuilder.append("=============== HEAP INFO(S) ===============================");
stringBuilder.append("\n");

ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);

System.err.println("Over All Memory: " + (memoryInfo.availMem / 1024) + " KB");
stringBuilder.append("Over All Memory: " + (memoryInfo.availMem / 1024) + " KB");
stringBuilder.append("\n");
System.err.println("low Memory: " + memoryInfo.lowMemory);
stringBuilder.append("low Memory: " + memoryInfo.lowMemory);
stringBuilder.append("\n");
System.err.println("Threshold Memory: " + (memoryInfo.threshold / 1024) + " KB");
stringBuilder.append("Threshold Memory: " + (memoryInfo.threshold / 1024) + " KB");
stringBuilder.append("\n");

System.err.println("=============== OS INFO ===============================");
stringBuilder.append("=============== OS INFO ===============================");
stringBuilder.append("\n");
System.err.println("Device MODEL: " + android.os.Build.MODEL);
stringBuilder.append("Device MODEL: " + android.os.Build.MODEL);
stringBuilder.append("\n");
System.err.println("VERSION RELEASE: " + android.os.Build.VERSION.RELEASE);
stringBuilder.append("VERSION RELEASE: " + android.os.Build.VERSION.RELEASE);
stringBuilder.append("\n");
System.err.println("VERSION SDK: " + android.os.Build.VERSION.SDK);
stringBuilder.append("VERSION SDK: " + android.os.Build.VERSION.SDK);
stringBuilder.append("\n");

System.err.println("=============== Device Information ===============================");
stringBuilder.append("=============== Device Information ===============================");
stringBuilder.append("\n");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.err.println("Device Resolution (WxH)= " + dm.widthPixels + " x " + dm.heightPixels);
stringBuilder.append("Device Resolution (WxH)= " + dm.widthPixels + " x " + dm.heightPixels);
stringBuilder.append("\n");
System.err.println("Density DPI= " + dm.densityDpi);
stringBuilder.append("Density DPI= " + dm.densityDpi);
stringBuilder.append("\n");

} catch (Exception e) {
e.printStackTrace();
StringWriter stackTrace = new StringWriter();
e.printStackTrace(new PrintWriter(stackTrace));
stringBuilder.append("\n");
stringBuilder.append("=============== Exception while Fetching Information ===============================");
stringBuilder.append("\n");
stringBuilder.append(stackTrace);
stringBuilder.append("\n");
}
appendLog(stringBuilder.toString());
}
Now the below method appendLog() is for the write all the above data in the file which stored in your device SD card.

public void appendLog(String text) {
File logFile = new File("sdcard/" + getResources().getString(R.string.app_name) + ".sys"));
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
Calendar calendar = Calendar.getInstance();
try {
System.err.println("Logged Date-Time : " + calendar.getTime().toLocaleString());
} catch (Exception e) {
}
buf.append("Logged Date-Time : " + calendar.getTime().toLocaleString());
buf.append("\n\n");
buf.append(text);
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}

That's it....
Now every when application run all information write in the log file in your SD Card.

NOTE:

Also when your app is crashed or any exception is occurred in your activity that also write in your log file.

Put this method in all the activity in which you want the exception.
That all exception wrote on log file.

And file name is same as your application name.

Hope this post is help full for you...

No comments:

Post a Comment