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 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 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 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; } }