After the application occurs crash to see log, to determine where the problem is, but once the application is published, it is necessary to find a way to the user's crash log analysis.
So to crawl log after the occurrence of crash, and then upload to the server, for developers to view, now there are many third-party to do this service, here is how to implement their own.
In fact, the principle is very simple, after the application of an exception, will be the default exception handler to handle the exception,
All we have to do is take over the task and handle the exception ourselves, including collecting logs, saving them locally, and uploading them to the server.
The following is the exception handling class that you implement.
public class Crashhandler implements Uncaughtexceptionhandler {public static final String TAG = "Crashhandler";//system default UNC Aughtexception Processing class Private Thread.uncaughtexceptionhandler mdefaulthandler;//Crashhandler instance private static Crashhandler INSTANCE = new Crashhandler ();//Program Context object private Context mcontext;//used to store device information and exception information private map< String, string> infos = new hashmap<string, string> ();//For formatting dates, as part of the log file name private DateFormat formatter = new Si Mpledateformat ("Yyyy-mm-dd_hh-mm-ss");p rivate String namestring;/** guarantees only one Crashhandler instance */private Crashhandler () {} /** get Crashhandler instance, Singleton mode */public static Crashhandler getinstance () {return INSTANCE;} /** * Initialize * * @param context */public void init (context context) {Mcontext = context;//get system default Uncaughtexception processor Mdefau Lthandler = Thread.getdefaultuncaughtexceptionhandler ();// Set the Crashhandler as the default processor Thread.setdefaultuncaughtexceptionhandler (this) for the program; namestring = Bmobusermanager.getinstance (Mcontext). Getcurrentusername ();} /** * WhenUncaughtexception occurs when it is transferred to the function to process */@Overridepublic void uncaughtexception (thread thread, Throwable ex) {if (! HandleException (ex) && Mdefaulthandler! = null) {//If the user does not handle it, let the system default exception handler handle the Mdefaulthandler.uncaughtexception (thread, ex);} else {try {thread.sleep,} catch (Interruptedexception e) {log.e (TAG, "error:", e);} Exit Program Android.os.Process.killProcess (Android.os.Process.myPid ()); System.exit (1);}} /** * Custom error handling, collect error messages to send error reports, etc. are done here. * * @param ex * @return true: Returns False if the exception information is processed; */private boolean handleexception (Throwable ex) {if (ex = = null) {return false;} Wondermapapplication.getinstance (). Getsputil (). Setcrashlog (true);//Every time you enter the application check, if there is a log, there is an upload//use toast to display the exception information new Thread () {@Overridepublic void run () {looper.prepare (); Toast.maketext (Mcontext, "Sorry, the program has an exception, is collecting logs, is about to exit", Toast.length_long). Show (); Looper.loop ();}}. Start ();//Collect device parameter information collectdeviceinfo (mcontext);//save log file string fileName = Savecrashinfo2file (ex); return true;} /** * Collect device parameter information * * @param ctx */public void CollectDeviceInfo (Context ctx) {try {packagemanager pm = Ctx.getpackagemanager (); PackageInfo pi = pm.getpackageinfo (Ctx.getpackagename (), packagemanager.get_activities); if (pi! = null) {String Versionname = Pi.versionname = = null? "NULL": Pi.versionname; String Versioncode = Pi.versioncode + ""; Infos.put ("Versionname", Versionname); Infos.put ("Versioncode", VersionCode);}} catch (Namenotfoundexception e) {log.e (TAG, "An error occured if collect package info", e);} field[] fields = Build.class.getDeclaredFields (), for (Field field:fields) {try {field.setaccessible (true); Infos.put ( Field.getname (), Field.get (null). ToString ()); LOG.D (TAG, Field.getname () + ":" + field.get (null));} catch (Exception e) {log.e (TAG, "An error occured when collect crash info", e);}} /** * Save error message to file * * @param ex * @return return file name for easy Transfer of file to server */private String Savecrashinfo2file (Throwable ex) {Stringbuff ER sb = new StringBuffer (); for (map.entry<string, string> entry:infos.entrySet ()) {String key = Entry.getkey(); String value = Entry.getvalue (); Sb.append (key + "=" + value + "\ n");} Writer writer = new StringWriter (); PrintWriter printwriter = new PrintWriter (writer); Ex.printstacktrace (PrintWriter); Throwable cause = Ex.getcause (); while (cause! = null) {cause.printstacktrace (printwriter); cause = Cause.getcause ();} Printwriter.close (); String result = Writer.tostring (); L.D (wmodel.crashupload, result); Sb.append (result); try {long timestamp = System.currenttimemillis (); String time = Formatter.format (new Date ()); String fileName = namestring + "-" + Time + "-" + timestamp+ ". Log"; if (Environment.getexternalstoragestate (). Equals (EnviR Onment. media_mounted)) {String path = Wmapconstants.crashlogdir; File dir = new file (path), if (!dir.exists ()) {dir.mkdirs ();} FileOutputStream fos = new FileOutputStream (path + fileName), Fos.write (Sb.tostring (). GetBytes ()); Fos.close (); return fileName;} catch (Exception e) {log.e (TAG, "An error occured while writing file ...", e);} return null;}}
Use the following methods:
Add the following in the OnCreate of application
Crashhandler Crashhandler = Crashhandler.getinstance (); Crashhandler.init (this);
In this way, the application occurs crash, automatically save log to local.
In fact, there is a good thing, is not in the logcat inside to look for logs, directly to the local folder open to see just.
jason0539
Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)
Sweep code Follow me public number
Android app crash Auto grab Log_ automatically save crash log to Local