Update the license for 2017-2018 license
[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                 StringBuffer stackMessage = new StringBuffer();
58                 int maxStackTraceEntries = 10;
59                 try {
60                         maxStackTraceEntries = Integer.valueOf(AAIConfig.get(AAIConstants.LOGGING_MAX_STACK_TRACE_ENTRIES));
61                 }
62                 catch (AAIException a) {
63                         //ignore, use default
64                 }
65                 catch (NumberFormatException n) {
66                         //ignore, use default
67                 }
68                 if (e != null) {
69                         Throwable rootCause = ExceptionUtils.getRootCause(e);
70                         if (rootCause != null) {
71                                 stackMessage.append("root cause=" + ExceptionUtils.getRootCause(e));
72                                 StackTraceElement[] elements = rootCause.getStackTrace();
73                                 int i = 0;
74                                 for (StackTraceElement element : elements) {
75                                         if (i < maxStackTraceEntries) {
76                                                 stackMessage.append(" ClassName- ");
77                                                 stackMessage.append(element.getClassName());
78                                                 stackMessage.append(" :LineNumber- ");
79                                                 stackMessage.append(element.getLineNumber());
80                                                 stackMessage.append(" :MethodName- ");
81                                                 stackMessage.append(element.getMethodName());
82                                         }
83                                         i++;
84                                 }
85                         } else if (e.getCause() != null) {
86                                 stackMessage.append("cause=" + e.getCause());
87                                 StackTraceElement[] elements = e.getCause().getStackTrace();
88                                 int i = 0;
89                                 for (StackTraceElement element : elements) {
90                                         if (i < maxStackTraceEntries) {
91                                                 stackMessage.append(" ClassName- ");
92                                                 stackMessage.append(element.getClassName());
93                                                 stackMessage.append(" :LineNumber- ");
94                                                 stackMessage.append(element.getLineNumber());
95                                                 stackMessage.append(" :MethodName- ");
96                                                 stackMessage.append(element.getMethodName());
97                                         }
98                                         i++;
99                                 }
100                         } else if (e.getStackTrace() != null) {
101                                 stackMessage.append("ex=" + e.toString());
102                                 StackTraceElement[] elements = e.getStackTrace();
103                                 int i = 0;
104                                 for (StackTraceElement element : elements) {
105                                         if (i < maxStackTraceEntries) {
106                                                 stackMessage.append(" ClassName- ");
107                                                 stackMessage.append(element.getClassName());
108                                                 stackMessage.append(" :LineNumber- ");
109                                                 stackMessage.append(element.getLineNumber());
110                                                 stackMessage.append(" :MethodName- ");
111                                                 stackMessage.append(element.getMethodName());
112                                         }
113                                         i++;
114                                 }
115                         }
116                 }
117                 return stackMessage.toString();
118         }
119
120 }