aboutsummaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/log/elements/LogFieldsMdcHandler.java
blob: 3e4d7d4c899b403e88fa88f756597bde499061a9 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package org.openecomp.sdc.common.log.elements;

import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.common.log.api.ILogConfiguration;
import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
import org.openecomp.sdc.common.log.enums.Severity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.net.InetAddress;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import static java.lang.Integer.valueOf;

public class LogFieldsMdcHandler implements ILogFieldsHandler {

    private static LogFieldsMdcHandler instanceMdcWrapper = new LogFieldsMdcHandler();

    public static LogFieldsMdcHandler getInstance() {
        return instanceMdcWrapper;
    }

    private final static String dateFormatPattern = "yyyy-MM-dd HH:mm:ss.SSSz";
    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern);
    protected static Logger log = LoggerFactory.getLogger(LogFieldsMdcHandler.class.getName());
    protected static String hostAddress;
    protected static String fqdn;

    static {
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
            fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
        } catch (Exception ex) {
            log.error("failed to get machine parameters", ex);
        }
    }

    @Override
    public void startAuditTimer() {
        if (StringUtils.isEmpty(MDC.get(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP))) {
            MDC.put(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP, generatedTimeNow());
        }
    }

    @Override
    public void startMetricTimer() {
        if (StringUtils.isEmpty(MDC.get(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP))) {
            MDC.put(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP, generatedTimeNow());
        }
    }

    @Override
    public void stopAuditTimer() {
        //set start time if it is not set yet
        startAuditTimer();
        MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
        setElapsedTime(MDC.get(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP));
    }

    @Override
    public void stopMetricTimer() {
        //set start time if it is not set yet
        startMetricTimer();
        MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
        setElapsedTime(MDC.get(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP));
    }

    @Override
    public void setClassName(String className) {
        MDC.put(ILogConfiguration.MDC_CLASS_NAME, className);
    }

    @Override
    public void setServerFQDN(String serverFQDN) {
        MDC.put(ILogConfiguration.MDC_SERVER_FQDN, serverFQDN);
    }

    @Override
    public void setServerIPAddress(String serverIPAddress) {
        MDC.put(ILogConfiguration.MDC_SERVER_IP_ADDRESS, serverIPAddress);
    }

    @Override
    public void setServerFQDNInternally() {
        setServerFQDN(fqdn);
    }

    @Override
    public void setServerIPAddressInternally() {
        setServerIPAddress(hostAddress);
    }

    @Override
    public void setInstanceUUID(String instanceUUID) {
        MDC.put(ILogConfiguration.MDC_INSTANCE_UUID, instanceUUID);
    }

    @Override
    public void setProcessKey(String processKey) {
        MDC.put(ILogConfiguration.MDC_PROCESS_KEY, processKey);
    }

    @Override
    public void setAlertSeverity(Severity alertSeverity) {
        MDC.put(ILogConfiguration.MDC_ALERT_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
    }

    @Override
    public void setOptCustomField1(String customField1) {
        MDC.put(ILogConfiguration.MDC_OPT_FIELD1, customField1);
    }

    @Override
    public void setOptCustomField2(String customField2) {
        MDC.put(ILogConfiguration.MDC_OPT_FIELD2, customField2);
    }

    @Override
    public void setOptCustomField3(String customField3) {
        MDC.put(ILogConfiguration.MDC_OPT_FIELD3, customField3);
    }

    @Override
    public void setOptCustomField4(String customField4) {
        MDC.put(ILogConfiguration.MDC_OPT_FIELD4, customField4);
    }

    @Override
    public void setKeyRequestId(String keyRequestId) {
        MDC.put(ILogConfiguration.MDC_KEY_REQUEST_ID, keyRequestId); // eg. servletRequest.getSession().getId()
    }

    @Override
    public void setRemoteHost(String remoteHost) {
        MDC.put(ILogConfiguration.MDC_REMOTE_HOST, remoteHost);
    }

    @Override
    public void setServiceName(String serviceName) {
        MDC.put(ILogConfiguration.MDC_SERVICE_NAME, serviceName);
    }

    @Override
    public void setStatusCode(String statusCode) {
        MDC.put(ILogConfiguration.MDC_STATUS_CODE, statusCode);
    }

    @Override
    public void setPartnerName(String partnerName) {
        MDC.put(ILogConfiguration.MDC_PARTNER_NAME, partnerName);
    }

    @Override
    public void setResponseCode(int responseCode) {
        MDC.put(ILogConfiguration.MDC_RESPONSE_CODE, Integer.toString(responseCode));
    }

    @Override
    public void setResponseDesc(String responseDesc) {
        MDC.put(ILogConfiguration.MDC_RESPONSE_DESC, responseDesc);
    }

    @Override
    public void setServiceInstanceId(String serviceInstanceId) {
        MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
    }

    @Override
    public void setTargetEntity(String targetEntity) {
        MDC.put(ILogConfiguration.MDC_TARGET_ENTITY, targetEntity);
    }

    @Override
    public void setTargetServiceName(String targetServiceName) {
        MDC.put(ILogConfiguration.MDC_TARGET_SERVICE_NAME, targetServiceName);
    }

    @Override
    public void setTargetVirtualEntity(String targetVirtualEntity) {
        MDC.put(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
    }

    @Override
    public void setErrorCode(int errorCode) {
        MDC.put(ILogConfiguration.MDC_ERROR_CODE, valueOf(errorCode).toString());
    }

    @Override
    public void setErrorCategory(String errorCategory) {
        MDC.put(ILogConfiguration.MDC_ERROR_CATEGORY, errorCategory);
    }

    @Override
    public String getErrorCode() {
        return MDC.get(ILogConfiguration.MDC_ERROR_CODE);
    }

    @Override
    public String getServiceName() {
        return MDC.get(ILogConfiguration.MDC_SERVICE_NAME);
    }

    @Override
    public String getErrorCategory() {
        return MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY);
    }

    @Override
    public void clear() {
        MDC.clear();
    }

    @Override
    public boolean isMDCParamEmpty(String mdcKeyName) {
        return StringUtils.isEmpty(MDC.get(mdcKeyName));
    }

    @Override
    public String getFqdn() {
        return fqdn;
    }

    @Override
    public String getHostAddress() {
        return hostAddress;
    }

    @Override
    public String getKeyRequestId() {
        return MDC.get(ILogConfiguration.MDC_KEY_REQUEST_ID);
    }

    @Override
    public void removeStatusCode() {
        MDC.remove(ILogConfiguration.MDC_STATUS_CODE);
    }

    @Override
    public void removePartnerName(){
        MDC.remove(ILogConfiguration.MDC_PARTNER_NAME);
    }

    @Override
    public void removeResponseCode(){
        MDC.remove(ILogConfiguration.MDC_RESPONSE_CODE);
    }

    @Override
    public void removeResponseDesc(){
        MDC.remove(ILogConfiguration.MDC_RESPONSE_DESC);
    }

    @Override
    public void removeServiceInstanceId(){
        MDC.remove(ILogConfiguration.MDC_SERVICE_INSTANCE_ID);
    }

    @Override
    public void removeTargetEntity(){
        MDC.remove(ILogConfiguration.MDC_TARGET_ENTITY);
    }

    @Override
    public void removeTargetServiceName(){
        MDC.remove(ILogConfiguration.MDC_TARGET_SERVICE_NAME);
    }

    @Override
    public void removeTargetVirtualEntity(){
        MDC.remove(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY);
    }

    @Override
    public void removeErrorCode(){
        MDC.remove(ILogConfiguration.MDC_ERROR_CODE);
    }

    @Override
    public void removeErrorCategory(){
        MDC.remove(ILogConfiguration.MDC_ERROR_CATEGORY);
    }

    @Override
    public void removeErrorDescription(){
        MDC.remove(ILogConfiguration.MDC_ERROR_DESC);
    }

    @Override
    public void setAuditMessage(String message) {
        MDC.put(ILogConfiguration.MDC_AUDIT_MESSAGE, message);
    }

    @Override
    public String getAuditMessage() {
        return MDC.get(ILogConfiguration.MDC_AUDIT_MESSAGE);
    }

    private void setElapsedTime(String beginTimestamp) {
        try {
            final LocalDateTime startTime = LocalDateTime.parse(beginTimestamp, dateTimeFormatter);
            final LocalDateTime endTime = LocalDateTime.parse(MDC.get(ILogConfiguration.MDC_END_TIMESTAMP), dateTimeFormatter);
            final Duration timeDifference = Duration.between(startTime, endTime);

            MDC.put(ILogConfiguration.MDC_ELAPSED_TIME, String.valueOf(timeDifference.toMillis()));

        } catch(Exception ex) {
            log.error("failed to calculate elapsed time",ex);
        }
    }

    private String generatedTimeNow() {
        return dateTimeFormatter
                .withZone(ZoneOffset.UTC)
                .format(Instant.now());
    }

}