From: jsimomaa Date: Mon, 8 Jan 2018 05:54:46 +0000 (+0200) Subject: AuditLogging improvements for easier unit testing X-Git-Tag: v1.43.0~136^2~633 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=87590104a1ecf5e0b2edb82c0b49601cc8f97488;p=simantics%2Fplatform.git AuditLogging improvements for easier unit testing refs #7684 Change-Id: I6f4b10d2344b4588582f2558b9fda75f6d20145a --- diff --git a/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Client.scl b/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Client.scl index f60474aa4..4c8c9be68 100644 --- a/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Client.scl +++ b/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Client.scl @@ -1,6 +1,13 @@ import "Map" as Map importJava "org.simantics.audit.client.AuditLoggingClient" where + + @JavaName fromProps + auditLoggingClient :: Map.T a a -> b + + @JavaName getUUID + getAuditLoggingUUID :: String + @JavaName sendLog sendLogM :: Map.T String a -> () sendLog :: [a] -> () diff --git a/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Server.scl b/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Server.scl index 396f5f561..9304294c0 100644 --- a/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Server.scl +++ b/bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Server.scl @@ -5,6 +5,7 @@ importJava "org.simantics.audit.AuditLogging" where log :: String -> Map.T String a -> () error :: String -> Map.T String a -> () trace :: String -> Map.T String a -> () + getLogEventsDays :: String -> String -> Integer -> [String] getLogEvents :: String -> String -> String -> String -> [String] importJava "org.simantics.audit.server.AuditLoggingServer" where 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..334e19c47 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,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 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 +91,6 @@ public class AuditLogging { throw new AuditLoggingException(e); } } - public static Path getEntryRoot(String uuid) { return Activator.getLogLocation().resolve(uuid); diff --git a/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingAPIClient.java b/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingAPIClient.java index a7fc6022a..645a60657 100644 --- a/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingAPIClient.java +++ b/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingAPIClient.java @@ -47,6 +47,10 @@ public class AuditLoggingAPIClient { } } + public String getUuid() { + return uuid; + } + private void register(String id) throws AuditLoggingException { try { Response response = base.path("register").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(Collections.singletonMap("id", id))); diff --git a/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java b/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java index e31a6ab21..63045fdcf 100644 --- a/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java +++ b/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java @@ -21,29 +21,36 @@ public class AuditLoggingClient { private AuditLoggingAPIClient apiClient; - private AuditLoggingClient() throws AuditLoggingException { + private AuditLoggingClient(String serverAddress) throws AuditLoggingException { // Read config from sysargs - System.out.println("asd"); - String serverAddress = System.getProperty(AUDIT_SERVER_ADDRESS); - if (serverAddress != null && !serverAddress.isEmpty()) { - apiClient = new AuditLoggingAPIClient("testlog", serverAddress); - - } else { - LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS); - } + apiClient = new AuditLoggingAPIClient("testlog", serverAddress); + } + + private static AuditLoggingClient fromEnv() throws AuditLoggingException { + return fromProps(System.getProperties()); } - private static AuditLoggingClient instance() throws AuditLoggingException { + + public static AuditLoggingClient fromProps(Map properties) throws AuditLoggingException { if (INSTANCE == null) { synchronized (AuditLoggingClient.class) { if (INSTANCE == null) { - INSTANCE = new AuditLoggingClient(); + String serverAddress = (String) properties.get(AUDIT_SERVER_ADDRESS); + if (serverAddress != null && !serverAddress.isEmpty()) { + INSTANCE = new AuditLoggingClient(serverAddress); + } else { + LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS); + } } } } return INSTANCE; } + public static String getUUID() throws AuditLoggingException { + return fromEnv().apiClient.getUuid(); + } + public static void sendLog(List keyValues) throws AuditLoggingException { commit(Level.INFO, toMap(keyValues.toArray())); } @@ -84,7 +91,7 @@ public class AuditLoggingClient { private static void commit(Level level, Map message) throws AuditLoggingException { try { - AuditLoggingAPIClient client = instance().apiClient; + AuditLoggingAPIClient client = fromEnv().apiClient; if (client == null) { // No can do - at least log to file LOGGER.warn("Audit logging server not configured - printing event to log"); diff --git a/bundles/org.simantics.auditlogging/src/org/simantics/audit/server/AuditLoggingAPI.java b/bundles/org.simantics.auditlogging/src/org/simantics/audit/server/AuditLoggingAPI.java index 980ea4c98..c5eccfad9 100644 --- a/bundles/org.simantics.auditlogging/src/org/simantics/audit/server/AuditLoggingAPI.java +++ b/bundles/org.simantics.auditlogging/src/org/simantics/audit/server/AuditLoggingAPI.java @@ -59,4 +59,17 @@ public class AuditLoggingAPI { return Response.serverError().entity(buildJSONResponse("message", e.getMessage())).build(); } } + + @Path("{uuid}/error") + @POST + public Response error(@PathParam("uuid") String uuid, Map payload) { + + try { + AuditLogging.error(uuid, payload); + return Response.ok().build(); + } catch (AuditLoggingException e) { + LOGGER.error("Could not log error audit with id {}", uuid, e); + return Response.serverError().entity(buildJSONResponse("message", e.getMessage())).build(); + } + } }