Rework of the DCAE client
[clamp.git] / src / main / java / org / onap / clamp / clds / util / LoggingUtils.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP CLAMP\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights\r
6  *                             reserved.\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
11  *\r
12  * http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
22  */\r
23 \r
24 package org.onap.clamp.clds.util;\r
25 \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
31 \r
32 import org.jboss.logging.MDC;\r
33 \r
34 public class LoggingUtils {\r
35 \r
36     /**\r
37      * Set request related logging variables in thread local data via MDC\r
38      * \r
39      * @param service\r
40      *            Service Name of API (ex. "PUT template")\r
41      * @param partner\r
42      *            Partner name (client or user invoking API)\r
43      */\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
48     }\r
49 \r
50     /**\r
51      * Set time related logging variables in thread local data via MDC\r
52      * \r
53      * @param beginTimeStamp\r
54      *            Start time\r
55      * @param endTimeStamp\r
56      *            End time\r
57      */\r
58     public static void setTimeContext(Date beginTimeStamp, Date endTimeStamp) {\r
59         String beginTime = "";\r
60         String endTime = "";\r
61         String elapsedTime = "";\r
62 \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
67         }\r
68 \r
69         MDC.put("BeginTimestamp", beginTime);\r
70         MDC.put("EndTimestamp", endTime);\r
71         MDC.put("ElapsedTime", elapsedTime);\r
72     }\r
73 \r
74     /**\r
75      * Set response related logging variables in thread local data via MDC\r
76      * \r
77      * @param code\r
78      *            Response code ("0" indicates success)\r
79      * @param description\r
80      *            Response description\r
81      * @param className\r
82      *            class name of invoking class\r
83      */\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
89     }\r
90 \r
91     /**\r
92      * Set target related logging variables in thread local data via MDC\r
93      * \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
98      */\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
102     }\r
103 \r
104     /**\r
105      * Set error related logging variables in thread local data via MDC\r
106      * \r
107      * @param code\r
108      *            Error code\r
109      * @param description\r
110      *            Error description\r
111      */\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
115     }\r
116 \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
122     }\r
123 \r
124 }\r