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.apache.log4j.MDC;
\r
35 * This class handles the special info that appear in the log, like RequestID,
\r
39 public class LoggingUtils {
\r
42 * Set request related logging variables in thread local data via MDC
\r
45 * Service Name of API (ex. "PUT template")
\r
47 * Partner name (client or user invoking API)
\r
49 public static void setRequestContext(String service, String partner) {
\r
50 MDC.put("RequestId", UUID.randomUUID().toString());
\r
51 MDC.put("ServiceName", service);
\r
52 MDC.put("PartnerName", partner);
\r
56 * Set time related logging variables in thread local data via MDC.
\r
58 * @param beginTimeStamp
\r
60 * @param endTimeStamp
\r
63 public static void setTimeContext(Date beginTimeStamp, Date endTimeStamp) {
\r
64 String beginTime = "";
\r
65 String endTime = "";
\r
66 String elapsedTime = "";
\r
68 if (beginTimeStamp != null && endTimeStamp != null) {
\r
69 elapsedTime = String.valueOf(endTimeStamp.getTime() - beginTimeStamp.getTime());
\r
70 beginTime = generateTimestampStr(beginTimeStamp);
\r
71 endTime = generateTimestampStr(endTimeStamp);
\r
74 MDC.put("BeginTimestamp", beginTime);
\r
75 MDC.put("EndTimestamp", endTime);
\r
76 MDC.put("ElapsedTime", elapsedTime);
\r
80 * Set response related logging variables in thread local data via MDC.
\r
83 * Response code ("0" indicates success)
\r
84 * @param description
\r
85 * Response description
\r
87 * class name of invoking class
\r
89 public static void setResponseContext(String code, String description, String className) {
\r
90 MDC.put("ResponseCode", code);
\r
91 MDC.put("StatusCode", code.equals("0") ? "COMPLETE" : "ERROR");
\r
92 MDC.put("ResponseDescription", description != null ? description : "");
\r
93 MDC.put("ClassName", className != null ? className : "");
\r
97 * Set target related logging variables in thread local data via MDC
\r
99 * @param targetEntity
\r
100 * Target entity (an external/sub component, for ex. "sdc")
\r
101 * @param targetServiceName
\r
102 * Target service name (name of API invoked on target)
\r
104 public static void setTargetContext(String targetEntity, String targetServiceName) {
\r
105 MDC.put("TargetEntity", targetEntity != null ? targetEntity : "");
\r
106 MDC.put("TargetServiceName", targetServiceName != null ? targetServiceName : "");
\r
110 * Set error related logging variables in thread local data via MDC.
\r
114 * @param description
\r
115 * Error description
\r
117 public static void setErrorContext(String code, String description) {
\r
118 MDC.put("ErrorCode", code);
\r
119 MDC.put("ErrorDescription", description != null ? description : "");
\r
122 private static String generateTimestampStr(Date timeStamp) {
\r
123 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
\r
124 TimeZone tz = TimeZone.getTimeZone("UTC");
\r
125 df.setTimeZone(tz);
\r
126 return df.format(timeStamp);
\r
130 * Get a previously stored RequestID for the thread local data via MDC. If
\r
131 * one was not previously stored, generate one, store it, and return that
\r
134 * @return A string with the request ID
\r
136 public static String getRequestId() {
\r
139 reqid = (String) MDC.get("RequestID");
\r
140 if (reqid == null || reqid.isEmpty()) {
\r
141 reqid = UUID.randomUUID().toString();
\r
142 MDC.put("RequestId", reqid);
\r