2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.logging;
23 import java.time.Instant;
24 import java.time.ZoneOffset;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
28 import org.apache.commons.lang.exception.ExceptionUtils;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.util.AAIConfig;
31 import org.onap.aai.util.AAIConstants;
33 public class LogFormatTools {
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);
38 public static String getCurrentDateTime() {
39 return DTF.format(ZonedDateTime.now());
42 public static String toDate(long timestamp) {
43 return DTF.format(Instant.ofEpochMilli(timestamp));
46 public static long toTimestamp(String date) {
47 return ZonedDateTime.parse(date, DTF).toInstant().toEpochMilli();
54 * @return the stack top
55 * @throws NumberFormatException the number format exception
56 * @throws AAIException the AAI exception
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;
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
72 Throwable rootCause = ExceptionUtils.getRootCause(e);
73 if (rootCause != null) {
74 stackMessage.append("root cause=" + ExceptionUtils.getRootCause(e));
75 StackTraceElement[] elements = rootCause.getStackTrace();
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());
88 } else if (e.getCause() != null) {
89 stackMessage.append("cause=" + e.getCause());
90 StackTraceElement[] elements = e.getCause().getStackTrace();
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());
103 } else if (e.getStackTrace() != null) {
104 stackMessage.append("ex=" + e.toString());
105 StackTraceElement[] elements = e.getStackTrace();
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());
120 return stackMessage.toString();