简体   繁体   English

如何从android进程中获取错误日志以及何时调用get error log方法?

[英]How to get the error log from android process and when to call the get error log method?

I have implemented the try catch on the exception possible places. 我已经在异常可能的地方实施了try catch。 There are different other exception will be thrown by android OS[Database Exception, Permission and other exception]. Android OS会抛出其他不同的异常[数据库异常,权限和其他异常]。 I want to write those error log data into file{only there is a crash on our application}. 我想将这些错误日志数据写入文件{仅在我们的应用程序崩溃时}。

Already I have code to write the given data into file. 我已经有代码将给定的数据写入文件。 I just want to know how to get the error log from android process. 我只想知道如何从android进程获取错误日志。

If there is any crash the app instance is not available. 如果发生任何崩溃,则该应用实例不可用。 And I don't know the exact place where to call the get the error log method. 而且我不知道在何处调用get错误日志方法。 on this. 在此。 Please help me on this. 请帮我。

In your application class you can initialize for an unhandled exception. 在您的应用程序类中,您可以初始化未处理的异常。 The callback method will have an throwable object. 回调方法将具有可抛出对象。 From this object we can get the crash log data and then the same data to be written to the file. 从该对象中,我们可以获取崩溃日志数据,然后将相同的数据写入文件。

Application Class: 应用类别:

public class MyApplication extends Application {
    // uncaught exception handler variable
    private UncaughtExceptionHandler defaultUEH;

    public MyApplication() {
        defaultUEH = Thread.getDefaultUncaughtExceptionHandler();

        // setup handler for uncaught exception
        Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
    }

    // handler listener
    private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
                        //To write the crash log data to file
            AppUtil.getInstance().writeActivityLogToFile(true,
                    getErrorMessgae(ex));

            // re-throw critical exception further to the os (important) to handle
            defaultUEH.uncaughtException(thread, ex);
            // System.exit(2);
        }
    };

    /**
     * To get the crash log message from throwable object
     * 
     * @param e
     *            - throwable exception object
     * @return - the crash log
     */
    private String getErrorMessgae(Throwable e) {
        StackTraceElement[] stackTrackElementArray = e.getStackTrace();
        String crashLog = e.toString() + "\n\n";
        crashLog += "--------- Stack trace ---------\n\n";
        for (int i = 0; i < stackTrackElementArray.length; i++) {
            crashLog += "    " + stackTrackElementArray[i].toString() + "\n";
        }
        crashLog += "-------------------------------\n\n";

        // If the exception was thrown in a background thread inside
        // AsyncTask, then the actual exception can be found with getCause
        crashLog += "--------- Cause ---------\n\n";
        Throwable cause = e.getCause();
        if (cause != null) {
            crashLog += cause.toString() + "\n\n";
            stackTrackElementArray = cause.getStackTrace();
            for (int i = 0; i < stackTrackElementArray.length; i++) {
                crashLog += "    " + stackTrackElementArray[i].toString()
                        + "\n";
            }
        }
        crashLog += "-------------------------------\n\n";
        return crashLog;
    }
}

Declare your application class in your manifest in the following tag: 在清单文件的以下标记中声明您的应用程序类:

<application
    android:name=".application.FleetLOCApplication"
    ......>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM