1 package org.simantics.audit.client;
3 import java.util.Arrays;
4 import java.util.HashMap;
8 import org.simantics.audit.AuditLogging;
9 import org.simantics.audit.AuditLogging.Level;
10 import org.simantics.audit.AuditLoggingException;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 public class AuditLoggingClient {
16 private static final String AUDIT_SERVER_ADDRESS = "org.simantics.audit.serverAddress";
18 private static final Logger LOGGER = LoggerFactory.getLogger(AuditLoggingClient.class);
20 private static AuditLoggingClient INSTANCE;
22 private AuditLoggingAPIClient apiClient;
24 private AuditLoggingClient() throws AuditLoggingException {
25 // Read config from sysargs
26 System.out.println("asd");
27 String serverAddress = System.getProperty(AUDIT_SERVER_ADDRESS);
28 if (serverAddress != null && !serverAddress.isEmpty()) {
29 apiClient = new AuditLoggingAPIClient("testlog", serverAddress);
32 LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS);
36 private static AuditLoggingClient instance() throws AuditLoggingException {
37 if (INSTANCE == null) {
38 synchronized (AuditLoggingClient.class) {
39 if (INSTANCE == null) {
40 INSTANCE = new AuditLoggingClient();
47 public static void sendLog(List<Object> keyValues) throws AuditLoggingException {
48 commit(Level.INFO, toMap(keyValues.toArray()));
51 private static Map<String, Object> toMap(Object... keyValues) {
52 if ((keyValues.length % 2) != 0)
53 throw new IllegalArgumentException("Invalid amount of arguments! " + Arrays.toString(keyValues));
54 Map<String, Object> results = new HashMap<>(keyValues.length / 2);
55 for (int i = 0; i < keyValues.length; i += 2) {
56 Object key = keyValues[i];
57 Object value = keyValues[i + 1];
58 if (!(key instanceof String))
59 throw new IllegalArgumentException("Key with index " + i + " is not String");
60 results.put((String) key, value);
65 public static void sendLog(Map<String, Object> event) throws AuditLoggingException {
66 commit(Level.INFO, event);
69 public static void sendError(Map<String, Object> event) throws AuditLoggingException {
70 commit(Level.ERROR, event);
73 public static void sendError(List<Object> keyValues) throws AuditLoggingException {
74 commit(Level.ERROR, toMap(keyValues.toArray()));
77 public static void sendTrace(Map<String, Object> event) throws AuditLoggingException {
78 commit(Level.TRACE, event);
81 public static void sendTrace(List<Object> keyValues) throws AuditLoggingException {
82 commit(Level.TRACE, toMap(keyValues.toArray()));
85 private static void commit(Level level, Map<String, Object> message) throws AuditLoggingException {
87 AuditLoggingAPIClient client = instance().apiClient;
89 // No can do - at least log to file
90 LOGGER.warn("Audit logging server not configured - printing event to log");
91 LOGGER.info(message.toString());
98 client.error(message);
101 client.trace(message);
107 } catch (AuditLoggingException e) {
108 // Just for debugging purposes
109 LOGGER.error("Could not send audit event {} with level {}", message, level, e);
110 // log this locally to a file just in case
111 AuditLogging.log("local", message);