Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / logging / LogFormatTools.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License 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.onap.aai.logging;
21
22 import java.time.Instant;
23 import java.time.ZoneOffset;
24 import java.time.ZonedDateTime;
25 import java.time.format.DateTimeFormatter;
26 import org.onap.aai.util.AAIConfig;
27 import org.onap.aai.util.AAIConstants;
28 import org.onap.aai.exceptions.AAIException;
29 import org.apache.commons.lang.exception.ExceptionUtils;
30
31 public class LogFormatTools {
32
33         private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
34         private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern(DATE_FORMAT)
35                                                                                                                                         .withZone(ZoneOffset.UTC);
36
37         public static String getCurrentDateTime() {
38                 return DTF.format(ZonedDateTime.now());
39         }
40
41         public static String toDate(long timestamp) {
42                 return DTF.format(Instant.ofEpochMilli(timestamp));
43         }
44
45         public static long toTimestamp(String date) {
46                 return ZonedDateTime.parse(date, DTF).toInstant().toEpochMilli();
47         }
48         /**
49          * Gets the stack top.
50          *
51          * @param e the e
52          * @return the stack top
53          * @throws NumberFormatException the number format exception
54          * @throws AAIException the AAI exception
55          */
56         public static String getStackTop(Throwable e) {
57             // StringBuilder is more efficient than StringBuffer and should only
58                 // StringBuffer is only supposed to be used if multiple threads are modifying
59                 // the same object and since this object is created locally not necessary
60                 StringBuilder stackMessage = new StringBuilder();
61                 int maxStackTraceEntries = 10;
62                 try {
63                         maxStackTraceEntries = Integer.valueOf(AAIConfig.get(AAIConstants.LOGGING_MAX_STACK_TRACE_ENTRIES));
64                 }
65                 catch (AAIException a) {
66                         //ignore, use default
67                 }
68                 catch (NumberFormatException n) {
69                         //ignore, use default
70                 }
71                 if (e != null) {
72                         Throwable rootCause = ExceptionUtils.getRootCause(e);
73                         if (rootCause != null) {
74                                 stackMessage.append("root cause=" + ExceptionUtils.getRootCause(e));
75                                 StackTraceElement[] elements = rootCause.getStackTrace();
76                                 int i = 0;
77                                 for (StackTraceElement element : elements) {
78                                         if (i < maxStackTraceEntries) {
79                                                 stackMessage.append(" ClassName- ");
80                                                 stackMessage.append(element.getClassName());
81                                                 stackMessage.append(" :LineNumber- ");
82                                                 stackMessage.append(element.getLineNumber());
83                                                 stackMessage.append(" :MethodName- ");
84                                                 stackMessage.append(element.getMethodName());
85                                         }
86                                         i++;
87                                 }
88                         } else if (e.getCause() != null) {
89                                 stackMessage.append("cause=" + e.getCause());
90                                 StackTraceElement[] elements = e.getCause().getStackTrace();
91                                 int i = 0;
92                                 for (StackTraceElement element : elements) {
93                                         if (i < maxStackTraceEntries) {
94                                                 stackMessage.append(" ClassName- ");
95                                                 stackMessage.append(element.getClassName());
96                                                 stackMessage.append(" :LineNumber- ");
97                                                 stackMessage.append(element.getLineNumber());
98                                                 stackMessage.append(" :MethodName- ");
99                                                 stackMessage.append(element.getMethodName());
100                                         }
101                                         i++;
102                                 }
103                         } else if (e.getStackTrace() != null) {
104                                 stackMessage.append("ex=" + e.toString());
105                                 StackTraceElement[] elements = e.getStackTrace();
106                                 int i = 0;
107                                 for (StackTraceElement element : elements) {
108                                         if (i < maxStackTraceEntries) {
109                                                 stackMessage.append(" ClassName- ");
110                                                 stackMessage.append(element.getClassName());
111                                                 stackMessage.append(" :LineNumber- ");
112                                                 stackMessage.append(element.getLineNumber());
113                                                 stackMessage.append(" :MethodName- ");
114                                                 stackMessage.append(element.getMethodName());
115                                         }
116                                         i++;
117                                 }
118                         }
119                 }
120                 return stackMessage.toString();
121         }
122
123 }