2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.log.elements;
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.common.log.api.ILogConfiguration;
25 import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
26 import org.openecomp.sdc.common.log.enums.Severity;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 import java.net.InetAddress;
32 import java.time.Duration;
33 import java.time.Instant;
34 import java.time.LocalDateTime;
35 import java.time.ZoneOffset;
36 import java.time.format.DateTimeFormatter;
38 import static java.lang.Integer.valueOf;
40 public class LogFieldsMdcHandler implements ILogFieldsHandler {
42 private static LogFieldsMdcHandler instanceMdcWrapper = new LogFieldsMdcHandler();
44 public static LogFieldsMdcHandler getInstance() {
45 return instanceMdcWrapper;
48 private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSSz";
49 private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_PATTERN);
50 protected static Logger log = LoggerFactory.getLogger(LogFieldsMdcHandler.class.getName());
51 static String hostAddress;
52 protected static String fqdn;
56 hostAddress = InetAddress.getLocalHost().getHostAddress();
57 fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
58 } catch (Exception ex) {
59 log.error("failed to get machine parameters", ex);
64 public void startAuditTimer() {
65 if (StringUtils.isEmpty(MDC.get(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP))) {
66 MDC.put(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP, generatedTimeNow());
71 public void startMetricTimer() {
72 if (StringUtils.isEmpty(MDC.get(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP))) {
73 MDC.put(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP, generatedTimeNow());
78 public void stopAuditTimer() {
79 //set start time if it is not set yet
81 MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
82 setElapsedTime(MDC.get(ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP));
86 public void stopMetricTimer() {
87 //set start time if it is not set yet
89 MDC.put(ILogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
90 setElapsedTime(MDC.get(ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP));
94 public void setClassName(String className) {
95 MDC.put(ILogConfiguration.MDC_CLASS_NAME, className);
99 public void setServerFQDN(String serverFQDN) {
100 MDC.put(ILogConfiguration.MDC_SERVER_FQDN, serverFQDN);
104 public void setServerIPAddress(String serverIPAddress) {
105 MDC.put(ILogConfiguration.MDC_SERVER_IP_ADDRESS, serverIPAddress);
109 public void setServerFQDNInternally() {
114 public void setServerIPAddressInternally() {
115 setServerIPAddress(hostAddress);
119 public void setInstanceUUID(String instanceUUID) {
120 MDC.put(ILogConfiguration.MDC_INSTANCE_UUID, instanceUUID);
124 public void setProcessKey(String processKey) {
125 MDC.put(ILogConfiguration.MDC_PROCESS_KEY, processKey);
129 public void setAlertSeverity(Severity alertSeverity) {
130 MDC.put(ILogConfiguration.MDC_ALERT_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
134 public void setOptCustomField1(String customField1) {
135 MDC.put(ILogConfiguration.MDC_OPT_FIELD1, customField1);
139 public void setOptCustomField2(String customField2) {
140 MDC.put(ILogConfiguration.MDC_OPT_FIELD2, customField2);
144 public void setOptCustomField3(String customField3) {
145 MDC.put(ILogConfiguration.MDC_OPT_FIELD3, customField3);
149 public void setOptCustomField4(String customField4) {
150 MDC.put(ILogConfiguration.MDC_OPT_FIELD4, customField4);
154 public void setKeyRequestId(String keyRequestId) {
155 MDC.put(ILogConfiguration.MDC_KEY_REQUEST_ID, keyRequestId); // eg. servletRequest.getSession().getId()
159 public void setRemoteHost(String remoteHost) {
160 MDC.put(ILogConfiguration.MDC_REMOTE_HOST, remoteHost);
164 public void setServiceName(String serviceName) {
165 MDC.put(ILogConfiguration.MDC_SERVICE_NAME, serviceName);
169 public void setStatusCode(String statusCode) {
170 MDC.put(ILogConfiguration.MDC_STATUS_CODE, statusCode);
174 public void setPartnerName(String partnerName) {
175 MDC.put(ILogConfiguration.MDC_PARTNER_NAME, partnerName);
179 public void setResponseCode(int responseCode) {
180 MDC.put(ILogConfiguration.MDC_RESPONSE_CODE, Integer.toString(responseCode));
184 public void setResponseDesc(String responseDesc) {
185 MDC.put(ILogConfiguration.MDC_RESPONSE_DESC, responseDesc);
189 public void setServiceInstanceId(String serviceInstanceId) {
190 MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
194 public void setTargetEntity(String targetEntity) {
195 MDC.put(ILogConfiguration.MDC_TARGET_ENTITY, targetEntity);
199 public void setTargetServiceName(String targetServiceName) {
200 MDC.put(ILogConfiguration.MDC_TARGET_SERVICE_NAME, targetServiceName);
204 public void setTargetVirtualEntity(String targetVirtualEntity) {
205 MDC.put(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
209 public void setErrorCode(int errorCode) {
210 MDC.put(ILogConfiguration.MDC_ERROR_CODE, valueOf(errorCode).toString());
214 public void setErrorCategory(String errorCategory) {
215 MDC.put(ILogConfiguration.MDC_ERROR_CATEGORY, errorCategory);
219 public String getErrorCode() {
220 return MDC.get(ILogConfiguration.MDC_ERROR_CODE);
224 public String getServiceName() {
225 return MDC.get(ILogConfiguration.MDC_SERVICE_NAME);
229 public String getErrorCategory() {
230 return MDC.get(ILogConfiguration.MDC_ERROR_CATEGORY);
234 public void clear() {
239 public boolean isMDCParamEmpty(String mdcKeyName) {
240 return StringUtils.isEmpty(MDC.get(mdcKeyName));
244 public String getFqdn() {
249 public String getHostAddress() {
254 public String getKeyRequestId() {
255 return MDC.get(ILogConfiguration.MDC_KEY_REQUEST_ID);
259 public void removeStatusCode() {
260 MDC.remove(ILogConfiguration.MDC_STATUS_CODE);
264 public void removePartnerName() {
265 MDC.remove(ILogConfiguration.MDC_PARTNER_NAME);
269 public void removeResponseCode() {
270 MDC.remove(ILogConfiguration.MDC_RESPONSE_CODE);
274 public void removeResponseDesc() {
275 MDC.remove(ILogConfiguration.MDC_RESPONSE_DESC);
279 public void removeServiceInstanceId() {
280 MDC.remove(ILogConfiguration.MDC_SERVICE_INSTANCE_ID);
284 public void removeTargetEntity() {
285 MDC.remove(ILogConfiguration.MDC_TARGET_ENTITY);
289 public void removeTargetServiceName() {
290 MDC.remove(ILogConfiguration.MDC_TARGET_SERVICE_NAME);
294 public void removeTargetVirtualEntity() {
295 MDC.remove(ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY);
299 public void removeErrorCode() {
300 MDC.remove(ILogConfiguration.MDC_ERROR_CODE);
304 public void removeErrorCategory() {
305 MDC.remove(ILogConfiguration.MDC_ERROR_CATEGORY);
309 public void removeErrorDescription() {
310 MDC.remove(ILogConfiguration.MDC_ERROR_DESC);
314 public void setAuditMessage(String message) {
315 MDC.put(ILogConfiguration.MDC_AUDIT_MESSAGE, message);
319 public String getAuditMessage() {
320 return MDC.get(ILogConfiguration.MDC_AUDIT_MESSAGE);
323 private void setElapsedTime(String beginTimestamp) {
325 final LocalDateTime startTime = LocalDateTime.parse(beginTimestamp, dateTimeFormatter);
326 final LocalDateTime endTime = LocalDateTime.parse(MDC.get(ILogConfiguration.MDC_END_TIMESTAMP), dateTimeFormatter);
327 final Duration timeDifference = Duration.between(startTime, endTime);
329 MDC.put(ILogConfiguration.MDC_ELAPSED_TIME, String.valueOf(timeDifference.toMillis()));
331 } catch (Exception ex) {
332 log.error("failed to calculate elapsed time", ex);
336 private String generatedTimeNow() {
337 return dateTimeFormatter
338 .withZone(ZoneOffset.UTC)
339 .format(Instant.now());