1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.node;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
40 * Logging for data router delivery events (PUB/DEL/EXP)
42 public class StatusLog {
44 private static final String EXCEPTION = "Exception";
45 private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(StatusLog.class);
46 private static StatusLog instance = new StatusLog();
47 private SimpleDateFormat filedate = new SimpleDateFormat("-yyyyMMddHHmm");
49 private String prefix = "logs/events";
50 private String suffix = ".log";
51 private String plainfile;
52 private String curfile;
53 private long nexttime;
54 private OutputStream os;
56 private static NodeConfigManager config = NodeConfigManager.getInstance();
62 * Parse an interval of the form xxhyymzzs and round it to the nearest whole fraction of 24 hours.If no units are
63 * specified, assume seconds.
65 public static long parseInterval(String interval, int def) {
67 Matcher m = Pattern.compile("(?:(\\d+)[Hh])?(?:(\\d+)[Mm])?(?:(\\d+)[Ss]?)?").matcher(interval);
71 int dist = best - dur;
75 best = getBest(dur, best, dist);
78 } catch (Exception e) {
79 eelfLogger.error(EXCEPTION, e);
84 private static int getBest(int dur, int best, int dist) {
86 for (int i = 0; i < 8; i++) {
89 for (int j = 0; j < 4; j++) {
92 for (int k = 0; k < 3; k++) {
95 int ndist = cur - dur;
109 private static int getDur(Matcher m) {
111 String x = m.group(1);
113 dur += 3600 * Integer.parseInt(x);
117 dur += 60 * Integer.parseInt(x);
121 dur += Integer.parseInt(x);
130 * Get the name of the current log file
132 * @return The full path name of the current event log file
134 public static synchronized String getCurLogFile() {
136 instance.checkRoll(System.currentTimeMillis());
137 } catch (Exception e) {
138 eelfLogger.error(EXCEPTION, e);
140 return (instance.curfile);
144 * Log a received publication attempt.
146 * @param pubid The publish ID assigned by the node
147 * @param feedid The feed id given by the publisher
148 * @param requrl The URL of the received request
149 * @param method The method (DELETE or PUT) in the received request
150 * @param ctype The content type (if method is PUT and clen > 0)
151 * @param clen The content length (if method is PUT)
152 * @param srcip The IP address of the publisher
153 * @param user The identity of the publisher
154 * @param status The status returned to the publisher
156 public static void logPub(String pubid, String feedid, String requrl, String method, String ctype, long clen,
157 String srcip, String user, int status) {
159 "PUB|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + srcip
160 + "|" + user + "|" + status);
164 * Log a data transfer error receiving a publication attempt
166 * @param pubid The publish ID assigned by the node
167 * @param feedid The feed id given by the publisher
168 * @param requrl The URL of the received request
169 * @param method The method (DELETE or PUT) in the received request
170 * @param ctype The content type (if method is PUT and clen > 0)
171 * @param clen The expected content length (if method is PUT)
172 * @param rcvd The content length received
173 * @param srcip The IP address of the publisher
174 * @param user The identity of the publisher
175 * @param error The error message from the IO exception
177 public static void logPubFail(String pubid, String feedid, String requrl, String method, String ctype, long clen,
178 long rcvd, String srcip, String user, String error) {
179 instance.log("PBF|" + pubid + "|" + feedid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen + "|" + rcvd
180 + "|" + srcip + "|" + user + "|" + error);
184 * Log a delivery attempt.
186 * @param pubid The publish ID assigned by the node
187 * @param feedid The feed ID
188 * @param subid The (space delimited list of) subscription ID
189 * @param requrl The URL used in the attempt
190 * @param method The method (DELETE or PUT) in the attempt
191 * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
192 * @param clen The content length (if PUT and not metaonly)
193 * @param user The identity given to the subscriber
194 * @param status The status returned by the subscriber or -1 if an exeception occured trying to connect
195 * @param xpubid The publish ID returned by the subscriber
197 public static void logDel(String pubid, String feedid, String subid, String requrl, String method, String ctype,
198 long clen, String user, int status, String xpubid) {
199 if (feedid == null) {
203 "DEL|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
204 + "|" + user + "|" + status + "|" + xpubid);
208 * Log delivery attempts expired
210 * @param pubid The publish ID assigned by the node
211 * @param feedid The feed ID
212 * @param subid The (space delimited list of) subscription ID
213 * @param requrl The URL that would be delivered to
214 * @param method The method (DELETE or PUT) in the request
215 * @param ctype The content type (if method is PUT, not metaonly, and clen > 0)
216 * @param clen The content length (if PUT and not metaonly)
217 * @param reason The reason the attempts were discontinued
218 * @param attempts The number of attempts made
220 public static void logExp(String pubid, String feedid, String subid, String requrl, String method, String ctype,
221 long clen, String reason, int attempts) {
222 if (feedid == null) {
226 "EXP|" + pubid + "|" + feedid + "|" + subid + "|" + requrl + "|" + method + "|" + ctype + "|" + clen
227 + "|" + reason + "|" + attempts);
231 * Log extra statistics about unsuccessful delivery attempts.
233 * @param pubid The publish ID assigned by the node
234 * @param feedid The feed ID
235 * @param subid The (space delimited list of) subscription ID
236 * @param clen The content length
237 * @param sent The # of bytes sent or -1 if subscriber returned an error instead of 100 Continue, otherwise, the
238 * number of bytes sent before an error occurred.
240 public static void logDelExtra(String pubid, String feedid, String subid, long clen, long sent) {
241 if (feedid == null) {
244 instance.log("DLX|" + pubid + "|" + feedid + "|" + subid + "|" + clen + "|" + sent);
247 private synchronized void checkRoll(long now) throws IOException {
248 if (now >= nexttime) {
253 intvl = parseInterval(config.getEventLogInterval(), 300000);
254 prefix = config.getEventLogPrefix();
255 suffix = config.getEventLogSuffix();
256 nexttime = now - now % intvl + intvl;
257 curfile = prefix + filedate.format(new Date(nexttime - intvl)) + suffix;
258 plainfile = prefix + suffix;
263 private synchronized void log(String s) {
265 long now = System.currentTimeMillis();
268 os = new FileOutputStream(curfile, true);
269 (new File(plainfile)).delete();
270 Files.createLink(Paths.get(plainfile), Paths.get(curfile));
272 os.write((NodeUtils.logts(new Date(now)) + '|' + s + '\n').getBytes());
274 } catch (IOException ioe) {
275 eelfLogger.error("IOException", ioe);