Strategies / Techniques for Crash Reporting in Java

I am developing a new application for Java applications and I want to enable the crash reporting tool - you know what happens with the program - the program crashes, a window appears asking you to click on it to send it, etc.

I like how the error will be sent to our servers - perhaps through a simple web service. I am no longer sure how the failure capture mechanism should be implemented. I would welcome any advice from those who have implemented something similar.

+4
source share
6 answers

I see three cases:

  • disaster. The JVM itself is either dead or dying. You cannot assume that any of your codes will work - for example, you cannot allocate any memory. Therefore, in this case you cannot hope that you can send any diagnostics. The best you can hope for is to perform some diagnostic operations, such as the remnants of a kernel left in the ashes of a dead program. In this case, when starting a new launch, you can search for such debris and suggest that the user collect it or, rather, try to collect the diagnostic package itself.

  • Low-level application code does not throw an exception, possibly a RunTime exception, such as a NullPointer exception. In this case, you could in your main (provided that you have) you could catch an Exception and hope that your Crash Reporter code will work. Pass the exception and stack trace to Crash Reporter.

  • You low-level code will catch something really unhealthy. It is not enough to terminate the process, but it is worth reporting it. Here you have not only an exception, but also other contextual information. We have even more to ship to Crash Reporter.

+2
source

There is a command line option that you can provide the JVM that will run the batch file after the JVM crashes with a memory dump. All you do is create an external program that reports an error, and then use the JVM parameter to send the core dump via email using the utility you made.

-XX:-HeapDumpOnOutOfMemoryError -XX:OnError="<cmd args>;<cmd args>" 
+7
source

Use Thread.setUncaughtExceptionHandler and static Thread.setDefaultUncaughtExceptionHandler to (try) to report exceptions from your logging system.

+5
source

Use logging. The general template works as follows:

  • Create an application that sends an error message to the server (most registration frameworks support applications that send log messages via mail or even JDBC). If there is no existing appender, they have examples of how to do this.
  • Add this application to the root log and set the threshold to ERROR
  • Record the error if you notice an exception. Then the logging system will make plumbing for you.
+2
source

I don't know if this is the best Java can offer, but this is what I did some time ago.

At first, all the interesting activity that might crash was sent using the command template. This application consisted of getting the application server over the Internet, so a lot could go wrong. Exceptions were caught by the command manager and the corresponding result displayed to the user (usually a dialog box with an error message is displayed, after which a trip message is issued and an alarm message is sent).

Second, Swing used a custom event queue to catch any exceptions that occur in the event stream. I hope Java now has a better solution, but basically when the exception occurred, you had to check if your code was involved, otherwise some Swing errors could crash your application, which you don't like. And, of course, you had to check the recursion (the crash repeats over and over again when you try to display a message to the user).

By the way, most of any failure will keep your JVM, including due to memory errors, sufficient to send e-mail in most cases, since after an error from memory, as a rule, the error frees up enough stack (and therefore a lot), to allow further garbage collection and paste your code live. But in this case, you should still get out quickly. IDEA continues to work after a memory error, but often does not work properly. They would be better off, IMO.

You push a new queue with the next and subclass of EventQueue to associate your behavior.

  Toolkit.getDefaultToolkit().getSystemEventQueue().push(newQueue); 
0
source

One option is to use BugSense. It is designed to report crashes in mobile applications, but the API states that it can be used for any crashes. This is pretty simple from what I read, and all I have to do is create a simple POST request with all the values.

 { "client": { "name": "bugsense-android", // Obligatory "version": "0.6" }, "request": { "remote_ip": "10.0.0.1", "custom_data": { "key1": "value1", "key2": "value2" } }, "exception": { "message": "java.lang.RuntimeException: exception requested", // Obligatory "where": "MainActivity.java:47", // Obligatory "klass": "java.lang.RuntimeException", // Type of exception "backtrace": "java.lang.RuntimeException: exception requested\r\nat com.sfalma.trace.example.MainActivity$1.onClick(MainActivity.java:47)\r\nat android.view.View.performClick(View.java:2408)\r\nat android.view.View$PerformClick.run(View.java:8816)\r\nat android.os.Handler.handleCallback(Handler.java:587)\r\nat android.os.Handler.dispatchMessage(Handler.java:92)\r\nat android.os.Looper.loop(Looper.java:123)\r\nat android.app.ActivityThread.main(ActivityThread.java:4627)\r\nat java.lang.reflect.Method.invokeNative(Native Method)\r\nat java.lang.reflect.Method.invoke(Method.java:521)\r\nat com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)\r\nat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)\r\nat dalvik.system.NativeStart.main(Native Method)\\n" // Obligatory }, "application_environment": { "phone": "android", // Device model (could be PC or Max) Obligatory "appver": "1.2", // Obligatory "appname": "com.sfalma", // Obligatory "osver": "2.3", // Obligatory "wifi_on": "true", "mobile_net_on": "true", "gps_on": "true", "screen_dpi(x:y)": "120.0:120.0", "screen:width": "240", "screen:height": "400", "screen:orientation": "normal" } } 

You can read about it here .

0
source

All Articles