Remove the demo from logging-library
[platform-oam.git] / logging-library / src / main / java / org / acumos / logging / library / util / LogConfig.java
1 /*
2 ===============LICENSE_START=======================================================
3 Acumos
4 ===================================================================================
5 Copyright (C) 2019 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6 ===================================================================================
7 This Acumos software file is distributed by AT&T and Tech Mahindra
8 under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11  
12            http://www.apache.org/licenses/LICENSE-2.0
13  
14 This file is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 ===============LICENSE_END=========================================================
19 */
20 package org.acumos.logging.library.util;
21
22 import java.net.InetAddress;
23 import java.util.Properties;
24 import java.util.UUID;
25
26 import org.acumos.logging.library.util.ACUMOSLogConstants.MDCs;
27 import org.acumos.logging.library.util.ACUMOSLogConstants.ResponseStatus;
28 import org.slf4j.MDC;
29
30 public class LogConfig {
31
32         public static void setEnteringMDCs(String targetEntry, String targetService, String user, String responseCode,
33                         String responseSeverity) throws Exception {
34                 String hostname = "";
35                 String ip = "";
36
37                 ACUMOSLogConstants.setDefaultMDCs();
38                 String requestId = UUID.randomUUID().toString();
39                 String finalHostIP = getHostDetails();
40                 if (finalHostIP != null) {
41                         String hostIpArr[] = finalHostIP.split("/");
42                         hostname = hostIpArr[0];
43                         ip = hostIpArr[1];
44                 }
45                 MDC.put(MDCs.REQUEST_ID, requestId);
46                 MDC.put(MDCs.TARGET_ENTITY, targetEntry);
47                 MDC.put(MDCs.TARGET_SERVICE_NAME, targetService);
48                 MDC.put(MDCs.CLIENT_IP_ADDRESS, ip);
49                 MDC.put(MDCs.SERVER_FQDN, hostname);
50
51                 if (user != null && !user.isEmpty()) {
52                         MDC.put(MDCs.USER, user);
53                 }
54
55                 if (responseCode != null && !responseCode.isEmpty()) {
56                         if (responseCode.equalsIgnoreCase(LoggingConstant.MDC_COMPLETED)) {
57                                 MDC.put(MDCs.RESPONSE_DESCRIPTION, MDCs.ResponseStatusMDC.MDC_COMPLETED.toString());
58                         } else if (responseCode.equalsIgnoreCase(LoggingConstant.MDC_ERROR)) {
59                                 MDC.put(MDCs.RESPONSE_DESCRIPTION, MDCs.ResponseStatusMDC.MDC_ERROR.toString());
60                         } else if (responseCode.equalsIgnoreCase(LoggingConstant.MDC_INPROGRESS)) {
61                                 MDC.put(MDCs.RESPONSE_DESCRIPTION, MDCs.ResponseStatusMDC.MDC_INPROGRESS.toString());
62                         }
63                 }
64
65                 if (responseSeverity != null && !responseSeverity.isEmpty()) {
66                         if (responseSeverity.equalsIgnoreCase(LoggingConstant.MDC_INFO)) {
67                                 MDC.put(MDCs.RESPONSE_SEVERITY, MDCs.ResponseSeverityMDC.MDC_INFO.toString());
68                         } else if (responseSeverity.equalsIgnoreCase(LoggingConstant.MDC_ERROR)) {
69                                 MDC.put(MDCs.RESPONSE_SEVERITY, MDCs.ResponseSeverityMDC.MDC_ERROR.toString());
70                         } else if (responseSeverity.equalsIgnoreCase(LoggingConstant.MDC_TRACE)) {
71                                 MDC.put(MDCs.RESPONSE_SEVERITY, MDCs.ResponseSeverityMDC.MDC_TRACE.toString());
72                         } else if (responseSeverity.equalsIgnoreCase(LoggingConstant.MDC_DEBUG)) {
73                                 MDC.put(MDCs.RESPONSE_SEVERITY, MDCs.ResponseSeverityMDC.MDC_DEBUG.toString());
74                         } else if (responseSeverity.equalsIgnoreCase(LoggingConstant.MDC_WARN)) {
75                                 MDC.put(MDCs.RESPONSE_SEVERITY, MDCs.ResponseSeverityMDC.MDC_WARN.toString());
76                         } else if (responseSeverity.equalsIgnoreCase(LoggingConstant.MDC_FATAL)) {
77                                 MDC.put(MDCs.RESPONSE_SEVERITY, MDCs.ResponseSeverityMDC.MDC_TRACE.toString());
78                         }
79                 }
80
81         }
82
83         public static void clearMDCDetails() {
84                 MDC.clear();
85         }
86
87         public static String getHostDetails() throws Exception {
88                 InetAddress ip;
89                 String hostname = null;
90                 String ipStr = null;
91                 String finalHostIP = null;
92
93                 ip = InetAddress.getLocalHost();
94                 hostname = ip.getHostName();
95                 ipStr = ip.getHostAddress();
96                 if (hostname != null && ipStr != null) {
97                         finalHostIP = hostname + "/" + ipStr;
98                 }
99                 return finalHostIP;
100         }
101 }