]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java
e31a6ab214531a967fee0a06d3d41cd12fb7e573
[simantics/platform.git] / bundles / org.simantics.auditlogging / src / org / simantics / audit / client / AuditLoggingClient.java
1 package org.simantics.audit.client;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
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;
13
14 public class AuditLoggingClient {
15
16     private static final String AUDIT_SERVER_ADDRESS = "org.simantics.audit.serverAddress";
17     
18     private static final Logger LOGGER = LoggerFactory.getLogger(AuditLoggingClient.class);
19     
20     private static AuditLoggingClient INSTANCE;
21
22     private AuditLoggingAPIClient apiClient;
23
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);
30              
31         } else {
32             LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS);
33         }
34     }
35
36     private static AuditLoggingClient instance() throws AuditLoggingException {
37         if (INSTANCE == null) {
38             synchronized (AuditLoggingClient.class) {
39                 if (INSTANCE == null) {
40                     INSTANCE = new AuditLoggingClient();
41                 }
42             }
43         }
44         return INSTANCE;
45     }
46
47     public static void sendLog(List<Object> keyValues) throws AuditLoggingException {
48         commit(Level.INFO, toMap(keyValues.toArray()));
49     }
50
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);
61         }
62         return results;
63     }
64     
65     public static void sendLog(Map<String, Object> event) throws AuditLoggingException {
66         commit(Level.INFO, event);
67     }
68
69     public static void sendError(Map<String, Object> event) throws AuditLoggingException {
70         commit(Level.ERROR, event);
71     }
72
73     public static void sendError(List<Object> keyValues) throws AuditLoggingException {
74         commit(Level.ERROR, toMap(keyValues.toArray()));
75     }
76
77     public static void sendTrace(Map<String, Object> event) throws AuditLoggingException {
78         commit(Level.TRACE, event);
79     }
80
81     public static void sendTrace(List<Object> keyValues) throws AuditLoggingException {
82         commit(Level.TRACE, toMap(keyValues.toArray()));
83     }
84
85     private static void commit(Level level, Map<String, Object> message) throws AuditLoggingException {
86         try {
87             AuditLoggingAPIClient client = instance().apiClient;
88             if (client == null) {
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());
92             } else {
93                 switch (level) {
94                 case INFO:
95                     client.log(message);
96                     break;
97                 case ERROR:
98                     client.error(message);
99                     break;
100                 case TRACE:
101                     client.trace(message);
102                     break;
103                 default:
104                     break;
105                 }
106             }
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);
112             throw e;
113         }
114     }
115 }