X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.auditlogging%2Fsrc%2Forg%2Fsimantics%2Faudit%2Fclient%2FAuditLoggingClient.java;fp=bundles%2Forg.simantics.auditlogging%2Fsrc%2Forg%2Fsimantics%2Faudit%2Fclient%2FAuditLoggingClient.java;h=e31a6ab214531a967fee0a06d3d41cd12fb7e573;hb=da4210cb095e4acd25ddba55a86aa6fe0b18301d;hp=0000000000000000000000000000000000000000;hpb=ad864a5ed509612375e2a904422e18287dfd5cc5;p=simantics%2Fplatform.git 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 new file mode 100644 index 000000000..e31a6ab21 --- /dev/null +++ b/bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java @@ -0,0 +1,115 @@ +package org.simantics.audit.client; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.simantics.audit.AuditLogging; +import org.simantics.audit.AuditLogging.Level; +import org.simantics.audit.AuditLoggingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AuditLoggingClient { + + private static final String AUDIT_SERVER_ADDRESS = "org.simantics.audit.serverAddress"; + + private static final Logger LOGGER = LoggerFactory.getLogger(AuditLoggingClient.class); + + private static AuditLoggingClient INSTANCE; + + private AuditLoggingAPIClient apiClient; + + private AuditLoggingClient() 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); + } + } + + private static AuditLoggingClient instance() throws AuditLoggingException { + if (INSTANCE == null) { + synchronized (AuditLoggingClient.class) { + if (INSTANCE == null) { + INSTANCE = new AuditLoggingClient(); + } + } + } + return INSTANCE; + } + + public static void sendLog(List keyValues) throws AuditLoggingException { + commit(Level.INFO, toMap(keyValues.toArray())); + } + + private static Map toMap(Object... keyValues) { + if ((keyValues.length % 2) != 0) + throw new IllegalArgumentException("Invalid amount of arguments! " + Arrays.toString(keyValues)); + Map results = new HashMap<>(keyValues.length / 2); + for (int i = 0; i < keyValues.length; i += 2) { + Object key = keyValues[i]; + Object value = keyValues[i + 1]; + if (!(key instanceof String)) + throw new IllegalArgumentException("Key with index " + i + " is not String"); + results.put((String) key, value); + } + return results; + } + + public static void sendLog(Map event) throws AuditLoggingException { + commit(Level.INFO, event); + } + + public static void sendError(Map event) throws AuditLoggingException { + commit(Level.ERROR, event); + } + + public static void sendError(List keyValues) throws AuditLoggingException { + commit(Level.ERROR, toMap(keyValues.toArray())); + } + + public static void sendTrace(Map event) throws AuditLoggingException { + commit(Level.TRACE, event); + } + + public static void sendTrace(List keyValues) throws AuditLoggingException { + commit(Level.TRACE, toMap(keyValues.toArray())); + } + + private static void commit(Level level, Map message) throws AuditLoggingException { + try { + AuditLoggingAPIClient client = instance().apiClient; + if (client == null) { + // No can do - at least log to file + LOGGER.warn("Audit logging server not configured - printing event to log"); + LOGGER.info(message.toString()); + } else { + switch (level) { + case INFO: + client.log(message); + break; + case ERROR: + client.error(message); + break; + case TRACE: + client.trace(message); + break; + default: + break; + } + } + } catch (AuditLoggingException e) { + // Just for debugging purposes + LOGGER.error("Could not send audit event {} with level {}", message, level, e); + // log this locally to a file just in case + AuditLogging.log("local", message); + throw e; + } + } +}