diff --git a/app/build.gradle b/app/build.gradle index d9bbdd9a..ece50952 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 23 - buildToolsVersion "23.0.3" + buildToolsVersion '24' defaultConfig { applicationId "com.orhanobut.loggersample" @@ -24,5 +24,4 @@ dependencies { compile 'com.android.support:appcompat-v7:23.4.0' compile project(':logger') compile 'com.jakewharton.timber:timber:4.1.1' - -} +} \ No newline at end of file diff --git a/app/src/main/java/com/orhanobut/loggersample/CrashHandler.java b/app/src/main/java/com/orhanobut/loggersample/CrashHandler.java new file mode 100644 index 00000000..b7661298 --- /dev/null +++ b/app/src/main/java/com/orhanobut/loggersample/CrashHandler.java @@ -0,0 +1,123 @@ +package com.orhanobut.loggersample; + +import android.util.Log; + +/** + * @author Kale + * @date 2016/7/1 + */ + +class CrashHandler implements Thread.UncaughtExceptionHandler { + + private static final String TAG = "CrashHandler"; + + private static CrashHandler sInstance = new CrashHandler(); + + //系统默认的异常处理(默认情况下,系统会终止当前的异常程序) + private Thread.UncaughtExceptionHandler mDefaultCrashHandler; + + static CrashHandler getInstance() { + return sInstance; + } + + //这里主要完成初始化工作 + void init() { + //获取系统默认的异常处理器 + mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); + //将当前实例设为系统默认的异常处理器 + Thread.setDefaultUncaughtExceptionHandler(this); + } + + /** + * 这个是最关键的函数,当程序中有未被捕获的异常,系统将会自动调用#uncaughtException方法 + * thread为出现未捕获异常的线程,ex为未捕获的异常,有了这个ex,我们就可以得到异常信息。 + */ + @Override + public void uncaughtException(Thread thread, Throwable ex) { + //这里可以通过网络上传异常信息到服务器,便于开发人员分析日志从而解决bug + uploadExceptionToServer(thread, ex); + //打印出当前调用栈信息 + ex.printStackTrace(); + + //如果系统提供了默认的异常处理器,则交给系统去结束我们的程序,否则就由我们自己结束自己 + if (mDefaultCrashHandler != null) { + mDefaultCrashHandler.uncaughtException(thread, ex); + } + + } + + private void uploadExceptionToServer(Thread thread, Throwable ex) { + Log.d(TAG, "======================================== "); + System.out.println(" .;jLGEEEEEEEEEEEEEEEt \n" + + " .;jLGEEEEEEEEEEEEEEEt \n" + + " .;jLGEEEEEEEEEEEEEEEi \n" + + " .;jLDEEEEEEEEEEEEEEEt \n" + + " .;jLGEEEEEEEEEEEEEEEi \n" + + " :;jLGEEEEEEEEEEEEEEE; \n" + + " :;jLDEEEEEEEEEEEEEEEi \n" + + " :;jLGEEEEEEEEEEEEEEE; \n" + + " :;jLDEEEEEEEEEEEEEEE, \n" + + " :;jLDEEEEEEEEEEEEEEE; \n" + + " :;jLDEEEEEEEEEEEEEEE, \n" + + " :;jLDEEEEEEEEEEEEEEE, \n" + + " :;jLDEEEEEEEEEEEEEEE; \n" + + " :;jLDEEEEEEEEEEEEEEE: \n" + + " :;jLDEEEEEEEEEEEEEEE. \n" + + " :;jLDEEEEEEEEEEEEEEE, \n" + + " .,tLGEEEEEEEEEEEEEEEL \n" + + " .,tLGEEEEEEEEEEEEEEEf \n" + + " .D## .,tLGEEEEEEEEEEEEEEEL\n" + + " ,W##### .,tLGEEEEEEEEEEEEEEE\n" + + " t######### .,tLGEEEEEEEEEEEEEE\n" + + " L############ .,tLGEEEEEEEEEEEEE\n" + + " EG, j######## .,tLGEEEEEEEEEEEE\n" + + " ;####### .,tLGEEEEEEEEEEE\n" + + " ####### .,tLGEEEEEEEEEE\n" + + " ####### .,tLGEEEEEEEEE\n" + + " ####### .;tLGEEEEEEEE\n" + + " ####### .;jLGEEEEEEE\n" + + " ####### .;jLGEEEEEE\n" + + " ####### .;jLGEEEEE\n" + + " ####### .;jLGEEEE\n" + + " ####### .;jLGEEE\n" + + " ####### .;jLGEE\n" + + " ####### .;jLGE\n" + + " ####### .;jLG\n" + + " ####### ################# .;tj\n" + + " ####### ;j###########Ef;; .,,\n" + + " ####### #######i ..\n" + + " ####### #####; \n" + + " ####### ####W \n" + + " ####### ;####t \n" + + " ####### E#### ##### \n" + + " ####### ####K f##### \n" + + " ####### t####; #####E \n" + + " ####### W#### #####t \n" + + " ####### .####D ##### \n" + + " ####### f####, ##### \n" + + " ####### ###### L#######L ##### ;#######j \n" + + " ############### ,###########j ##### ###########. \n" + + " #######K######## #############j ##### .############# \n" + + " ####### K######## ####t .D##### ####K #####j ,#####j \n" + + " ####### K######## E## ;####j ####D W#### ####W \n" + + " ####### W######## tf ####D ####D ####: ####W \n" + + " ####### ######### iW##### ####D L#### .W####t \n" + + " ####### ######### i########## ####D ##### ;D####### \n" + + " ####### ######### ;############ ####D ##############K \n" + + " ####### ######### D#####KjE##### ##### ############K. \n" + + " ####### ######### L####G ##### ##### #########G, \n" + + " ####### ######### ####D ##### ##### ######L \n" + + " ####### ######### j#### ##### ##### #####; tG \n" + + " ####### ######### D####; ##### ##### E####; ## \n" + + " ####### ######### j#####. ##### ##### .#####j ;##D \n" + + " .####### ;#########; #######DD###### #####; W#######DD#####: \n" + + " ########W .###########: t############## #####L #############K \n" + + " ;;fW###########Gi;; ;t################# .W############; ###### W##########E \n" + + " ################### ################### ;jD#####DDj i##### ,W######K. "); + + Log.d(TAG, "thread name: " + thread.getName()); + Log.d(TAG, "throwable msg: " + ex.getMessage()); + Log.d(TAG, "======================================== "); + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/orhanobut/loggersample/CrashlyticsTree.java b/app/src/main/java/com/orhanobut/loggersample/CrashlyticsTree.java new file mode 100644 index 00000000..f9cf62c7 --- /dev/null +++ b/app/src/main/java/com/orhanobut/loggersample/CrashlyticsTree.java @@ -0,0 +1,28 @@ +package com.orhanobut.loggersample; + +import android.support.annotation.Nullable; +import android.util.Log; + +import timber.log.Timber; + +/** + * @author Kale + * @date 2016/5/23 + */ +public class CrashlyticsTree extends Timber.Tree { + + @Override + protected void log(int priority, @Nullable String tag, @Nullable String message, @Nullable Throwable t) { + if (priority == Log.VERBOSE || priority == Log.DEBUG || priority == Log.INFO) { + return; + } + + if (t == null && message != null) { + //Crashlytics.logException(new Exception(message)); + } else if (t != null && message != null) { +// Crashlytics.logException(new Exception(message, t)); + } else if (t != null) { +// Crashlytics.logException(t); + } + } +} diff --git a/app/src/main/java/com/orhanobut/loggersample/MainActivity.java b/app/src/main/java/com/orhanobut/loggersample/MainActivity.java index a18de73d..e0bf910b 100644 --- a/app/src/main/java/com/orhanobut/loggersample/MainActivity.java +++ b/app/src/main/java/com/orhanobut/loggersample/MainActivity.java @@ -6,6 +6,7 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; +import android.widget.TextView; import java.util.Arrays; @@ -16,13 +17,23 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + CrashHandler.getInstance().init(); + + TextView view = new TextView(this); +// view.setText(12345); // 如果出现了崩溃,那么就会调用崩溃处理机制 + Logger.initialize( - Settings.getInstance() + new Settings() .isShowMethodLink(true) .isShowThreadInfo(false) .setMethodOffset(0) .setLogPriority(BuildConfig.DEBUG ? Log.VERBOSE : Log.ASSERT) ); + + if (!BuildConfig.DEBUG) { + // for release + Logger.plant(new CrashlyticsTree()); + } levTest(); objTest(); @@ -38,7 +49,7 @@ private void levTest() { Logger.d(test); //Logger.d(test, "s"); // Note:incorrect - + Logger.t("Custom Tag"); Logger.t("Custom Tag").w("logger with custom tag"); try { Class.forName("kale"); @@ -91,7 +102,7 @@ private void largeDataTest() { // 内部类中打log测试 /////////////////////////////////////////////////////////////////////////// - public static class User { + private static class User { private String name; @@ -102,7 +113,7 @@ public static class User { this.sex = sex; } - public void show() { + void show() { Logger.d("name:%s sex:%s", name, sex); } } diff --git a/build.gradle b/build.gradle index 0632aac9..eaf1105c 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:2.2.0-alpha4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/logger/build.gradle b/logger/build.gradle index 7faf0a4e..fc8b9362 100644 --- a/logger/build.gradle +++ b/logger/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' android { compileSdkVersion 23 - buildToolsVersion "23.0.3" + buildToolsVersion '24' defaultConfig { minSdkVersion 15 diff --git a/logger/src/main/java/com/orhanobut/logger/LogPrinter.java b/logger/src/main/java/com/orhanobut/logger/LogPrinter.java index b49feea1..80430e14 100644 --- a/logger/src/main/java/com/orhanobut/logger/LogPrinter.java +++ b/logger/src/main/java/com/orhanobut/logger/LogPrinter.java @@ -28,7 +28,7 @@ public final class LogPrinter extends Timber.DebugTree { private static final String PROPERTY = System.getProperty("line.separator"); - public LogPrinter(Settings settings) { + LogPrinter(Settings settings) { this.settings = settings; } diff --git a/logger/src/main/java/com/orhanobut/logger/Settings.java b/logger/src/main/java/com/orhanobut/logger/Settings.java index a79a12e6..db32a3a9 100644 --- a/logger/src/main/java/com/orhanobut/logger/Settings.java +++ b/logger/src/main/java/com/orhanobut/logger/Settings.java @@ -8,21 +8,13 @@ */ public class Settings { - protected int methodOffset = 0; + int methodOffset = 0; - protected boolean showMethodLink = true; + boolean showMethodLink = true; - protected boolean showThreadInfo = false; + boolean showThreadInfo = false; - protected int priority = Log.VERBOSE; - - public static Settings getInstance() { - return new Settings(); - } - - private Settings() { - - } + int priority = Log.VERBOSE; public Settings setMethodOffset(int methodOffset) { this.methodOffset = methodOffset; @@ -40,7 +32,12 @@ public Settings isShowMethodLink(boolean showMethodLink) { } /** - * @param priority one of {@link Log#VERBOSE},{@link Log#DEBUG},{@link Log#INFO},{@link Log#WARN},{@link Log#ERROR} + * @param priority one of + * {@link Log#VERBOSE}, + * {@link Log#DEBUG}, + * {@link Log#INFO}, + * {@link Log#WARN}, + * {@link Log#ERROR} */ public Settings setLogPriority(int priority) { this.priority = priority;