新增捕获并上传异常日志的功能

This commit is contained in:
zlzw 2022-10-22 13:23:57 +08:00
parent 77f5d89f28
commit 38a269ae1c
8 changed files with 114 additions and 5 deletions

View File

@ -148,6 +148,9 @@
<meta-data <meta-data
android:name="IS_GOOGLE_PLAY" android:name="IS_GOOGLE_PLAY"
android:value="${isGooglePlay}" /> android:value="${isGooglePlay}" />
<meta-data
android:name="IS_UPLOAD_ERROR_LOG"
android:value="${isUploadLog}" />
<meta-data <meta-data
android:name="com.openinstall.APP_KEY" android:name="com.openinstall.APP_KEY"

View File

@ -99,7 +99,7 @@ public class AppContext extends CommonAppContext {
@Override @Override
public void onActivityDestroyed(@NonNull Activity activity) { public void onActivityDestroyed(@NonNull Activity activity) {
for (WeakReference<Activity> reference : activities) { for (WeakReference<Activity> reference : activities) {
if(reference.get()==activity){ if (reference.get() == activity) {
activities.remove(reference); activities.remove(reference);
return; return;
} }
@ -297,7 +297,7 @@ public class AppContext extends CommonAppContext {
e.printStackTrace(); e.printStackTrace();
//闪退后finish所有Activity并且杀死进程 //闪退后finish所有Activity并且杀死进程
for (WeakReference<Activity> activity : activities) { for (WeakReference<Activity> activity : activities) {
if (activity != null) { if (activity != null && activity.get() != null) {
activity.get().finish(); activity.get().finish();
} }
} }

View File

@ -6,6 +6,8 @@ import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.yunbao.common.utils.FileUtil; import com.yunbao.common.utils.FileUtil;
import com.yunbao.common.utils.SpUtil;
import java.io.File; import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
@ -94,7 +96,7 @@ public class NeverCrashUtils {
} }
e.printStackTrace(); e.printStackTrace();
Toast.makeText(application, "发生闪退", Toast.LENGTH_SHORT).show(); Toast.makeText(application, "发生闪退", Toast.LENGTH_SHORT).show();
// FileUtil.saveStringToFile(new File(application.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()),throwableToString(e),"error.log"); FileUtil.saveStringToFile(new File(application.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()),throwableToString(e),"error.log");
getMainCrashHandler().mainException(Looper.getMainLooper().getThread(), e); getMainCrashHandler().mainException(Looper.getMainLooper().getThread(), e);
} }
@ -124,8 +126,13 @@ public class NeverCrashUtils {
} }
private static String throwableToString(Throwable e) { private static String throwableToString(Throwable e) {
StringWriter writer=new StringWriter(); StringWriter writer=new StringWriter();
writer.write(SpUtil.getInstance().getStringValue(SpUtil.USER_INFO));
writer.write("\n");
writer.write("time="+System.currentTimeMillis());
writer.write("\n");
writer.write("[ERROR]");
PrintWriter printWriter=new PrintWriter(writer); PrintWriter printWriter=new PrintWriter(writer);
e.printStackTrace(printWriter); e.printStackTrace(printWriter);
return "[ERROR]"+writer.toString(); return writer.toString();
} }
} }

View File

@ -27,6 +27,7 @@ import com.adjust.sdk.AdjustEvent;
import com.alibaba.android.arouter.facade.annotation.Route; import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.shayu.phonelive.AppContext;
import com.tencent.rtmp.ITXLivePlayListener; import com.tencent.rtmp.ITXLivePlayListener;
import com.tencent.rtmp.TXLiveBase; import com.tencent.rtmp.TXLiveBase;
import com.tencent.rtmp.TXLiveConstants; import com.tencent.rtmp.TXLiveConstants;
@ -48,6 +49,7 @@ import com.yunbao.common.manager.IMLoginManager;
import com.yunbao.common.manager.imrongcloud.RongcloudIMManager; import com.yunbao.common.manager.imrongcloud.RongcloudIMManager;
import com.yunbao.common.utils.DownloadUtil; import com.yunbao.common.utils.DownloadUtil;
import com.yunbao.common.utils.L; import com.yunbao.common.utils.L;
import com.yunbao.common.utils.LogUtil;
import com.yunbao.common.utils.MD5Util; import com.yunbao.common.utils.MD5Util;
import com.yunbao.common.utils.RouteUtil; import com.yunbao.common.utils.RouteUtil;
import com.yunbao.common.utils.SpUtil; import com.yunbao.common.utils.SpUtil;
@ -137,6 +139,7 @@ public class LauncherActivity extends AppCompatActivity implements View.OnClickL
} }
}; };
mHandler.sendEmptyMessageDelayed(WHAT_GET_CONFIG, 1000); mHandler.sendEmptyMessageDelayed(WHAT_GET_CONFIG, 1000);
LogUtil.pushError(AppContext.sInstance);
} }

View File

@ -33,6 +33,8 @@ public class CommonAppConfig {
public static final String HOST = getMetaDataString("SERVER_HOST"); public static final String HOST = getMetaDataString("SERVER_HOST");
//是否使用谷歌支付 //是否使用谷歌支付
public static final boolean IS_GOOGLE_PLAY = getMetaDataBoolean("IS_GOOGLE_PLAY"); public static final boolean IS_GOOGLE_PLAY = getMetaDataBoolean("IS_GOOGLE_PLAY");
//是否开启上报错误日志功能
public static final boolean IS_UPLOAD_ERROR_LOG = getMetaDataBoolean("IS_UPLOAD_ERROR_LOG");
//外部sd卡 //外部sd卡
public static final String DCMI_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath(); public static final String DCMI_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();

View File

@ -2,9 +2,11 @@ package com.yunbao.common.utils;
import android.media.MediaMetadataRetriever; import android.media.MediaMetadataRetriever;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter; import java.io.PrintWriter;
/** /**
@ -32,6 +34,26 @@ public class FileUtil {
} }
} }
/**
* 从文件读取字符串
*/
public static String loadStringToFile(File file){
if(file.exists()){
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
StringBuilder builder=new StringBuilder();
String tmp;
while ((tmp=reader.readLine())!=null){
builder.append(tmp).append("\n");
}
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
/** /**
* 获取 视频 音频 时长 * 获取 视频 音频 时长
* @param path 视频 音频 文件路径 * @param path 视频 音频 文件路径

View File

@ -1,9 +1,24 @@
package com.yunbao.common.utils; package com.yunbao.common.utils;
import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okserver.OkUpload;
import com.lzy.okserver.upload.UploadListener;
import com.lzy.okserver.upload.UploadTask;
import com.yunbao.common.CommonAppConfig;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/** /**
* Created by cxf on 2019/6/20. * Created by cxf on 2019/6/20.
@ -31,4 +46,59 @@ public class LogUtil {
} }
} }
} }
public static void pushError(Context context){
if(!CommonAppConfig.IS_UPLOAD_ERROR_LOG){
return;
}
File file = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()+File.separator+"error.log");
if(file.exists()){
Log.i("异常上传", "pushError: 准备上传文件");
String err = FileUtil.loadStringToFile(file);
PostRequest<String> request = OkGo.<String>post("https://newht.yaoulive.com/index.php/api/error_log/report")
.params("log", err)
.params("uid", CommonAppConfig.getInstance().getUid())
.params("time", new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss", Locale.CHINA).format(new Date()))
.params("isGoogle",CommonAppConfig.IS_GOOGLE_PLAY)
.params("appVersion",CommonAppConfig.getInstance().getVersion())
.tag("errorLog")
.converter(new StringCallback() {
String TAG = "异常上传";
@Override
public void onSuccess(Response<String> response) {
Log.i(TAG, "onSuccess: " + response.message());
file.delete();
}
});
UploadTask<String> task = OkUpload.request("errorLog", request)
.save()
.register(new UploadListener<String>("errorLog") {
@Override
public void onStart(Progress progress) {
}
@Override
public void onProgress(Progress progress) {
}
@Override
public void onError(Progress progress) {
}
@Override
public void onFinish(String s, Progress progress) {
file.delete();
}
@Override
public void onRemove(Progress progress) {
}
});
task.start();
}
}
} }

View File

@ -25,6 +25,8 @@ ext {
baiduAppSecretKey: "nEVSgmuGpU0pjPr6VleEGGAl0hzGW52S", baiduAppSecretKey: "nEVSgmuGpU0pjPr6VleEGGAl0hzGW52S",
// true表示谷歌支付 false // true表示谷歌支付 false
isGooglePlay : false isGooglePlay : false,
//
isUploadLog : false
] ]
} }