aboutsummaryrefslogtreecommitdiffstats
path: root/appc-common/src/main/java/org/onap/appc/logging/LoggingUtils.java
blob: c37a9a5a4a53fd8ccaba964956c00dc82bcc3fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.logging;

import org.onap.appc.i18n.Msg;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.att.eelf.i18n.EELFResolvableErrorEnum;
import com.att.eelf.i18n.EELFResourceManager;
import org.slf4j.MDC;
import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

/**
 * Logging utilities
 */
public class LoggingUtils {

    private static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
    private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
    private static final EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();

    private LoggingUtils() {
        throw new IllegalAccessError("LoggingUtils");
    }

    public static void logErrorMessage(String errorCode, String errorDescription,
            String targetEntity, String targetServiceName, String additionalMessage,
            String className) {
        logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage,
                className);
    }

    public static void logErrorMessage(String targetEntity, String targetServiceName,
            String additionalMessage, String className) {
        logError("", "", targetEntity, targetServiceName, additionalMessage, className);
    }

    public static void logErrorMessage(String targetServiceName, String additionalMessage,
            String className) {
        logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage,
                className);
    }

    private static void logError(String errorCode, String errorDescription, String targetEntity,
            String targetServiceName, String additionalMessage, String className) {
        populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName,
                className);
        errorLogger.error(additionalMessage == null ? "" : additionalMessage);
        cleanErrorLogContext();
    }

    public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code,
            String responseDescription, String className) {
        populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
        auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG, MDC.get(MDC_SERVICE_NAME),
                MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
                MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
                MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP),
                MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP),
                MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)));
        cleanAuditErrorContext();
    }

    public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code,
            String responseDescription, String className, EELFResolvableErrorEnum resourceId,
            String... arguments) {
        populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
        auditLogger.info(resourceId, arguments);
        cleanAuditErrorContext();
    }

    public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code,
            String responseDescription, String className, EELFResolvableErrorEnum resourceId,
            String... arguments) {
        populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
        auditLogger.warn(resourceId, arguments);
        cleanAuditErrorContext();
    }

    public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp,
            String targetEntity, String targetServiceName, String statusCode, String responseCode,
            String responseDescription, String className) {
        populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName,
                statusCode, responseCode, responseDescription, className);
        metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG, MDC.get(MDC_SERVICE_NAME),
                MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
                MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
                MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
                MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
                MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
                MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
        cleanMetricContext();
    }

    private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp,
            String code, String responseDescription, String className) {
        populateTimeContext(beginTimeStamp, endTimeStamp);
        populateRequestContext();
        String statusCode = ("100".equals(code) || "400".equals(code))
                ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR;
        populateResponseContext(statusCode, code, responseDescription );
        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
    }

    private static void cleanAuditErrorContext() {
        cleanRequestContext();
        cleanTimeContext();
        cleanResponseContext();
        MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
    }

    private static void populateErrorLogContext(String errorCode, String errorDescription,
            String targetEntity, String targetServiceName, String className) {
        populateErrorContext(errorCode, errorDescription);
        populateTargetContext(targetEntity, targetServiceName != null ? targetServiceName : "");
        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
    }

    private static void cleanErrorLogContext() {
        cleanErrorContext();
        cleanTargetContext();
        MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
    }

    private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp,
            String targetEntity, String targetServiceName, String statusCode, String responseCode,
            String responseDescription, String className) {
        populateRequestContext();
        populateTimeContext(beginTimeStamp, endTimeStamp);
        populateTargetContext(targetEntity, targetServiceName);
        populateResponseContext(statusCode, responseCode, responseDescription);
        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
    }

    private static void cleanMetricContext() {
        cleanRequestContext();
        cleanTimeContext();
        cleanTargetContext();
        cleanResponseContext();
        MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
    }

    private static void populateTargetContext(String targetEntity, String targetServiceName) {
        MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity != null ? targetEntity : "");
        MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME,
                targetServiceName != null ? targetServiceName : "");
    }

    private static void cleanTargetContext() {
        MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
        MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
    }

    private static void populateRequestContext() {
        try {
            UUID.fromString(MDC.get(MDC_KEY_REQUEST_ID));
            //reaching here without exception means existing RequestId is
            //valid UUID as per ECOMP logging standards, no-op
        } catch (Exception e) {
            MDC.put(MDC_KEY_REQUEST_ID, UUID.randomUUID().toString());
        }

        try {
            String partnerName = MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME);

            //ECOMP logging standards require some value for PartnerName.  Default to appc if empty
            if (partnerName.isEmpty())
                MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, "appc");
        } catch (Exception e) {
            MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, "appc");
        }

        try {
            String serviceName = MDC.get(MDC_SERVICE_NAME);

            //ECOMP logging standards require some value for ServiceName.  Default to DEFAULT if empty
            if (serviceName.isEmpty())
                MDC.put(MDC_SERVICE_NAME, "DEFAULT");
        } catch (Exception e) {
            MDC.put(MDC_SERVICE_NAME, "DEFAULT");
        }
    }

    private static void cleanRequestContext() {
        MDC.remove(MDC_KEY_REQUEST_ID);
        MDC.remove(LoggingConstants.MDCKeys.PARTNER_NAME);
        MDC.remove(MDC_SERVICE_NAME);
    }
    private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
        String beginTime = "";
        String endTime = "";
        String elapsedTime = "";

        if (beginTimeStamp != null && endTimeStamp != null) {
            elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
            beginTime = generateTimestampStr(beginTimeStamp);
            endTime = generateTimestampStr(endTimeStamp);
        }

        MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
        MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
        MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
    }

    public static String generateTimestampStr(Instant timeStamp) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
        TimeZone tz = TimeZone.getTimeZone("UTC");
        df.setTimeZone(tz);
        return df.format(Date.from(timeStamp));
    }

    private static void cleanTimeContext() {
        MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
        MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
        MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
    }

    private static void populateResponseContext(String statusCode, String responseCode,
            String responseDescription) {
        MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode != null ? statusCode : "");
        MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
        MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
                responseDescription != null ? responseDescription : "");
    }

    private static void cleanResponseContext() {
        MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
        MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
        MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
    }

    private static void populateErrorContext(String errorCode, String errorDescription) {
        MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
        MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
    }

    private static void cleanErrorContext() {
        MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
        MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);
    }

}