Skip to content

Instantly share code, notes, and snippets.

@AndrewReitz
Created June 21, 2014 12:51
Show Gist options
  • Save AndrewReitz/46d5bcc7da2ed0319926 to your computer and use it in GitHub Desktop.
Save AndrewReitz/46d5bcc7da2ed0319926 to your computer and use it in GitHub Desktop.
Crashlytics Tree
public final class CrashlyticsTree extends HollowTree {
private static final Pattern ANONYMOUS_CLASS = Pattern.compile("\\$\\d+$");
private enum LogLevel {
INFO,
ERROR,
WARNING
}
@Override public void i(String message, Object... args) {
log(LogLevel.INFO, createTag(), formatString(message, args));
}
@Override public void i(Throwable t, String message, Object... args) {
log(LogLevel.INFO, createTag(), formatString(message, args), t);
}
@Override public void w(String message, Object... args) {
log(LogLevel.WARNING, createTag(), formatString(message, args));
}
@Override public void w(Throwable t, String message, Object... args) {
log(LogLevel.WARNING, createTag(), formatString(message, args), t);
}
@Override public void e(String message, Object... args) {
log(LogLevel.ERROR, createTag(), formatString(message, args));
}
@Override public void e(Throwable t, String message, Object... args) {
log(LogLevel.ERROR, createTag(), formatString(message, args), t);
}
private void log(LogLevel logLevel, String tag, String message) {
log(logLevel, tag, message, null);
}
private void log(LogLevel logLevel, String tag, String message, Throwable t) {
Crashlytics.log(String.format("%s/%s: %s", logLevel, tag, message));
Crashlytics.logException(t); // always log even if null, crashylitcs ignores them
}
/**
* Create a tag from the calling class (4 up).
* This is not exactly fast but then again this really shouldn't be called a lot
*/
private static String createTag() {
String tag = new Throwable().getStackTrace()[4].getClassName();
Matcher m = ANONYMOUS_CLASS.matcher(tag);
if (m.find()) {
tag = m.replaceAll("");
}
return tag.substring(tag.lastIndexOf('.') + 1);
}
private static String formatString(String message, Object... args) {
return args.length == 0 ? message : String.format(message, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment