切记,这样打日志定位 Bug 又快又准!
SegmentFault
共 16118字,需浏览 33分钟
· 2021-10-26
概述
请统一日志格式
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;
}
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));
@JsonProperty(index = 1)
private String eventName;
将request和response放置在一起
{
"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"
}
}
}
}
}
@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;
}
日志里加入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"
}
}
}
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);
}
评论
网红台妹 | 咪啦,甜美又性感的顶级美女模特
今日女神推荐:咪啦,网名昵称:咪啦,她是一位来自中国台湾省的网红美女、平面模特,目前在外网的ins上面,她是一位坐拥10多万粉丝关注的人气网红美女。1995年11月29日出生于中国台湾省的她,拥有一张纯天然的完美脸蛋,她的五官长相精致好看,笑容甜美迷人,在网红圈和模特圈有着不俗的人气,妥妥的一位甜美
逆锋起笔
0
魔改Transformer!9种提速又提效的模型优化方案
向AI转型的程序员都关注了这个号👇👇👇Transformer目前已经成为人工智能领域的主流模型,应用非常广泛。然而Transformer中注意力机制计算代价较高,随着序列长度的增加,这个计算量还会持续上升。为了解决这个问题,业内出现了许多Transformer的魔改工作,以优化Transformer
机器学习AI算法工程
0
冲鸭~支付宝又送疯了!!!
今天支付宝又撒钱了,原来只能领取一两毛钱,刚刚试了试,竟然领了8.08块钱无门槛红包,线下实体店消费可以使用,付款使用支付宝即可抵扣。 领取方法 一 保存这张支付宝专属红包码到相册,再用支付宝扫码,即可直接领现金红包,每天都能领一次,最近加码数量优先,领完即止。👇长按图片,保存到
宅哥技术
0
警报炸锅了,FastJson 又立功了。。
往期热门文章:1、一个小公司的技术开发心酸事(已倒闭)2、JetBrains 如何看待自己的软件在中国被频繁破解?3、程序员因Bug被要求归还4万多年终奖,网友:不还!4、一套万能通用的异步处理方案5、微服务全做错了!谷歌提出新方法,成本直接降9倍!线上事故回顾前段时间新增一个特别简单的功能,晚上上
Java后端技术
1
我用这10招,能减少了80%的BUG
将Python客栈设为“星标⭐”第一时间收到最新资讯前言对于大部分程序员来说,主要的工作时间是在开发和修复BUG。有可能修改了一个BUG,会导致几个新BUG的产生,不断循环。那么,有没有办法能够减少BUG,保证代码质量,提升工作效率?答案是肯定的。如果能做到,我们多出来的时间,多摸点鱼,做点自己喜欢
Python客栈
0
免费AI编程助手,支持Visual Studio,让编码愉悦又轻松
前言前有Copilot各种酷炫操作,今有国产软件杀出重围。给大家介绍的是一款国内的国产编程神器,可与微软GitHub Copilot比比身手。关键它还是完全免费。它就是:非十团队国产自主研发的Fitten Code。此工具的速度是GitHub Copilot的两倍,同时它的精确度还有大约2
dotNET全栈开发
10
华为又招了一名天才少年。。
大家好,我是 Jack。今天周五,咱就随便聊聊科技圈发生的那些事。一、雷军辟谣昨天,也就是 4 月 18 日,雷总在小米北京交付中心和体验中心开启了直播。直播中为自己澄清了坊间的“爽文”传闻。“我希望大家能帮我辟谣:其实我不是高考状元,没有考700分,在低谷的时候卡里也没有冰冷的四十亿。”这倒是印证
Jack Cui
10