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=========================================================
20 package org.onap.aai.logging;
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;
31 public class LogFormatTools {
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);
37 public static String getCurrentDateTime() {
38 return DTF.format(ZonedDateTime.now());
41 public static String toDate(long timestamp) {
42 return DTF.format(Instant.ofEpochMilli(timestamp));
45 public static long toTimestamp(String date) {
46 return ZonedDateTime.parse(date, DTF).toInstant().toEpochMilli();
52 * @return the stack top
53 * @throws NumberFormatException the number format exception
54 * @throws AAIException the AAI exception
56 public static String getStackTop(Throwable e) {
57 StringBuffer stackMessage = new StringBuffer();
58 int maxStackTraceEntries = 10;
60 maxStackTraceEntries = Integer.valueOf(AAIConfig.get(AAIConstants.LOGGING_MAX_STACK_TRACE_ENTRIES));
62 catch (AAIException a) {
65 catch (NumberFormatException n) {
69 Throwable rootCause = ExceptionUtils.getRootCause(e);
70 if (rootCause != null) {
71 stackMessage.append("root cause=" + ExceptionUtils.getRootCause(e));
72 StackTraceElement[] elements = rootCause.getStackTrace();
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());
85 } else if (e.getCause() != null) {
86 stackMessage.append("cause=" + e.getCause());
87 StackTraceElement[] elements = e.getCause().getStackTrace();
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());
100 } else if (e.getStackTrace() != null) {
101 stackMessage.append("ex=" + e.toString());
102 StackTraceElement[] elements = e.getStackTrace();
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());
117 return stackMessage.toString();