wuxw7
2018-06-16 12b3cd9bacbb4157209122b1d62284f2b7212493
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.java110.core.context;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.java110.common.log.LoggerEngine;
import com.java110.common.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
/**
 * 交互日志抽象类
 * Created by wuxw on 2018/6/9.
 */
public abstract class AbstractTransactionLog  implements TransactionLog {
 
    private final static Logger logger = LoggerFactory.getLogger(AbstractTransactionLog.class);
 
    private String port;
 
    private String logStatus;
 
    private String requestMessage;
 
    private long costTime;
 
    private String responseMessage;
 
    public String getHostIp() {
        String ip = null;
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        }catch (Exception e){
            logger.error("获取主机Ip失败",e);
            ip = "-1";
        }
        return ip;
    }
 
    /**
     * 时间戳
     * @return
     */
    public String getTimestamp(){
        return DateUtil.getNowDefault();
    }
 
 
    @Override
    public String getLogStatus() {
        return logStatus;
    }
 
    public String getRequestMessage() {
        return requestMessage;
    }
 
    public String getResponseMessage() {
        return responseMessage;
    }
 
    public long getCostTime() {
        return costTime;
    }
 
    @Override
    public String getPort() {
        return port;
    }
 
    /**
     * 预构建
     * @param reqInfo
     * @param headerAll
     */
    protected void preBuilder(String reqInfo, Map<String,String> headerAll){
 
        if(headerAll != null && headerAll.containsKey("port")){
            this.port = headerAll.get("port");
        }
 
    }
 
    /**
     * 重新构建 TransactionLog 对象 主要用于服务调用方
     * @return
     */
    public TransactionLog reBuilder(String requestMessage,String responseMessage,String logStatus,long costTime){
 
        this.logStatus = logStatus;
        this.requestMessage = requestMessage;
        this.responseMessage = responseMessage;
        this.costTime = costTime;
        return this;
    }
 
    /**
     * 重新构建 TransactionLog 对象 主要用于服务提供方
     * @return
     */
    public TransactionLog reBuilder(String appId,String userId,String requestMessage,String responseMessage,String logStatus,long costTime){
 
        this.logStatus = logStatus;
        this.requestMessage = requestMessage;
        this.responseMessage = responseMessage;
        this.setAppId(appId);
        this.setUserId(userId);
        this.costTime = costTime;
        return this;
    }
 
 
    @Override
    public String toString() {
        //return JSONObject.toJSONString(this);
        JSONObject logMessage = JSONObject.parseObject("{}");
        logMessage.put("transactionId",getTransactionId());
        logMessage.put("dataFlowId",getDataFlowId());
        logMessage.put("ip",getHostIp());
        logMessage.put("port", getPort());
        logMessage.put("appId",getAppId());
        logMessage.put("userId",getUserId());
        logMessage.put("serviceCode",getServiceCode());
        logMessage.put("serviceName",getServiceName());
        logMessage.put("timestamp",getTimestamp());
        logMessage.put("logStatus",getLogStatus());
        logMessage.put("costTime",costTime);
        logMessage.put("requestMessage",getRequestMessage());
        logMessage.put("responseMessage",getResponseMessage());
        return logMessage.toJSONString(logMessage,SerializerFeature.WriteNullStringAsEmpty);
    }
}