2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.ccadaptor;
26 import java.io.BufferedWriter;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31 import java.io.StringWriter;
32 import java.nio.file.Path;
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
38 public final class DebugLog {
40 public static final String LOG_FILE = "/tmp/rt.log";
41 private final Path path;
43 public DebugLog(Path path) {
47 public void printRTAriDebug(String methodName, String message) {
48 writeToLogFile(methodName, message);
51 private String currentDateTime() {
52 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
53 return dateFormat.format(new Date());
56 private void appendToFile(File logPath, String dataToWrite) {
57 try (BufferedWriter out = new BufferedWriter(new FileWriter(logPath, true))) {
58 out.write(dataToWrite);
59 } catch (IOException e) {
64 public void outputStackTrace(Exception e) {
65 String fn = "DebugLog.outputStackTrace";
66 StringWriter sw = new StringWriter();
67 e.printStackTrace(new PrintWriter(sw));
68 String stackTrace = sw.toString();
69 writeToLogFile(fn, "Stack trace::: " + stackTrace);
72 private void writeToLogFile(String methodName, String message) {
73 File logPath = path.toFile();
74 if (logPath.exists()) {
75 StringBuilder logMessageBuilder = createLogMessage(methodName, message);
76 appendToFile(logPath, logMessageBuilder.toString());
80 private StringBuilder createLogMessage(String methodName, String message) {
81 StringBuilder logMessageBuilder = new StringBuilder();
83 .append(currentDateTime())
89 return logMessageBuilder;