aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/logging/AAILogger.java
blob: 2a87d1600708b7de93a49c1dc8b466edfb6df64d (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.openecomp.aai.logging;

import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.util.AAIConfig;
import org.openecomp.aai.util.AAIConstants;
import org.slf4j.MDC;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

/**
 * This class provides Logger methods for the AAI application
 */
@SuppressWarnings("rawtypes")
public class AAILogger {

	public EELFLogger logger;

	// private boolean mdcInitialized = false;

	/**
	 * Instantiates a new AAI logger.
	 *
	 * @param name the name
	 */
	public AAILogger(String name) {
		// if ( !mdcInitialized ) { address problem where host/ip not in most
		// logfile entries
		MDC.put("ERROR_CODE", "");
		MDC.put("ERROR_TEXT", "");
		if (MDC.get("hostaddress") == null) {
			mdcSetUp();
			// mdcInitialized = true;
		}
		this.logger = EELFManager.getInstance().getLogger(name);
	}

	/**
	 * Instantiates a new AAI logger.
	 *
	 * @param clazz the clazz
	 */
	public AAILogger(Class clazz) {
		this(clazz.getSimpleName());
	}

	/**
	 * Mdc set up.
	 */
	private void mdcSetUp() {
		InetAddress ip;
		String hostname;

		MDC.put("hostname", "");
		MDC.put("hostaddress", "");
	
		try {
			ip = InetAddress.getLocalHost();
			if (ip != null) {
				hostname = ip.getCanonicalHostName();
				if (hostname != null)
					MDC.put("hostname", hostname);
				MDC.put("hostaddress", ip.getHostAddress());
			}
			// System.out.println("MDC setup " + MDC.get("hostname"));
		} catch (UnknownHostException e) {

			e.printStackTrace();

		}
	}

	/**
	 * Method isInfoEnabled.
	 * 
	 * @return boolean
	 */
	public boolean isInfoEnabled() {
		return logger.isInfoEnabled();
	}

	/**
	 * Method isDebugEnabled.
	 * 
	 * @return boolean
	 */
	public boolean isDebugEnabled() {
		return logger.isDebugEnabled();
	}

	/**
	 * Method debug.
	 * 
	 * @param logline
	 *            LogLine
	 * @param text
	 *            String
	 */
	public void debug(LogLine logline, String text) {
		if (isDebugEnabled()) {
			logline.setLevel("DEBUG");
			logline.setUserContributed(text);
			String msg = logline.getLine(false).replaceAll("\\n", "^");// make it
																	// more
																	// readable
																	// by
																	// replacing
																	// newlines
			logger.debug(msg);
		}
	}

	/**
	 * Method debug.
	 * 
	 * @param logline
	 *            LogLine
	 * @param text
	 *            String
	 * @param t
	 *            Throwable
	 */
	public void debug(LogLine logline, String text, Throwable t) {
		if (isDebugEnabled()) {
			logline.setLevel("DEBUG");
			logline.add("db", text);
			String msg = logline.getLine(false).replaceAll("\\n", "^");// make it
																	// more
																	// readable
																	// by
																	// replacing
																	// newlines
			logger.debug(msg, t);

			Throwable nestedT = getNestedThrowable(t);
			if (nestedT != null) {
				logger.debug(logline + "More info on previous error: ", nestedT);
			}
		}
	}
	
	/**
	 * Method audit.
	 * 
	 * @param logline
	 *            LogLine
	 */
	public void audit(LogLine logline) {
		if (isInfoEnabled()) {
			logline.setLevel("INFO");
			String msg = logline.getLine(true);
			logger.info(msg);
		}
	}

	/**
	 * Method info.
	 * 
	 * @param logline
	 *            LogLine
	 * @param success
	 *            Boolean true or false
	 * @param errorCode 
	 * 	 		"0" for success=true and a valid "AAI_*" error code from error.properties for success=false
	 *           ex. 			aaiLogger.info(logline, false, "AAI_7402", e);
	 *                          aaiLogger.info(logline, true, "0");
	 *                          aaiLogger.info(logline, false, e.getErrorObject().getErrorCodeString(), e);
	 */
	public void info(LogLine logline, Boolean success, String errorCode) {
		info(logline, success, errorCode, null);
	}

	/**
	 * Method info.
	 * 
	 * @param logline
	 *            LogLine
	 * @param success
	 *            Boolean true or false
	 * @param errorCode 
	 * 			"0" for success=true and a valid "AAI_*" error code from error.properties for success=false
	 *           ex. 			aaiLogger.info(logline, false, "AAI_7402", e);
	 *                          aaiLogger.info(logline, true, "0");
	 *                          aaiLogger.info(logline, false, e.getErrorObject().getErrorCodeString(), e);
	 * @param t
	 *            Throwable
	 */
	public void info(LogLine logline, Boolean success, String errorCode, Throwable t) {
		if (isInfoEnabled()) {
			logline.setLevel("INFO");
			if (errorCode.equals("0")) {
				logline.setEc(errorCode);
				//Set Response Description to "Success" if error code is 0.
				if (success){
					logline.setEt("Success");
				}
			}
			else {
				ErrorObject errorObject = ErrorLogHelper.getErrorObject(errorCode);
				logline.setEc(errorObject.getErrorCodeString());
				logline.setEt(errorObject.getErrorText()); 
			}
			
			if (t != null) {
				Throwable nestedT = getNestedThrowable(t);
				if (nestedT != null) {
					logline.add("info", nestedT.getStackTrace().toString());
				}
			}
			String msg = logline.finish(success);
			logger.info(msg);
		}
	}

	/**
	 * Method error.
	 * 
	 * @param errorObject
	 *            ErrorObject
	 * @param logline
	 *            LogLine
	 */
	@Deprecated
	public void error(ErrorObject errorObject, LogLine logline) {
		error(errorObject, logline, null);
	}

	/**
	 * Method error.
	 * 
	 * @param errorObject
	 *            ErrorObject
	 * @param logline
	 *            LogLine
	 * @param t
	 *            Throwable
	 */
	public void error(ErrorObject errorObject, LogLine logline, Throwable t) {

		String errorSeverity = errorObject.getSeverity();
		if (errorSeverity.equalsIgnoreCase("WARN"))
			logline.setLevel("WARN");
		else if (errorSeverity.equalsIgnoreCase("ERROR"))
			logline.setLevel("ERROR");
		else if (errorSeverity.equalsIgnoreCase("FATAL"))
			logline.setLevel("FATAL");

		String errorMessage = errorObject.getErrorText() + ":"
				+ errorObject.getRESTErrorCode() + ":" + errorObject.getHTTPResponseCode();
		if (errorObject.getDetails() != null)
			errorMessage += ":" + errorObject.getDetails();
		errorMessage = errorMessage.replaceAll("\\n", "^");
		MDC.put("ERROR_CODE", errorObject.getErrorCodeString());
		MDC.put("ERROR_TEXT", errorMessage);
		try {
			if (t != null)
				errorMessage += ":" + getStackTop(t);
		} catch (AAIException e) {
			errorMessage += ": unable to get stack trace, " + e.getMessage() + ":" + e.getErrorObject().getErrorText();
		}
		errorMessage = errorMessage.replaceAll("\\n", "^");

		logline.setEc(errorObject.getErrorCodeString());
		logline.setEt(errorMessage);
		
		//Set status code correctly
		logline.setSs("ERROR");


		if (errorSeverity.equalsIgnoreCase("WARN"))
			warn(logline.getLine(false));
		else if (errorSeverity.equalsIgnoreCase("ERROR"))
			error(logline.getLine(false));
		else if (errorSeverity.equalsIgnoreCase("FATAL"))
			fatal(logline.getLine(false));
		// logNestedException(Level.DEBUG, errorMessage, t);
	}

	/**
	 * Method warn.
	 * 
	 * @param logline
	 *            String
	 */
	private void warn(String logline) {
		logger.warn(logline);
	}

	/**
	 * Method error.
	 * 
	 * @param logline
	 *            String
	 */
	private void error(String logline) {
		logger.error(logline);
	}

	/**
	 * Method fatal.
	 * 
	 * @param logline
	 *            String
	 */
	private void fatal(String logline) {
		logger.error(logline);
	}

	// /**
	// * Method logNestedException.
	// *
	// * @param level
	// * Level
	// * @param logline
	// * String
	// * @param t
	// * Throwable
	// */
	// private void logNestedException(Level level, String logline, Throwable t)
	// {
	// if (null == t) {
	// return;
	// }
	//
	// try {
	// Class tC = t.getClass();
	// Method[] mA = tC.getMethods();
	// Method nextThrowableMethod = null;
	//
	// for (int i = 0; i < mA.length; i++) {
	// if (("getCause".equals(mA[i].getName())) ||
	// "getRootCause".equals(mA[i].getName())
	// || "getNextException".equals(mA[i].getName()) ||
	// "getException".equals(mA[i].getName())) {
	// // check param types
	// Class[] params = mA[i].getParameterTypes();
	//
	// if ((null == params) || (0 == params.length)) {
	// // just found the getter for the nested throwable
	// nextThrowableMethod = mA[i];
	//
	// break; // no need to search further
	// }
	// }
	// }
	//
	// if (null != nextThrowableMethod) {
	// // get the nested throwable and log it
	// Throwable nextT = (Throwable) nextThrowableMethod.invoke(t, new
	// Object[0]);
	//
	// if (null != nextT) {
	// this.logger.log(AAICLASS, level, logline + "More info on previous error:
	// ", nextT);
	// }
	// }
	// } catch (Exception e) {
	// // do nothing
	// }
	// }

	/**
	 * This method returns the nested throwable of the given throwable.
	 *
	 * @param t            Throwable
	 * @return the nested throwable
	 */
	private Throwable getNestedThrowable(Throwable t) {
		if (null == t) {
			return null;
		}

		try {
			Class tC = t.getClass();
			Method[] mA = tC.getMethods();
			Method nextThrowableMethod = null;

			for (int i = 0; i < mA.length; i++) {
				if (("getCause".equals(mA[i].getName())) || "getRootCause".equals(mA[i].getName())
						|| "getNextException".equals(mA[i].getName()) || "getException".equals(mA[i].getName())) {
					// check param types
					Class[] params = mA[i].getParameterTypes();

					if ((null == params) || (0 == params.length)) {
						// just found the getter for the nested throwable
						nextThrowableMethod = mA[i];

						break; // no need to search further
					}
				}
			}

			if (null != nextThrowableMethod) {
				// get the nested throwable and log it
				return (Throwable) nextThrowableMethod.invoke(t, new Object[0]);

			}
		} catch (Exception e) {
			// do nothing
		}

		return null;
	}

	/**
	 * Gets the stack top.
	 *
	 * @param e the e
	 * @return the stack top
	 * @throws NumberFormatException the number format exception
	 * @throws AAIException the AAI exception
	 */
	public String getStackTop(Throwable e) throws NumberFormatException, AAIException {
		StringBuffer stackMessage = new StringBuffer();
		int maxStackTraceEntries = Integer.valueOf(AAIConfig.get(AAIConstants.LOGGING_MAX_STACK_TRACE_ENTRIES));
		if (e != null) {
			Throwable rootCause = ExceptionUtils.getRootCause(e);
			if (rootCause != null) {
				stackMessage.append("root cause=" + ExceptionUtils.getRootCause(e));
				StackTraceElement[] elements = rootCause.getStackTrace();
				int i = 0;
				for (StackTraceElement element : elements) {
					if (i < maxStackTraceEntries) {
						stackMessage.append(" ClassName- ");
						stackMessage.append(element.getClassName());
						stackMessage.append(" :LineNumber- ");
						stackMessage.append(element.getLineNumber());
						stackMessage.append(" :MethodName- ");
						stackMessage.append(element.getMethodName());
					}
					i++;
				}
			} else if (e.getCause() != null) {
				stackMessage.append("cause=" + e.getCause());
				StackTraceElement[] elements = e.getCause().getStackTrace();
				int i = 0;
				for (StackTraceElement element : elements) {
					if (i < maxStackTraceEntries) {
						stackMessage.append(" ClassName- ");
						stackMessage.append(element.getClassName());
						stackMessage.append(" :LineNumber- ");
						stackMessage.append(element.getLineNumber());
						stackMessage.append(" :MethodName- ");
						stackMessage.append(element.getMethodName());
					}
					i++;
				}
			} else if (e.getStackTrace() != null) {
				stackMessage.append("ex=" + e.toString());
				StackTraceElement[] elements = e.getStackTrace();
				int i = 0;
				for (StackTraceElement element : elements) {
					if (i < maxStackTraceEntries) {
						stackMessage.append(" ClassName- ");
						stackMessage.append(element.getClassName());
						stackMessage.append(" :LineNumber- ");
						stackMessage.append(element.getLineNumber());
						stackMessage.append(" :MethodName- ");
						stackMessage.append(element.getMethodName());
					}
					i++;
				}
			}
		}
		return stackMessage.toString();
	}
}