aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/logging/LogLine.java
blob: 50149e69bf8a4d7c01b08335de58d2f16a726b98 (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
/*-
 * ============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.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.slf4j.MDC;
/**
 * This class is used to help standardize how log lines are written and provide profiling info.
 *
 * Note that the name-value pairs are assembled in the order they are inserted.. Init sets up the tr, fr and co values 
 * so that they can be used consistently via start() for debug and error logs. finish() adds the ss and tt so they
 * can be provided for each INFO record.
 */

public class LogLine {

	private long startTime = 0;
	private long endTime = 0;
	
	private String co = ""; // component
	private String tr = ""; // transId
	private String ll = ""; // log level
	
	private String fr = ""; // fromAppId
	private String to = ""; // toAppId
	private String me = ""; // operation
	
	private String tt = ""; // time taken
	private String ss = ""; // success
	private String ec = "0"; // error code
	private String et = ""; // error text 
	
	
	private String userContributed = "";
	
	/**
	 *  Initialize the start time and identify the component, transactionId and fromAppId
	 * which are mandatory to log on each line.
	 *
	 * @param component identifies the subsystem or component of the application
	 * @param transId identifies the unique transaction id for the request being processed
	 * @param fromAppId identifies the application that is making the request
	 * @param operation the operation
	 */
	

	
	public void init(String component, String transId, String fromAppId, String operation){

		init(component, transId, fromAppId, "AAI", operation);
	}
	
	/**
	 * Inits the logline.
	 *
	 * @param component the component
	 * @param transId the trans id
	 * @param fromAppId the from app id
	 * @param toAppId the to app id
	 * @param operation the operation
	 */
	public void init(String component, String transId, String fromAppId, String toAppId, String operation){

		this.setUserContributed("");
		startTime = System.currentTimeMillis();
		this.setCo(component);
		this.setTr(transId);
		this.setFr(fromAppId);
		this.setTo(toAppId);
		this.setMe(operation);
	}
		
	/**
	 *  Overrides the value from init().
	 */
	public void startTimer() {
		startTime = System.currentTimeMillis();
	}
	
	/**
	 * Adds the.
	 *
	 * @param name the name
	 * @param value the value
	 */
	public void add(String name, String value) {
		String uc = getUserContributed();
		if (value != null) {
			uc += ":" + name + "=" + value.replaceAll("\\|", "^").trim();
		} else { 
			uc += ":" + name + "=" + value;
		}
		setUserContributed(uc);
	}

	/**
	 * Adds the.
	 *
	 * @param name the name
	 * @param value the value
	 */
	public void add(String name, int value) {
		String uc = getUserContributed();
		uc += ":" + name + "=" +  Integer.toString(value);
		setUserContributed(uc);
	}

	/**
	 * Adds the.
	 *
	 * @param name the name
	 * @param value the value
	 */
	public void add(String name, long value) {
		String uc = getUserContributed();
		uc += ":" + name + "=" +  Long.toString(value);
		setUserContributed(uc);
	}
	
	/**
	 * Adds the.
	 *
	 * @param name the name
	 * @param value the value
	 */
	public void add(String name, double value) {
		String uc = getUserContributed();
		uc += ":" + name + "=" +  Double.toString(value);
		setUserContributed(uc);
	}

	/**
	 * Adds the.
	 *
	 * @param name the name
	 * @param value the value
	 */
	public void add(String name, boolean value) {
		String uc = getUserContributed();
		uc += ":" + name + "=" +  Boolean.toString(value);
		setUserContributed(uc);
	}
	
	/**
	 *  Return the log line based on what we have so far.
	 *
	 * @param audit the audit
	 * @return the line
	 */
	protected String getLine(boolean audit) {
	
	// MDC is setup when AAILogger is setup
	String hostName = (String)MDC.get("hostname")== null ? "" : (String)MDC.get("hostname");
	String hostAddress = (String)MDC.get("hostaddress") == null ? "" :  (String)MDC.get("hostaddress");
	//String className = (String)MDC.get("classname") == null ? "" :  (String)MDC.get("classname");
	String className = "";
	
	Exception ex = new Exception();
	StackTraceElement[] stackTraceArray = ex.getStackTrace();
	
	//Find the class in the stack trace that calls the AAILogger.
	for (int i =0; i < stackTraceArray.length; i++ ){
		String fullClassName = stackTraceArray[i].getClassName();
		if (fullClassName.contains("AAILogger")){
			int aaiLoggerIndex = i;
			String nextClassInStackTrace = stackTraceArray[i+1].getClassName();
			if (nextClassInStackTrace.contains("AAILogger")){
				fullClassName = stackTraceArray[i+2].getClassName();
			} else {
				fullClassName = nextClassInStackTrace;
			}
			className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1 );
			break;
		}
	}
	
//	if (ex.getStackTrace().length > 3) { // 3rd in stack shd be aaiLogger so we need the 4th
//			String fullClassName = ex.getStackTrace()[3].getClassName();
//			if (!fullClassName.contains("aai"))
//				fullClassName = ex.getStackTrace()[2].getClassName();
//			className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1 );
//	}	
	
		// this will format it 
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
	dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
	
	Date startDate = new Date(startTime);
	String startTimeStr = dateFormat.format(startDate);	
	
	Date endDate = new Date(endTime);
	String endTimeStr = dateFormat.format(endDate);	
	
	String timeTaken = this.getTt();
	
	if (!ll.equals("INFO")) //if this is debug or error, use the now time
	{
		// "endtime" will now be equal to when the log is written (nowtime )	
		long nowTime = System.currentTimeMillis();
		timeTaken = String.valueOf(nowTime - startTime); //tt is "time taken" or "elapsed time"
		Date nowDate = new Date (nowTime);
		endTimeStr = dateFormat.format(nowDate);	
	
	}
							
		
	return    startTimeStr  				 				+ // start timestamp 
				"|"  + endTimeStr  			 				+ // end timestamp
				"|"  + this.getTr()          				+ // RequestId              					=> transId
				"|"                          				+ // serviceInstanceId      					=> NA
				"|"                          				+ // threadId               					=> NA 
				"|"                          				+ // physical server name   					=> NA
				"|" + this.getMe()           				+ // serviceName method       					=> operation
				"|" + this.getFr()            	            + // partnerName from                           => fromAppId  
				(audit ? "" :"|" + this.getTo())    	    + // TargetEntity                               => toAppID
				(audit ? "" :"|")           				+ //TargetServiceName							=> NA 
				"|"	+ this.getSs()					 		+ //StatusCode/success							=> NA 
				"|"	+ this.getEc()					 		+ //Response Code, is our error code			=> NA 
				"|"	+ this.getEt()						 	+ // Response Description, is our error text 	=> NA 
				"|"                          				+ // instanceUUID          					=> NA
				"|" + this.getLevel()        				+ // category               					=> loglevel 
				"|"                          				+ // severity               					=> NA
				"|" + hostAddress	         				+ // Server IP address      					=> hostAddress 
				"|" + timeTaken           	 				+ // Timer                  					=> tt
				"|" + hostName				 				+ // Server                 					=> hostName 
				"|" 					     				+ // IP Address 								=> NA 
				"|" + className			     				+ // className              					=> className
				"|"						 	 				+ //Unused 									=> NA 
				"|"							 				+ //ProcessKey									=> NA 
				(audit ? "": "|")			 				+ //TargetVirtualEntity 						=> NA 
				"|"							 				+ //CustomField1 								=> NA 
				"|"							 				+ //CustomField2 								=> NA 
				"|"							 				+ //CustomField3 								=> NA 
				"|"							 				+ //CustomField4 								=> NA 
				"|co=" + this.getCo()        				+ // DetailMessage component                    => component		
				    ":" + this.getUserContributed() 		+ "|";
	

	}
			
	
	/**
	 *  Return the finished log line, including success and elapsed time. 
	 * This should be called at the end for INFO logs
	 *
	 * @param success the success
	 * @return the string
	 */
	public String finish(boolean success) {
		endTime = System.currentTimeMillis();
		setSs((success ? "COMPLETE" : "ERROR"));
		setTt(String.valueOf(endTime - startTime)); // tt is "time taken" or "elapsed time" 
		return getLine(false);
	}

	/**
	 * Gets the co.
	 *
	 * @return the co
	 */
	private String getCo() {
		return co;
	}

	/**
	 * Sets the co.
	 *
	 * @param co the new co
	 */
	private void setCo(String co) {
		this.co = co;
	}

	/**
	 * Gets the tr.
	 *
	 * @return the tr
	 */
	private String getTr() {
		return tr;
	}

	/**
	 * Sets the tr.
	 *
	 * @param tr the new tr
	 */
	private void setTr(String tr) {
		this.tr = tr;
	}
	
	/**
	 * Gets the level.
	 *
	 * @return the level
	 */
	private String getLevel() {
		return ll;
	}

	/**
	 * Sets the level.
	 *
	 * @param ll the new level
	 */
	public void setLevel(String ll) {
		this.ll = ll;
	}

	/**
	 * Gets the fr.
	 *
	 * @return the fr
	 */
	private String getFr() {
		return fr;
	}

	/**
	 * Sets the fr.
	 *
	 * @param fr the new fr
	 */
	private void setFr(String fr) {
		this.fr = fr;
	}
	
	/**
	 * Gets the to.
	 *
	 * @return the to
	 */
	private String getTo() {
		return to;
	}

	/**
	 * Sets the to.
	 *
	 * @param to the new to
	 */
	private void setTo(String to) {
		this.to = to;
	}
	
	/**
	 * Gets the me.
	 *
	 * @return the me
	 */
	private String getMe() {
		return me;
	}

	/**
	 * Sets the me.
	 *
	 * @param me the new me
	 */
	private void setMe(String me) {
		this.me = me;
	}

	/**
	 * Gets the tt.
	 *
	 * @return the tt
	 */
	private String getTt() {
		return tt;
	}

	/**
	 * Sets the tt.
	 *
	 * @param tt the new tt
	 */
	private void setTt(String tt) {
		this.tt = tt;
	}

	/**
	 * Gets the ss.
	 *
	 * @return the ss
	 */
	private String getSs() {
		return ss;
	}

	/**
	 * Sets the ss.
	 *
	 * @param ss the new ss
	 */
	protected void setSs(String ss) {
		this.ss = ss;
	}
	
	/**
	 * Sets the ss.
	 *
	 * @param ss the new ss
	 */
	public void setSs(Boolean ss) {
		if (ss)
			this.ss = "y";
		else
			this.ss = "n";
	}
	
	/**
	 * Gets the ec.
	 *
	 * @return the ec
	 */
	public String getEc() {
		return ec;
	}

	/**
	 * Sets the ec.
	 *
	 * @param ec the new ec
	 */
	public void setEc(String ec) {
		this.ec = ec;
	}
	
	/**
	 * Gets the et.
	 *
	 * @return the et
	 */
	public String getEt() {
		return et;
	}

	/**
	 * Sets the et.
	 *
	 * @param et the new et
	 */
	public void setEt(String et) {
		this.et = et;
	}


	/**
	 * Gets the user contributed.
	 *
	 * @return the user contributed
	 */
	private String getUserContributed() {
		return userContributed;
	}

	/**
	 * Sets the user contributed.
	 *
	 * @param userContributed the new user contributed
	 */
	protected void setUserContributed(String userContributed) {
		this.userContributed = userContributed;
	}
		
}