Cleanup of utils classes.
[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 import javax.validation.constraints.NotNull;\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 public final class LoggingUtils {\r
39 \r
40     private static final DateFormat DATE_FORMAT = createDateFormat();\r
41 \r
42     /**\r
43      * Private constructor to avoid creating instances of util class.\r
44      */\r
45     private LoggingUtils() {\r
46     }\r
47 \r
48     /**\r
49      * Set request related logging variables in thread local data via MDC\r
50      *\r
51      * @param service Service Name of API (ex. "PUT template")\r
52      * @param partner Partner name (client or user invoking API)\r
53      */\r
54     public static void setRequestContext(String service, String partner) {\r
55         MDC.put("RequestId", UUID.randomUUID().toString());\r
56         MDC.put("ServiceName", service);\r
57         MDC.put("PartnerName", partner);\r
58     }\r
59 \r
60     /**\r
61      * Set time related logging variables in thread local data via MDC.\r
62      *\r
63      * @param beginTimeStamp Start time\r
64      * @param endTimeStamp End time\r
65      */\r
66     public static void setTimeContext(@NotNull Date beginTimeStamp, @NotNull Date endTimeStamp) {\r
67         MDC.put("BeginTimestamp", generateTimestampStr(beginTimeStamp));\r
68         MDC.put("EndTimestamp", generateTimestampStr(endTimeStamp));\r
69         MDC.put("ElapsedTime", String.valueOf(endTimeStamp.getTime() - beginTimeStamp.getTime()));\r
70     }\r
71 \r
72     /**\r
73      * Set response related logging variables in thread local data via MDC.\r
74      *\r
75      * @param code Response code ("0" indicates success)\r
76      * @param description Response description\r
77      * @param className class name of invoking class\r
78      */\r
79     public static void setResponseContext(String code, String description, String className) {\r
80         MDC.put("ResponseCode", code);\r
81         MDC.put("StatusCode", code.equals("0") ? "COMPLETE" : "ERROR");\r
82         MDC.put("ResponseDescription", description != null ? description : "");\r
83         MDC.put("ClassName", className != null ? className : "");\r
84     }\r
85 \r
86     /**\r
87      * Set target related logging variables in thread local data via MDC\r
88      *\r
89      * @param targetEntity Target entity (an external/sub component, for ex. "sdc")\r
90      * @param targetServiceName Target service name (name of API invoked on target)\r
91      */\r
92     public static void setTargetContext(String targetEntity, String targetServiceName) {\r
93         MDC.put("TargetEntity", targetEntity != null ? targetEntity : "");\r
94         MDC.put("TargetServiceName", targetServiceName != null ? targetServiceName : "");\r
95     }\r
96 \r
97     /**\r
98      * Set error related logging variables in thread local data via MDC.\r
99      *\r
100      * @param code Error code\r
101      * @param description Error description\r
102      */\r
103     public static void setErrorContext(String code, String description) {\r
104         MDC.put("ErrorCode", code);\r
105         MDC.put("ErrorDescription", description != null ? description : "");\r
106     }\r
107 \r
108     private static String generateTimestampStr(Date timeStamp) {\r
109         return DATE_FORMAT.format(timeStamp);\r
110     }\r
111 \r
112     /**\r
113      * Get a previously stored RequestID for the thread local data via MDC. If\r
114      * one was not previously stored, generate one, store it, and return that\r
115      * one.\r
116      *\r
117      * @return A string with the request ID\r
118      */\r
119     public static String getRequestId() {\r
120         String requestId;\r
121 \r
122         requestId = (String) MDC.get("RequestID");\r
123         if (requestId == null || requestId.isEmpty()) {\r
124             requestId = UUID.randomUUID().toString();\r
125             MDC.put("RequestId", requestId);\r
126         }\r
127         return requestId;\r
128     }\r
129 \r
130     private static DateFormat createDateFormat() {\r
131         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");\r
132         dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));\r
133         return dateFormat;\r
134     }\r
135 \r
136 }\r