wjc
2023-07-07 22494af577e21a930abef309f2f60c03c9615bd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.hdl.log.utils;
 
import android.text.TextUtils;
 
import com.aliyu.LogProducerClient;
import com.aliyun.sls.android.producer.Log;
import com.hdl.log.HDLLog;
import com.hdl.log.bean.LogBean;
import com.hdl.log.enums.Level;
 
import java.util.Map;
 
 
/**
 * Created by hxb on 2023/3/14.
 * 异常日志处理工具类
 */
public class LogUtil {
 
    private static final String LEVEL_PARAM = "level";
    private static LogProducerClient exceptionProducerClient;
 
    private static LogBean logBean;
 
    private static synchronized LogProducerClient getExceptionProducerClient() {
        if (exceptionProducerClient == null) {
            exceptionProducerClient = new LogProducerClient(HDLLog.getContext(), HDLLog.getEndPoint(), HDLLog.getProjectName(), HDLLog.getExceptionLog(), HDLLog.getAccessId(), HDLLog.getAccessKey(), null, null);
            exceptionProducerClient.initProducer();
        }
        return exceptionProducerClient;
    }
 
    /**
     * 异常日志
     *
     * @param title    标题
     * @param content  附加数据内容
     * @param otherMsg 其他附加数据,没有可为空
     * @param level    报警等级
     */
    public static void log(String title, String content, Map<String, String> otherMsg, Level level) {
        log(title,content,otherMsg,level,null);
    }
    /**
     * 异常日志
     *
     * @param title    标题
     * @param content  附加数据内容
     * @param otherMsg 其他附加数据,没有可为空
     * @param level    报警等级
     * @param type 异常类型
     */
    public static void log(String title, String content, Map<String, String> otherMsg, Level level,String type) {
        log(title,content,otherMsg,level,null,null);
    }
    /**
     * 异常日志
     *
     * @param title    标题
     * @param content  附加数据内容
     * @param otherMsg 其他附加数据,没有可为空
     * @param level    报警等级
     * @param type 异常类型
     * @param exceptionBlock 异常代码块
     */
    public static void log(String title, String content, Map<String, String> otherMsg, Level level,String type, String exceptionBlock) {
        /**
         * 没有homeId和userId  不允许上传   只上传住宅里面的异常  减少云端压力
         * 1.点击项目的时候设置
         * 2.关闭住宅页面的时候去掉
         */
        if (TextUtils.isEmpty(getLogBean().getUserId()) || TextUtils.isEmpty(getLogBean().getHomeId())) {
            return;
        }
        Log log = new Log();
        log.putContent("title", title);
        log.putContent("content", content);
        log.putContent(LEVEL_PARAM, level.getCode());
        log.putContent("userId", getLogBean().getUserId());
        log.putContent("homeId", getLogBean().getHomeId());
        log.putContent("time", System.currentTimeMillis() + "");
        log.putContent("userAgent", getLogBean().getUserAgent());
        log.putContent("appVersion", getLogBean().getAppVersion());
        log.putContent("type",type);
        log.putContent("exceptionBlock",exceptionBlock);
        if (otherMsg != null) {
            log.putContents(otherMsg);
        }
        getExceptionProducerClient().sendLog(log);
    }
 
    public static LogBean getLogBean() {
        if (null == logBean) {
            logBean = new LogBean();
        }
        return logBean;
    }
}