3f0d80ae9a89c802d2c9c4b06c97e514a8b3da44
[aai/aai-common.git] / aai-els-onap-logging / 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
21 package org.onap.aai.logging;
22
23 import org.apache.commons.lang.exception.ExceptionUtils;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.util.AAIConfig;
26 import org.onap.aai.util.AAIConstants;
27
28 import java.time.Instant;
29 import java.time.ZoneOffset;
30 import java.time.ZonedDateTime;
31 import java.time.format.DateTimeFormatter;
32
33 public class LogFormatTools {
34
35     private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
36     private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern(DATE_FORMAT).withZone(ZoneOffset.UTC);
37
38     public static String getCurrentDateTime() {
39         return DTF.format(ZonedDateTime.now());
40     }
41
42     public static String toDate(long timestamp) {
43         return DTF.format(Instant.ofEpochMilli(timestamp));
44     }
45
46     public static long toTimestamp(String date) {
47         return ZonedDateTime.parse(date, DTF).toInstant().toEpochMilli();
48     }
49
50     /**
51      * Gets the stack top.
52      *
53      * @param e the e
54      * @return the stack top
55      * @throws NumberFormatException the number format exception
56      * @throws AAIException the AAI exception
57      */
58     public static String getStackTop(Throwable e) {
59         // StringBuilder is more efficient than StringBuffer and should only
60         // StringBuffer is only supposed to be used if multiple threads are modifying
61         // the same object and since this object is created locally not necessary
62         StringBuilder stackMessage = new StringBuilder();
63         int maxStackTraceEntries = 10;
64         try {
65             maxStackTraceEntries = Integer.valueOf(AAIConfig.get(AAIConstants.LOGGING_MAX_STACK_TRACE_ENTRIES));
66         } catch (AAIException a) {
67             // ignore, use default
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         String stackMessageStr = stackMessage.toString();
121         stackMessageStr = stackMessageStr.replaceAll("\\n", "^");
122         return stackMessageStr;
123     }
124
125 }