X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.auditlogging%2Fsrc%2Forg%2Fsimantics%2Faudit%2FAuditLogging.java;h=3cd6ac3921ecfc9107166775ab3ee42ebeaf778b;hb=refs%2Fchanges%2F79%2F2779%2F1;hp=cbbf1420984adbf81a6169c103c94d421501e85e;hpb=da4210cb095e4acd25ddba55a86aa6fe0b18301d;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.auditlogging/src/org/simantics/audit/AuditLogging.java b/bundles/org.simantics.auditlogging/src/org/simantics/audit/AuditLogging.java index cbbf14209..3cd6ac392 100644 --- a/bundles/org.simantics.auditlogging/src/org/simantics/audit/AuditLogging.java +++ b/bundles/org.simantics.auditlogging/src/org/simantics/audit/AuditLogging.java @@ -40,18 +40,63 @@ public class AuditLogging { throw new AuditLoggingException("Could not register service with id " + id, e); } } - + + public static Map> allLogEvents(String level, int days) throws AuditLoggingException { + Map> results = new HashMap<>(); + try { + Files.walk(Activator.getLogLocation()).forEach(uuid -> { + String fileName = uuid.getFileName().toString(); + try { + List events = getLogEventsDays(fileName, level, days); + results.put(fileName, events); + } catch (AuditLoggingException e) { + LOGGER.error("Could not get audit log events for {}", fileName, e); + } + }); + } catch (IOException e) { + throw new AuditLoggingException(e); + } + return results; + } + + /** + * Gets audit events for the last 5 days + * + * @param uuid + * @param level + * @return + * @throws AuditLoggingException + */ + public static List 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 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 getLogEvents(String uuid, String level, LocalDate localStartDate, LocalDate localEndDate) throws AuditLoggingException { + Path entryRoot = getEntryRoot(uuid); + try { List allLines = new ArrayList<>(); while (localStartDate.isBefore(localEndDate)) { String fileName = resolveLogFileName(uuid, Level.valueOf(level.toUpperCase()), localStartDate); try { - List lines = Files.readAllLines(entryRoot.resolve(fileName)); - allLines.addAll(lines); + Path fileToRead = entryRoot.resolve(fileName); + if (Files.exists(fileToRead)) { + List 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); @@ -64,7 +109,6 @@ public class AuditLogging { throw new AuditLoggingException(e); } } - public static Path getEntryRoot(String uuid) { return Activator.getLogLocation().resolve(uuid);