老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

定位bug打日志的方式有哪些

這篇文章主要介紹“定位bug打日志的方式有哪些”,在日常操作中,相信很多人在定位bug打日志的方式有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”定位bug打日志的方式有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:主機域名、虛擬空間、營銷軟件、網站建設、江口網站維護、網站推廣。

請統一日志格式

日志格式最好是統一的,即方便查看定位問題又方便統計收集。我一般喜歡定義一個LogObject對象,里面定義日志的各個字段。例如:

import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.annotation.JsonInclude.Include;import com.fasterxml.jackson.annotation.JsonProperty;public class LogObject {@JsonProperty(index = 1)private String eventName;@JsonProperty(index = 2)private String traceId;    @JsonProperty(index = 3)private String msg;    @JsonProperty(index = 4)private long costTime;    @JsonProperty(index = 6)private Integer userId;    @JsonProperty(index = 7)private Object others;    @JsonProperty(index = 8)private Object request;    @JsonProperty(index = 9)private Object response;public String getEventName() {return eventName;
    }    public LogObject setEventName(String eventName) {this.eventName = eventName;return this;
    }    public Object getRequest() {return request;
    }    public LogObject setRequest(Object request) {this.request = request;return this;
    }    public Object getResponse() {return response;
    }    public LogObject setResponse(Object response) {this.response = response;return this;
    }    public String getMsg() {return msg;
    }    public LogObject setMsg(String msg) {this.msg = msg;return this;
    }public long getCostTime() {return costTime;
    }    public LogObject setCostTime(long costTime) {this.costTime = costTime;return this;
    }public Integer getUserId() {return userId;
    }    public LogObject setUserId(Integer userId) {this.userId = userId;return this;
    }    public Object getOthers() {return others;
    }    public LogObject setOthers(Object others) {this.others = others;return this;
    }    public String getTraceId() {return traceId;
    }    public LogObject setTraceId(String traceId) {this.traceId = traceId;return this;
    }
  • traceId: 調用鏈id

  • eventName: 事件名稱,一般就是業務方法名稱

  • userId: C端用戶id

  • msg: 結果消息

  • costTime: 接口響應時間

  • request: 接口請求入參

  • response: 接口返回值

  • others: 其他業務參數

使用鏈式的風格,方便設置字段的值:

long endTime = System.currentTimeMillis();
LogObject logObject = new LogObject();
logObject.setEventName(methodName).setMsg(msg).setTraceId(traceId).setUserId(backendId).setRequest(liveRoomPushOrderReqDto).setResponse(response).setCostTime((endTime - beginTime));LOGGER.info(JSON.toJSONString(logObject));

當然最好還是封裝出一個工具類出來,例如叫:LogTemplate,作為一個統一的入口。
另外可以使用JsonProperty注解,指定字段的順序,例如通過index=1,將eventName放置在最前面。

@JsonProperty(index = 1)private String eventName;
將request和response放置在一起

將請求和返回值,放置在同一條日志里,有個好處,就是非常方便查看上下文日志。
如果打印成兩條,返回值那條可能被沖到很后面,而且也得再做一次grep操作,影響效率。
具體的日志如下:

{   "eventName":"createOrder",   "traceId":"createOrder_1574923602015",   "msg":"success",   "costTime":317,   "request":{      "uId":111111111,      "skuList":[
         {"skuId":22222222,"buyNum":1,"buyPrice":8800,
         }
      ]
   },   "response":{      "code":0,      "message":"操作成功",      "data":{         "bigOrderId":"BIG2019",         "m2LOrderIds":{"MID2019":{               "22222222":"LIT2019"}
         }
      }
   }
}

為了能拼成一條,有兩種方案,一種是比較low的,直接在代碼里使用try catch finally,例如:

@PostMapping(value = "/createOrder")public JsonResult createOrder(@RequestBody Object request) throws Exception {
    String methodName = "/createOrder";
    Integer backendId = null;
    String msg = "success";
    long beginTime = System.currentTimeMillis();
    String traceId = "createOrder_"+beginTime;
    JsonResult response = null;try {
        OrderCreateRsp orderCreateRsp = orderOperateService.createOrder(request, traceId);
        response = JsonResult.success(orderCreateRsp);
    }
    catch (Exception e) {
        msg = e.getMessage();LOGGER.error(methodName+",userId:"+backendId+",request:"+ JsonHelper.toJson(request),e);
        throw new BizException(0,"下單失敗");
    }
    finally {
        long endTime = System.currentTimeMillis();
        LogObject logObject = new LogObject();
        logObject.setEventName(methodName) .setMsg(msg) .setTraceId(traceId) .setUserId(backendId) .setRequest(request) .setResponse(response) .setCostTime((endTime - beginTime));LOGGER.info(JSON.toJSONString(logObject));
    }
    
    return response;
}

這種方案呢,有個缺點,就是每個業務方法都得處理日志,更好的方案是使用aop加thread local的方式,將請求統一攔截且將返回值和請求參數串起來,這個網絡上的方案很多,這里就不闡述了。

對于對性能要求比較高的應用,反而推薦第一種方案,因為使用aop,有一些性能損耗。像我之前在唯品會參與的商品聚合服務,用的就是第一種方案,畢竟每一秒要處理上百萬的請求。

日志里加入traceId

如果應用中已經使用了統一調用鏈監控方案,且能根據調用鏈id查詢接口情況的,可以不用在代碼里手動加入traceId。
如果應用還沒接入調用鏈系統,建議加一下traceId,尤其是針對聚合服務,需要調用中臺各種微服務接口的。像聚合層下單業務,需要調用的微服務就有如下這么些:

  • 營銷系統

  • 訂單系統

  • 支付系統
    下單業務調用這些接口的時候,如果沒有使用traceId進行跟蹤的話,當下單失敗的時候,到底是哪個微服務接口失敗了,就比較難找。下面以小程序端,調用聚合層下單接口的例子作為展示:
    營銷系統:

{   "eventName":"pms/getInfo",   "traceId":"createOrder_1575270928956",   "msg":"success",   "costTime":2,   "userId":1111111111,   "request":{      "userId":1111111111,      "skuList":[
         {"skuId":2222,"skuPrice":65900,"buyNum":1,"activityType":0,"activityId":0,
         }
      ],
   },   "response":{      "result":1,      "msg":"success",      "data":{         "realPayFee":100,
      }
   }
}

訂單系統:

{   "eventName":"orderservice/createOrder",   "traceId":"createOrder_1575270928956",   "msg":"success",   "costTime":29,   "userId":null,   "request":{      "skuList":[
         {"skuId":2222,"buyNum":1,"buyPrice":65900,
         }
      ],
   },   "response":{      "result":"200",      "msg":"調用成功",      "data":{         "bigOrderId":"BIG2019",         "m2LOrderIds":{"MID2019":{               "88258135":"LIT2019"}
         }
      }
   }
}

支付系統:

{   "eventName":"payservice/pay",   "traceId":"createOrder_1575270928956",   "msg":"success",   "costTime":301,   "request":{      "orderId":"BIG2019",      "paySubject":"測試",      "totalFee":65900,
   },   "response":{      "requestId":"test",      "code":0,      "message":"操作成功",      "data":{         "payId":123,         "orderId":"BIG2019",         "tradeType":"JSAPI",         "perpayId":"test",         "nonceStr":"test",         "appId":"test",         "signType":"MD5",         "sign":"test",         "timeStamp":"1575270929"  }
   }
}

可以看到聚合層需要調用營銷、訂單和支付三個應用的接口,調用的過程中,使用traceId為createOrder_1575270928956的串了起來,這樣我們只需要grep這個traceId就可以把所有相關的調用和上下文找出來。
traceId如何生成呢,一種簡單的做法是,使用System.currentTimeMillis() 加上業務接口名字,如:

 long beginTime = System.currentTimeMillis();
 String traceId = "createOrder_"+beginTime;

加traceId會侵入到業務方法里,比如說:

public void createOrder(Object obj) {
  long beginTime = System.currentTimeMillis();
   String traceId = "createOrder_"+beginTime;
   pmsService.getInfo(obj,traceId);
   orderService.createOrder(obj,traceId);
   payService.getPrepayId(obj,traceId);
}

像pmsService這些內部的service方法,都需要加一個traceId字段,目前我覺得還好,要是覺得入侵了,也可以考慮thread local的方式,處理請求的時候,為當前線程存儲一下traceId,然后在業務方法里,再從當前線程里拿出來,避免接口方法里的traceId滿天飛。

到此,關于“定位bug打日志的方式有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注創新互聯網站,小編會繼續努力為大家帶來更多實用的文章!


當前文章:定位bug打日志的方式有哪些
瀏覽地址:http://www.xueling.net.cn/article/ggccce.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 蜜桃视频在线免费观看 | 日本欧美一区二区三区高清 | 97色偷偷色噜噜狠狠爱网站97 | av视频在线播放 | 一级毛片国产 | 91草逼视频 | www.在线国产| 三年片中国在线观看免费大全 | www.麻豆视频| 午夜寂寞福利视频 | 亚洲日本成本人观看 | 人与人性恔配视频免费 | 亚洲狠狠婷婷综合久久久久 | 亚洲欧美国产国产综合一区 | 姓一乱一口一交A片文 | 国产视频污 | 久久66热人妻偷产国产 | p色视频免费在线观看 | 国产美女被遭高潮免费网站 | 日韩一级在线视频 | 久章草在线无码视频观看 | 黄色在线观看www | 成人综合一区 | av在线亚洲欧洲日产一区二区 | AV无码一区二区三区 | 亚欧午夜福利网站 | 精品热久久 | 韩国三级中文字幕HD久久精品 | 色婷婷人妻av毛片一区 | 女友的滋味在线观看 | 亚洲Av无码专区国产乱码DVD | 国产内射露脸在线观看 | 99久久精品国产一区二区三区 | 香蕉啪视频在线观看视频久 | 亚洲午夜久久久综合37日本 | chinese猛男自慰gv网站 | 国产一在线精品一区在线观看 | 国产99久久久国产精品~~牛 | 九九精品影院 | 国产成人无码网站 | 法国啄木乌AV片在线播放 |