import "Map" as Map
importJava "org.simantics.audit.client.AuditLoggingClient" where
+
+ @JavaName fromProps
+ auditLoggingClient :: Map.T a a -> <Proc> b
+
+ @JavaName getUUID
+ getAuditLoggingUUID :: <Proc> String
+
@JavaName sendLog
sendLogM :: Map.T String a -> <Proc> ()
sendLog :: [a] -> <Proc> ()
log :: String -> Map.T String a -> <Proc, Exception> ()
error :: String -> Map.T String a -> <Proc, Exception> ()
trace :: String -> Map.T String a -> <Proc, Exception> ()
+ getLogEventsDays :: String -> String -> Integer -> <Proc, Exception> [String]
getLogEvents :: String -> String -> String -> String -> <Proc, Exception> [String]
importJava "org.simantics.audit.server.AuditLoggingServer" where
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);
}
}
+ 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)));
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<Object, Object> 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<Object> keyValues) throws AuditLoggingException {
commit(Level.INFO, toMap(keyValues.toArray()));
}
private static void commit(Level level, Map<String, Object> 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");
return Response.serverError().entity(buildJSONResponse("message", e.getMessage())).build();
}
}
+
+ @Path("{uuid}/error")
+ @POST
+ public Response error(@PathParam("uuid") String uuid, Map<String, Object> 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();
+ }
+ }
}