浏览代码

增加错误日志上报功能

Pq 11 月之前
父节点
当前提交
4012c7cedb

+ 79 - 0
BaseLibrary/src/main/java/com/cooleshow/base/bean/ErrorInfoBean.java

@@ -0,0 +1,79 @@
+package com.cooleshow.base.bean;
+
+/**
+ * Author by pq, Date on 2024/4/26.
+ */
+public class ErrorInfoBean {
+    private String clientType;//TEACHER,STUDENT,SCHOOL,BACKEND
+    private String phone;
+    private String appType;//IOS,ANDROID,HARMONY
+    private String content;
+    private String exceptionType;//ERROR,RECORD
+    private String exceptionTime;
+    private String deviceType;
+    private String deviceVersion;
+
+    public String getClientType() {
+        return clientType;
+    }
+
+    public void setClientType(String clientType) {
+        this.clientType = clientType;
+    }
+
+    public String getPhone() {
+        return phone;
+    }
+
+    public void setPhone(String phone) {
+        this.phone = phone;
+    }
+
+    public String getAppType() {
+        return appType;
+    }
+
+    public void setAppType(String appType) {
+        this.appType = appType;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public String getExceptionType() {
+        return exceptionType;
+    }
+
+    public void setExceptionType(String exceptionType) {
+        this.exceptionType = exceptionType;
+    }
+
+    public String getExceptionTime() {
+        return exceptionTime;
+    }
+
+    public void setExceptionTime(String exceptionTime) {
+        this.exceptionTime = exceptionTime;
+    }
+
+    public String getDeviceType() {
+        return deviceType;
+    }
+
+    public void setDeviceType(String deviceType) {
+        this.deviceType = deviceType;
+    }
+
+    public String getDeviceVersion() {
+        return deviceVersion;
+    }
+
+    public void setDeviceVersion(String deviceVersion) {
+        this.deviceVersion = deviceVersion;
+    }
+}

+ 20 - 0
BaseLibrary/src/main/java/com/cooleshow/base/constanst/ErrorConstants.java

@@ -0,0 +1,20 @@
+package com.cooleshow.base.constanst;
+
+/**
+ * Author by pq, Date on 2024/4/28.
+ */
+public class ErrorConstants {
+    public static final String APP_TYPE_ANDROID = "ANDROID";
+    public static final String ERROR_TYPE_ERROR = "ERROR";
+    public static final String ERROR_TYPE_RECORD = "RECORD";
+    public static final String ERROR_CLIENT_TYPE_STUDENT= "STUDENT";
+    public static final String ERROR_CLIENT_TYPE_TEACHER= "TEACHER";
+
+    public static String getErrorMsg(String errorCode,String errorMsg){
+        return String.format("errorCode:%s-errorMsg:%s", errorCode, errorMsg);
+    }
+
+    public static String getErrorMsg(int errorCode,CharSequence errorMsg){
+        return getErrorMsg(String.valueOf(errorCode),errorMsg.toString());
+    }
+}

+ 12 - 0
BaseLibrary/src/main/java/com/cooleshow/base/constanst/ErrorType.java

@@ -0,0 +1,12 @@
+package com.cooleshow.base.constanst;
+
+/**
+ * Author by pq, Date on 2024/4/26.
+ * 定义部分报错 出错的埋点位置,后续待补充
+ */
+public class ErrorType {
+    public static final String NET_INTERFACES_ERROR="NET_INTERFACES_ERROR";//接口请求过程出错,可能包含网络错误,解析错误,和服务端返回的错误
+    public static final String HTML_ERROR="HTML_ERROR";//H5页面错误
+
+
+}

+ 9 - 0
BaseLibrary/src/main/java/com/cooleshow/base/data/api/UploadApi.java

@@ -138,4 +138,13 @@ public interface UploadApi {
      */
     @GET("{group_name}" + "/imLiveBroadcastRoom/queryRoom")
     Observable<BaseResponse<LiveRoomInfoBean>> getLiveRoomInfo(@Path("group_name") String group_name,@Query("roomUid") String roomUid);
+
+    /**
+     * 上传错误信息
+     *
+     * @param
+     * @return
+     */
+    @POST(AUTH_GROUP + "sysExceptionLog/save")
+    Observable<BaseResponse<Object>> uploadAppLog(@Body RequestBody requestBody);
 }

+ 3 - 0
BaseLibrary/src/main/java/com/cooleshow/base/utils/ErrorParse.java

@@ -4,8 +4,10 @@ import android.app.Activity;
 
 import com.alibaba.android.arouter.launcher.ARouter;
 import com.cooleshow.base.common.AppManager;
+import com.cooleshow.base.constanst.ErrorType;
 import com.cooleshow.base.data.net.ApiException;
 import com.cooleshow.base.router.RouterPath;
+import com.cooleshow.base.utils.helper.LogUploadManager;
 
 /**
  * Author by pq, Date on 2022/5/9.
@@ -39,6 +41,7 @@ public class ErrorParse {
                 ToastUtil.getInstance().showShort(apiException.getErrmsg());
             }
         } else {
+            LogUploadManager.getInstance().uploadRecordLog(ErrorType.NET_INTERFACES_ERROR,throwable.toString());
             throwable.printStackTrace();
             ToastUtil.getInstance().showShort("网络或服务错误,请重试");
         }

+ 136 - 0
BaseLibrary/src/main/java/com/cooleshow/base/utils/helper/LogUploadManager.java

@@ -0,0 +1,136 @@
+package com.cooleshow.base.utils.helper;
+
+import android.app.Activity;
+import android.content.ComponentName;
+
+import com.cooleshow.base.bean.ErrorInfoBean;
+import com.cooleshow.base.common.AppManager;
+import com.cooleshow.base.common.BaseConstant;
+import com.cooleshow.base.constanst.ErrorConstants;
+import com.cooleshow.base.data.api.UploadApi;
+import com.cooleshow.base.data.net.BaseResponse;
+import com.cooleshow.base.data.net.RetrofitFactory;
+import com.cooleshow.base.utils.AppUtils;
+import com.cooleshow.base.utils.DeviceUtils;
+import com.cooleshow.base.utils.GsonUtils;
+import com.cooleshow.base.utils.RequestBodyUtil;
+import com.cooleshow.base.utils.SPUtils;
+import com.cooleshow.base.utils.TimeUtils;
+
+import org.json.JSONObject;
+
+import java.util.ArrayList;
+
+import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
+import io.reactivex.rxjava3.annotations.NonNull;
+import io.reactivex.rxjava3.core.Observable;
+import io.reactivex.rxjava3.core.Observer;
+import io.reactivex.rxjava3.disposables.Disposable;
+import io.reactivex.rxjava3.schedulers.Schedulers;
+import okhttp3.RequestBody;
+
+/**
+ * Author by pq, Date on 2024/4/26.
+ */
+public class LogUploadManager {
+    private static LogUploadManager instance;
+
+    private LogUploadManager() {
+    }
+
+    public static LogUploadManager getInstance() {
+        if (instance == null) {
+            synchronized (LogUploadManager.class) {
+                if (instance == null) {
+                    instance = new LogUploadManager();
+                }
+            }
+        }
+        return instance;
+    }
+
+    public void uploadErrorLog(String errorPosType, String errorMsg) {
+        uploadLog(ErrorConstants.ERROR_TYPE_ERROR, errorPosType, errorMsg);
+    }
+
+    public void uploadRecordLog(String errorPosType, String errorMsg) {
+        uploadLog(ErrorConstants.ERROR_TYPE_RECORD, errorPosType, errorMsg);
+    }
+
+    public void uploadLog(String errorType, String errorPosType, String errorMsg) {
+        try {
+            ErrorInfoBean errorInfoBean = new ErrorInfoBean();
+            errorInfoBean.setPhone(getPhone());
+            errorInfoBean.setContent(getContent(errorPosType, errorMsg));
+            errorInfoBean.setClientType(getClientType());
+            errorInfoBean.setAppType(ErrorConstants.APP_TYPE_ANDROID);
+            errorInfoBean.setExceptionType(errorType);
+            errorInfoBean.setDeviceType(DeviceUtils.getDeviceBrand() + "_" + DeviceUtils.getDeviceDevice());
+            errorInfoBean.setDeviceVersion(String.valueOf(DeviceUtils.getDeviceSDK()));
+            errorInfoBean.setExceptionTime(TimeUtils.getNowString());
+            ArrayList<ErrorInfoBean> list = new ArrayList();
+            list.add(errorInfoBean);
+            upload(list);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private String getClientType() {
+        if (BaseConstant.isStudentClient()) {
+            return ErrorConstants.ERROR_CLIENT_TYPE_STUDENT;
+        }
+        return ErrorConstants.ERROR_CLIENT_TYPE_TEACHER;
+    }
+
+    public void upload(ArrayList<ErrorInfoBean> list) {
+        String s = GsonUtils.toJson(list);
+        RequestBody requestBody = RequestBodyUtil.convertToRequestBodyJson(s);
+        Observable<BaseResponse<Object>> observable = RetrofitFactory.Companion.getInstance().create(UploadApi.class).uploadAppLog(requestBody);
+        observable.subscribeOn(Schedulers.newThread())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe(new Observer<BaseResponse<Object>>() {
+                    @Override
+                    public void onSubscribe(@NonNull Disposable d) {
+
+                    }
+
+                    @Override
+                    public void onNext(@NonNull BaseResponse<Object> response) {
+                    }
+
+                    @Override
+                    public void onError(@NonNull Throwable e) {
+                        e.printStackTrace();
+                    }
+
+                    @Override
+                    public void onComplete() {
+
+                    }
+                });
+    }
+
+    private String getPhone() {
+        String phone = SPUtils.getInstance().getString("phone");
+        return phone;
+    }
+
+    private String getContent(String errorType, String errorMsg) {
+        JSONObject jsonObject = new JSONObject();
+        try {
+            Activity activity = AppManager.Companion.getInstance().currentActivity();
+            if (activity != null) {
+                ComponentName componentName = activity.getComponentName();
+                String className = componentName.getClassName();
+                jsonObject.put("className", className);
+            }
+            jsonObject.put("errorType", errorType);
+            jsonObject.put("appVersion", AppUtils.getAppVersionName());
+            jsonObject.put("errorMsg", errorMsg);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return jsonObject.toString();
+    }
+}

+ 2 - 2
student/build.gradle

@@ -18,8 +18,8 @@ android {
         applicationId "com.cooleshow.student"
         minSdkVersion 21
         targetSdkVersion 30
-        versionCode 161
-        versionName "1.6.1"
+        versionCode 162
+        versionName "1.6.2"
 
         ndk {
             abiFilters "armeabi-v7a", "arm64-v8a"

+ 6 - 0
student/src/main/java/com/cooleshow/student/ui/main/MallFragment.java

@@ -43,6 +43,8 @@ import com.cooleshow.base.BuildConfig;
 import com.cooleshow.base.bean.WxPayResult;
 import com.cooleshow.base.common.WebConstants;
 import com.cooleshow.base.constanst.Constants;
+import com.cooleshow.base.constanst.ErrorConstants;
+import com.cooleshow.base.constanst.ErrorType;
 import com.cooleshow.base.data.net.RetrofitClientNoToken;
 import com.cooleshow.base.ui.fragment.BaseMVPFragment;
 import com.cooleshow.base.utils.AppUtils;
@@ -53,6 +55,7 @@ import com.cooleshow.base.utils.ToastUtil;
 import com.cooleshow.base.utils.UiUtils;
 import com.cooleshow.base.utils.Utils;
 import com.cooleshow.base.utils.helper.GlideEngine;
+import com.cooleshow.base.utils.helper.LogUploadManager;
 import com.cooleshow.base.utils.helper.PermissionTipHelper;
 import com.cooleshow.base.widgets.DialogUtil;
 import com.cooleshow.student.R;
@@ -850,6 +853,9 @@ public class MallFragment extends BaseMVPFragment<FragmentMallBinding, ShopMallP
                     LOG.i("onReceivedError:" + error.getDescription());
                     if (error.getErrorCode() == WebViewClient.ERROR_HOST_LOOKUP || error.getErrorCode() == WebViewClient.ERROR_CONNECT) {
                         showLoadErrorView();
+                    } else {
+                        String errorMsg = "url:" + request.getUrl().toString() + " " + error.getDescription();
+                        LogUploadManager.getInstance().uploadErrorLog(ErrorType.HTML_ERROR, ErrorConstants.getErrorMsg(error.getErrorCode(), errorMsg));
                     }
                 }
             }

+ 6 - 0
student/src/main/java/com/cooleshow/student/ui/web/HtmlActivity.java

@@ -55,6 +55,8 @@ import com.cooleshow.base.bean.WxPayResult;
 import com.cooleshow.base.common.WebApi;
 import com.cooleshow.base.common.WebConstants;
 import com.cooleshow.base.constanst.Constants;
+import com.cooleshow.base.constanst.ErrorConstants;
+import com.cooleshow.base.constanst.ErrorType;
 import com.cooleshow.base.constanst.StyleConfig;
 import com.cooleshow.base.constanst.UploadConstants;
 import com.cooleshow.base.data.auth.Https;
@@ -78,6 +80,7 @@ import com.cooleshow.base.utils.WebParamsUtils;
 import com.cooleshow.base.utils.helper.CommonShareHelper;
 import com.cooleshow.base.utils.helper.DownloadHelper;
 import com.cooleshow.base.utils.helper.GlideEngine;
+import com.cooleshow.base.utils.helper.LogUploadManager;
 import com.cooleshow.base.utils.helper.PermissionTipHelper;
 import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
 import com.cooleshow.base.utils.helper.WebLoadFileHelper;
@@ -1228,6 +1231,9 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
                     LOG.i("onReceivedError:" + error.getDescription());
                     if (error.getErrorCode() == WebViewClient.ERROR_HOST_LOOKUP || error.getErrorCode() == WebViewClient.ERROR_CONNECT) {
                         showLoadErrorView();
+                    } else {
+                        String errorMsg = "url:" + request.getUrl().toString() + " " + error.getDescription();
+                        LogUploadManager.getInstance().uploadErrorLog(ErrorType.HTML_ERROR, ErrorConstants.getErrorMsg(error.getErrorCode(), errorMsg));
                     }
                 }
             }

+ 2 - 2
teacher/build.gradle

@@ -20,8 +20,8 @@ android {
         applicationId "com.cooleshow.teacher"
         minSdkVersion 21
         targetSdkVersion 30
-        versionCode 161
-        versionName "1.6.1"
+        versionCode 162
+        versionName "1.6.2"
         ndk {
             abiFilters "armeabi-v7a", "arm64-v8a"
         }

+ 6 - 0
teacher/src/main/java/com/cooleshow/teacher/ui/main/MallFragment.java

@@ -43,6 +43,8 @@ import com.cooleshow.base.BuildConfig;
 import com.cooleshow.base.bean.WxPayResult;
 import com.cooleshow.base.common.WebConstants;
 import com.cooleshow.base.constanst.Constants;
+import com.cooleshow.base.constanst.ErrorConstants;
+import com.cooleshow.base.constanst.ErrorType;
 import com.cooleshow.base.constanst.StyleConfig;
 import com.cooleshow.base.data.net.RetrofitClientNoToken;
 import com.cooleshow.base.ui.fragment.BaseMVPFragment;
@@ -53,6 +55,7 @@ import com.cooleshow.base.utils.ToastUtil;
 import com.cooleshow.base.utils.UiUtils;
 import com.cooleshow.base.utils.Utils;
 import com.cooleshow.base.utils.helper.GlideEngine;
+import com.cooleshow.base.utils.helper.LogUploadManager;
 import com.cooleshow.base.utils.helper.PermissionTipHelper;
 import com.cooleshow.base.widgets.DialogUtil;
 import com.cooleshow.teacher.R;
@@ -857,6 +860,9 @@ public class MallFragment extends BaseMVPFragment<FragmentMallBinding, ShopMallP
                     LOG.i("onReceivedError:" + error.getDescription());
                     if (error.getErrorCode() == WebViewClient.ERROR_HOST_LOOKUP || error.getErrorCode() == WebViewClient.ERROR_CONNECT) {
                         showLoadErrorView();
+                    } else {
+                        String errorMsg = "url:" + request.getUrl().toString() + " " + error.getDescription();
+                        LogUploadManager.getInstance().uploadErrorLog(ErrorType.HTML_ERROR, ErrorConstants.getErrorMsg(error.getErrorCode(), errorMsg));
                     }
                 }
             }

+ 6 - 0
teacher/src/main/java/com/cooleshow/teacher/ui/web/HtmlActivity.java

@@ -54,6 +54,8 @@ import com.cooleshow.base.bean.WxPayResult;
 import com.cooleshow.base.common.WebApi;
 import com.cooleshow.base.common.WebConstants;
 import com.cooleshow.base.constanst.Constants;
+import com.cooleshow.base.constanst.ErrorConstants;
+import com.cooleshow.base.constanst.ErrorType;
 import com.cooleshow.base.constanst.StyleConfig;
 import com.cooleshow.base.constanst.UploadConstants;
 import com.cooleshow.base.data.auth.Https;
@@ -79,6 +81,7 @@ import com.cooleshow.base.utils.helper.CommonShareHelper;
 import com.cooleshow.base.utils.helper.DialogHelper;
 import com.cooleshow.base.utils.helper.DownloadHelper;
 import com.cooleshow.base.utils.helper.GlideEngine;
+import com.cooleshow.base.utils.helper.LogUploadManager;
 import com.cooleshow.base.utils.helper.PermissionTipHelper;
 import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
 import com.cooleshow.base.utils.helper.WebLoadFileHelper;
@@ -1238,6 +1241,9 @@ public class HtmlActivity extends BaseActivity<ActivityHtml1Binding> implements
                     LOG.i("onReceivedError:" + error.getDescription());
                     if (error.getErrorCode() == WebViewClient.ERROR_HOST_LOOKUP || error.getErrorCode() == WebViewClient.ERROR_CONNECT) {
                         showLoadErrorView();
+                    } else {
+                        String errorMsg = "url:" + request.getUrl().toString() + " " + error.getDescription();
+                        LogUploadManager.getInstance().uploadErrorLog(ErrorType.HTML_ERROR, ErrorConstants.getErrorMsg(error.getErrorCode(), errorMsg));
                     }
                 }
             }