2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END============================================
\r
20 * ===================================================================
\r
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
24 package org.onap.clamp.clds.util;
\r
26 import java.text.DateFormat;
\r
27 import java.text.SimpleDateFormat;
\r
28 import java.util.Date;
\r
29 import java.util.TimeZone;
\r
30 import java.util.UUID;
\r
32 import org.jboss.logging.MDC;
\r
34 public class LoggingUtils {
\r
37 * Set request related logging variables in thread local data via MDC
\r
40 * Service Name of API (ex. "PUT template")
\r
42 * Partner name (client or user invoking API)
\r
44 public static void setRequestContext(String service, String partner) {
\r
45 MDC.put("RequestId", "clds-" + UUID.randomUUID().toString());
\r
46 MDC.put("ServiceName", service);
\r
47 MDC.put("PartnerName", partner);
\r
51 * Set time related logging variables in thread local data via MDC
\r
53 * @param beginTimeStamp
\r
55 * @param endTimeStamp
\r
58 public static void setTimeContext(Date beginTimeStamp, Date endTimeStamp) {
\r
59 String beginTime = "";
\r
60 String endTime = "";
\r
61 String elapsedTime = "";
\r
63 if (beginTimeStamp != null && endTimeStamp != null) {
\r
64 elapsedTime = String.valueOf(endTimeStamp.getTime() - beginTimeStamp.getTime());
\r
65 beginTime = generateTimestampStr(beginTimeStamp);
\r
66 endTime = generateTimestampStr(endTimeStamp);
\r
69 MDC.put("BeginTimestamp", beginTime);
\r
70 MDC.put("EndTimestamp", endTime);
\r
71 MDC.put("ElapsedTime", elapsedTime);
\r
75 * Set response related logging variables in thread local data via MDC
\r
78 * Response code ("0" indicates success)
\r
79 * @param description
\r
80 * Response description
\r
82 * class name of invoking class
\r
84 public static void setResponseContext(String code, String description, String className) {
\r
85 MDC.put("ResponseCode", code);
\r
86 MDC.put("StatusCode", code.equals("0") ? "COMPLETE" : "ERROR");
\r
87 MDC.put("ResponseDescription", description != null ? description : "");
\r
88 MDC.put("ClassName", className != null ? className : "");
\r
92 * Set target related logging variables in thread local data via MDC
\r
94 * @param targetEntity
\r
95 * Target entity (an external/sub component, for ex. "sdc")
\r
96 * @param targetServiceName
\r
97 * Target service name (name of API invoked on target)
\r
99 public static void setTargetContext(String targetEntity, String targetServiceName) {
\r
100 MDC.put("TargetEntity", targetEntity != null ? targetEntity : "");
\r
101 MDC.put("TargetServiceName", targetServiceName != null ? targetServiceName : "");
\r
105 * Set error related logging variables in thread local data via MDC
\r
109 * @param description
\r
110 * Error description
\r
112 public static void setErrorContext(String code, String description) {
\r
113 MDC.put("ErrorCode", code);
\r
114 MDC.put("ErrorDescription", description != null ? description : "");
\r
117 private static String generateTimestampStr(Date timeStamp) {
\r
118 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
\r
119 TimeZone tz = TimeZone.getTimeZone("UTC");
\r
120 df.setTimeZone(tz);
\r
121 return df.format(timeStamp);
\r