throw new AuditLoggingException("Could not register service with id " + id, e);
}
}
-
+
+ /**
+ * Gets audit events for the last 5 days
+ *
+ * @param uuid
+ * @param level
+ * @return
+ * @throws AuditLoggingException
+ */
+ public static List<String> getLogEventsDays(String uuid, String level, int days) throws AuditLoggingException {
+ LocalDate endDate = LocalDate.now().plusDays(1);
+ LocalDate startDate = endDate.minusDays(days);
+ return getLogEvents(uuid, level, startDate, endDate);
+ }
+
public static List<String> getLogEvents(String uuid, String level, String startDate, String endDate) throws AuditLoggingException {
- Path entryRoot = getEntryRoot(uuid);
try {
LocalDate localStartDate = LocalDate.parse(startDate);
LocalDate localEndDate = LocalDate.parse(endDate).plusDays(1);
+ return getLogEvents(uuid, level, localStartDate, localEndDate);
+ } catch (Exception e) {
+ throw new AuditLoggingException(e);
+ }
+ }
+
+ private static List<String> getLogEvents(String uuid, String level, LocalDate localStartDate, LocalDate localEndDate) throws AuditLoggingException {
+ Path entryRoot = getEntryRoot(uuid);
+ try {
List<String> allLines = new ArrayList<>();
while (localStartDate.isBefore(localEndDate)) {
String fileName = resolveLogFileName(uuid, Level.valueOf(level.toUpperCase()), localStartDate);
try {
- List<String> lines = Files.readAllLines(entryRoot.resolve(fileName));
- allLines.addAll(lines);
+ Path fileToRead = entryRoot.resolve(fileName);
+ if (Files.exists(fileToRead)) {
+ List<String> lines = Files.readAllLines(fileToRead);
+ allLines.addAll(lines);
+ } else {
+ LOGGER.info("No logging events for " + fileName);
+ }
} catch (FileSystemException e) {
// presumably file not found but lets not throw this cause forward, log here
LOGGER.error("Could not read file {}", fileName, e);
throw new AuditLoggingException(e);
}
}
-
public static Path getEntryRoot(String uuid) {
return Activator.getLogLocation().resolve(uuid);