]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.auditlogging/src/org/simantics/audit/AuditLogging.java
AuditLogging improvements for easier unit testing
[simantics/platform.git] / bundles / org.simantics.auditlogging / src / org / simantics / audit / AuditLogging.java
index cbbf1420984adbf81a6169c103c94d421501e85e..334e19c4762787ca579db78da236687ae6b53f79 100644 (file)
@@ -40,18 +40,45 @@ public class AuditLogging {
             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);
@@ -64,7 +91,6 @@ public class AuditLogging {
             throw new AuditLoggingException(e);
         }
     }
-    
 
     public static Path getEntryRoot(String uuid) {
         return Activator.getLogLocation().resolve(uuid);