Permalink
Browse files

增加了模拟crash的方法

  • Loading branch information...
kale
kale committed Jul 5, 2016
1 parent 2e5da00 commit 5b102b4623b8f92c56b2f05dd7b22af1e5f311d8
@@ -30,35 +30,78 @@ allprojects {
Add the dependency
> compile 'com.github.tianzhijiexian:logger:[Latest release](https://github.com/tianzhijiexian/logger/releases)'
and also compile https://github.com/JakeWharton/timber
### Logger
```
D/MainActivity: ║first
D/MainActivity: ║second
D/MainActivity: ║third ==> levTest(MainActivity.java:62)
D/MainActivity: ╚═══════════════════════════
D/MainActivity: ║just test ==> test(MainActivity.java:67)
D/MainActivity: ╚═══════════════════════════
D/MainActivity: ║User{name=jack, sex=f, $change=Object} ==> objTest(MainActivity.java:72)
D/MainActivity: ╚═══════════════════════════
D/MainActivity: ║ArrayList size = 3 [
D/MainActivity: ║[0]:kale,
D/MainActivity: ║[1]:jack,
D/MainActivity: ║[2]:tony
D/MainActivity: ║
D/MainActivity: ║] ==> objTest(MainActivity.java:74)
D/MainActivity: ╚═══════════════════════════
D/MainActivity: ║String[3] {
D/MainActivity: ║[Android, ios, wp]
D/MainActivity: ║} ==> objTest(MainActivity.java:76)
D/MainActivity: ╚═══════════════════════════
D/MainActivity: ║double[4][5] {
D/MainActivity: ║[1.2, 1.6, 1.7, 30.0, 33.0]
D/MainActivity: ║[1.2, 1.6, 1.7, 30.0, 33.0]
D/MainActivity: ║[1.2, 1.6, 1.7, 30.0, 33.0]
D/MainActivity: ║[1.2, 1.6, 1.7, 30.0, 33.0]
D/MainActivity: ║
D/MainActivity: ║} ==> objTest(MainActivity.java:83)
D/MainActivity: ╚═══════════════════════════
D/MainActivity: ║{
D/MainActivity: ║ "widget": {
D/MainActivity: ║ "debug": "on",
D/MainActivity: ║ "window": {
D/MainActivity: ║ "title": "Sample Konfabulator Widget",
D/MainActivity: ║ "name": "main_window",
D/MainActivity: ║ "width": 500,
D/MainActivity: ║ "height": 500
D/MainActivity: ║ }
D/MainActivity: ║ }
D/MainActivity: ║} ==> jsonTest(MainActivity.java:87)
D/MainActivity: ╚═══════════════════════════
```
![](./images/obj.png)
### Usage
```java
Logger.d("hello"); // debug
![](./images/json.png)
Logger.e("hello"); // error
![](./images/large_data.png)
Logger.w("hello"); // warnning
![](./images/error.png)
Logger.v("hello"); // verbose
Logger.wtf("hello"); // what the fuck
Logger.json(JSON_CONTENT); // json
Logger.xml(XML_CONTENT); // xml
### Usage
```java
Logger.d("hello");
Logger.e("hello");
Logger.w("hello");
Logger.v("hello");
Logger.wtf("hello");
Logger.json(JSON_CONTENT);
Logger.xml(XML_CONTENT);
logger.object(...); // bean/map/Collection...
Logger.t("Custom Tag").w("logger with custom tag");
try {
Class.forName("kale");
} catch (ClassNotFoundException e) {
Logger.e(e, "something happened"); // exception
}
Logger.d("first\nsecond\nthird");
Logger.d("first\nsecond\nthird"); // third line
```
### Settings (optional)
@@ -71,7 +114,7 @@ public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
Logger.initialize(
Settings.getInstance()
new Settings()
.isShowMethodLink(true)
.isShowThreadInfo(false)
.setMethodOffset(0)
@@ -18,7 +18,7 @@ protected void log(int priority, @Nullable String tag, @Nullable String message,
}
if (t == null && message != null) {
//Crashlytics.logException(new Exception(message));
// Crashlytics.logException(new Exception(message));
} else if (t != null && message != null) {
// Crashlytics.logException(new Exception(message, t));
} else if (t != null) {
@@ -69,3 +69,6 @@
" }" +
"}} ";
}
@@ -4,6 +4,7 @@
import com.orhanobut.logger.Settings;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
@@ -17,11 +18,6 @@
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CrashHandler.getInstance().init();
TextView view = new TextView(this);
// view.setText(12345); // 如果出现了崩溃,那么就会调用崩溃处理机制
Logger.initialize(
new Settings()
.isShowMethodLink(true)
@@ -34,12 +30,18 @@ protected void onCreate(Bundle savedInstanceState) {
// for release
Logger.plant(new CrashlyticsTree());
}
levTest();
objTest();
jsonTest();
locationTest();
largeDataTest();
Logger.getSettings().setLogPriority(Log.ASSERT); // close log
CrashHandler.getInstance().init(); // 崩溃检测处理器
// setRes(123); // 模拟崩溃
}
private void levTest() {
@@ -117,4 +119,28 @@ void show() {
Logger.d("name:%s sex:%s", name, sex);
}
}
///////////////////////////////////////////////////////////////////////////
// crash
///////////////////////////////////////////////////////////////////////////
/**
* 这里模拟后端给客户端传值的情况。
*
* 这里的id来自外部输入,如果外部输入的值有问题,那么就可能崩溃。
* 但理论上是不会有数据异常的,为了不崩溃,这里加try-catch
*/
private void setRes(@StringRes int resId) {
TextView view = new TextView(this);
try {
view.setText(resId); // 如果出现了崩溃,那么就会调用崩溃处理机制
} catch (Exception e) {
// 防御了崩溃
e.printStackTrace();
// 把崩溃的异常和当前的上下文通过log系统分发
Logger.e(e, "res id = " + resId);
}
}
}
BIN -217 KB images/error.png
Binary file not shown.
BIN -157 KB images/json.png
Binary file not shown.
BIN -198 KB images/large_data.png
Binary file not shown.
BIN -84.5 KB images/obj.png
Binary file not shown.
BIN +117 KB images/simple.png
Binary file not shown.

0 comments on commit 5b102b4

Please sign in to comment.