Merge "Update License.txt"
[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.apache.log4j.MDC;\r
33 \r
34 /**\r
35  * This class handles the special info that appear in the log, like RequestID,\r
36  * time context, ...\r
37  *\r
38  */\r
39 public class LoggingUtils {\r
40 \r
41     /**\r
42      * Set request related logging variables in thread local data via MDC\r
43      * \r
44      * @param service\r
45      *            Service Name of API (ex. "PUT template")\r
46      * @param partner\r
47      *            Partner name (client or user invoking API)\r
48      */\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
53     }\r
54 \r
55     /**\r
56      * Set time related logging variables in thread local data via MDC.\r
57      * \r
58      * @param beginTimeStamp\r
59      *            Start time\r
60      * @param endTimeStamp\r
61      *            End time\r
62      */\r
63     public static void setTimeContext(Date beginTimeStamp, Date endTimeStamp) {\r
64         String beginTime = "";\r
65         String endTime = "";\r
66         String elapsedTime = "";\r
67 \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
72         }\r
73 \r
74         MDC.put("BeginTimestamp", beginTime);\r
75         MDC.put("EndTimestamp", endTime);\r
76         MDC.put("ElapsedTime", elapsedTime);\r
77     }\r
78 \r
79     /**\r
80      * Set response related logging variables in thread local data via MDC.\r
81      * \r
82      * @param code\r
83      *            Response code ("0" indicates success)\r
84      * @param description\r
85      *            Response description\r
86      * @param className\r
87      *            class name of invoking class\r
88      */\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
94     }\r
95 \r
96     /**\r
97      * Set target related logging variables in thread local data via MDC\r
98      * \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
103      */\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
107     }\r
108 \r
109     /**\r
110      * Set error related logging variables in thread local data via MDC.\r
111      * \r
112      * @param code\r
113      *            Error code\r
114      * @param description\r
115      *            Error description\r
116      */\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
120     }\r
121 \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
127     }\r
128 \r
129     /**\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
132      * one.\r
133      * \r
134      * @return A string with the request ID\r
135      */\r
136     public static String getRequestId() {\r
137         String reqid;\r
138 \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
143         }\r
144         return reqid;\r
145     }\r
146 \r
147 }\r