Skip to content

Commit

Permalink
增加了异常处理和发行版自行关闭log的方案
Browse files Browse the repository at this point in the history
  • Loading branch information
kale committed Jul 4, 2016
1 parent 33068f9 commit 2e5da00
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 23 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '24'

defaultConfig {
applicationId "com.orhanobut.loggersample"
Expand All @@ -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'

}
}
123 changes: 123 additions & 0 deletions app/src/main/java/com/orhanobut/loggersample/CrashHandler.java
Original file line number Diff line number Diff line change
@@ -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, "======================================== ");
}

}
28 changes: 28 additions & 0 deletions app/src/main/java/com/orhanobut/loggersample/CrashlyticsTree.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
19 changes: 15 additions & 4 deletions app/src/main/java/com/orhanobut/loggersample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -91,7 +102,7 @@ private void largeDataTest() {
// 内部类中打log测试
///////////////////////////////////////////////////////////////////////////

public static class User {
private static class User {

private String name;

Expand All @@ -102,7 +113,7 @@ public static class User {
this.sex = sex;
}

public void show() {
void show() {
Logger.d("name:%s sex:%s", name, sex);
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion logger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '24'

defaultConfig {
minSdkVersion 15
Expand Down
2 changes: 1 addition & 1 deletion logger/src/main/java/com/orhanobut/logger/LogPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
23 changes: 10 additions & 13 deletions logger/src/main/java/com/orhanobut/logger/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 2e5da00

Please sign in to comment.