Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Last active February 24, 2016 22:51
Show Gist options
  • Select an option

  • Save ccabanero/9b7b955c5ae2d9a9472a to your computer and use it in GitHub Desktop.

Select an option

Save ccabanero/9b7b955c5ae2d9a9472a to your computer and use it in GitHub Desktop.
Android-ErrorReporting-Rollbar
Sample code for using Rollbar to report run-time exceptions ...
public class ErrorReporter {
public static void initializeErrorLogging(Context context) {
Rollbar.init(context, Configuration.ROLLBAR_CLIENT_SIDE_TOKEN, Configuration.ROLLBAR_DEPLOYMENT_STATUS);
}
/**
* For creating a string that will parse the class name, method name, and line of code for the passed in stack trace element
* @param stackTraceElement The stack trace element on the thread where an exception was thrown
* @return Returns a string with the class name, method name, and line of code
*/
private static String fetchStackTraceDetails(StackTraceElement stackTraceElement) {
String stackTraceDetails = ". Class: " + stackTraceElement.getClassName();
stackTraceDetails += "; Method: " + stackTraceElement.getMethodName();
stackTraceDetails += "; Line: " + String.valueOf(stackTraceElement.getLineNumber());
return stackTraceDetails;
}
/**
* For reporting an error to Rollbar
* @param throwable The throwable (e.g. Exception)
* @param stackTraceElement The StackTraceElement on the associated thread to allow us to fetch the Class name, Method name, and line number where exception occurred
* @param errorLevel The error level (e.g. critical, warning, info, or debug)
*/
public static void report(Throwable throwable, StackTraceElement stackTraceElement, String errorLevel) {
String message = throwable.getMessage();
message += ErrorReporter.fetchStackTraceDetails(stackTraceElement);
Rollbar.reportException(throwable, errorLevel, message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment