|
@@ -0,0 +1,79 @@
|
|
|
|
+package com.swago.baseswago.util
|
|
|
|
+
|
|
|
|
+import com.aliyun.sls.android.producer.Log
|
|
|
|
+import com.aliyun.sls.android.producer.LogProducerClient
|
|
|
|
+import com.aliyun.sls.android.producer.LogProducerConfig
|
|
|
|
+import com.aliyun.sls.android.producer.LogProducerException
|
|
|
|
+import com.aliyun.sls.android.producer.LogProducerResult
|
|
|
|
+import com.swago.baseswago.inter.ApiManager
|
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
+import org.json.JSONObject
|
|
|
|
+import java.text.SimpleDateFormat
|
|
|
|
+import java.util.Date
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+object LogProducer {
|
|
|
|
+ private var config: LogProducerConfig? = null
|
|
|
|
+ private var client: LogProducerClient? = null
|
|
|
|
+ private var sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
|
|
|
+
|
|
|
|
+ init {
|
|
|
|
+ initProducer()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun initProducer() {
|
|
|
|
+ try {
|
|
|
|
+ config = LogProducerConfig()
|
|
|
|
+ config?.endpoint = "cn-hongkong.log.aliyuncs.com"
|
|
|
|
+ config?.project = "yunwei-swago"
|
|
|
|
+ config?.logstore = "app"
|
|
|
|
+ client = LogProducerClient(config
|
|
|
|
+ ) { resultCode, reqId, errorMessage, logBytes, compressedBytes ->
|
|
|
|
+ val result = LogProducerResult.fromInt(resultCode)
|
|
|
|
+ if (LogProducerResult.LOG_PRODUCER_SEND_UNAUTHORIZED == result || LogProducerResult.LOG_PRODUCER_PARAMETERS_INVALID == result) {
|
|
|
|
+ // 需要更新AccessKey或者SDK的初始化参数。
|
|
|
|
+ requestAccessKey()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (e: LogProducerException) {
|
|
|
|
+ e.printStackTrace()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun requestAccessKey() {
|
|
|
|
+ CoroutineScope(Dispatchers.Default).launch {
|
|
|
|
+ val model = ApiManager.configApi.getLogToken()
|
|
|
|
+ updateAccessKey(model.access_key_id,model.access_key_secret,model.security_token)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun updateAccessKey(accessKeyId:String,accessKeySecret:String,securityToken:String?){
|
|
|
|
+ if (!securityToken.isNullOrEmpty()){
|
|
|
|
+ config?.resetSecurityToken(accessKeyId,accessKeySecret,securityToken)
|
|
|
|
+ } else {
|
|
|
|
+ config?.setAccessKeyId(accessKeyId)
|
|
|
|
+ config?.setAccessKeySecret(accessKeySecret)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ fun addLog(customLog:JSONObject,type:String){
|
|
|
|
+ if (null == client) {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ val log = Log()
|
|
|
|
+ val jsonObject =JSONObject()
|
|
|
|
+ val jsonContentsObject =JSONObject()
|
|
|
|
+ jsonContentsObject.put("type", type)
|
|
|
|
+ jsonContentsObject.put("device_info", SwagoInfo.getDeviceInfo())
|
|
|
|
+ jsonContentsObject.put("device_system", "android")
|
|
|
|
+ jsonContentsObject.put("app_channel", SwagoInfo.getChannel())
|
|
|
|
+ jsonContentsObject.put("app_version",SwagoInfo.getVersionCode())
|
|
|
|
+ jsonContentsObject.put("user_id", UserInfo.getUserInfo()?.id)
|
|
|
|
+ jsonContentsObject.put("custom_log", customLog)
|
|
|
|
+ jsonObject.put("time", sdf.format(Date(System.currentTimeMillis())))
|
|
|
|
+ jsonObject.put("contents",jsonContentsObject)
|
|
|
|
+ log.putContent(jsonObject)
|
|
|
|
+ client?.addLog(log)
|
|
|
|
+ }
|
|
|
|
+}
|