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(String serverAddress) throws AuditLoggingException {
25 // Read config from sysargs
26 apiClient = new AuditLoggingAPIClient("testlog", serverAddress);
29 private static AuditLoggingClient fromEnv() throws AuditLoggingException {
30 return fromProps(System.getProperties());
34 public static AuditLoggingClient fromProps(Map<Object, Object> properties) throws AuditLoggingException {
35 if (INSTANCE == null) {
36 synchronized (AuditLoggingClient.class) {
37 if (INSTANCE == null) {
38 String serverAddress = (String) properties.get(AUDIT_SERVER_ADDRESS);
39 if (serverAddress != null && !serverAddress.isEmpty()) {
40 INSTANCE = new AuditLoggingClient(serverAddress);
42 LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS);
50 public static String getUUID() throws AuditLoggingException {
51 return fromEnv().apiClient.getUuid();
54 public static void sendLog(List<Object> keyValues) throws AuditLoggingException {
55 commit(Level.INFO, toMap(keyValues.toArray()));
58 private static Map<String, Object> toMap(Object... keyValues) {
59 if ((keyValues.length % 2) != 0)
60 throw new IllegalArgumentException("Invalid amount of arguments! " + Arrays.toString(keyValues));
61 Map<String, Object> results = new HashMap<>(keyValues.length / 2);
62 for (int i = 0; i < keyValues.length; i += 2) {
63 Object key = keyValues[i];
64 Object value = keyValues[i + 1];
65 if (!(key instanceof String))
66 throw new IllegalArgumentException("Key with index " + i + " is not String");
67 results.put((String) key, value);
72 public static void sendLog(Map<String, Object> event) throws AuditLoggingException {
73 commit(Level.INFO, event);
76 public static void sendError(Map<String, Object> event) throws AuditLoggingException {
77 commit(Level.ERROR, event);
80 public static void sendError(List<Object> keyValues) throws AuditLoggingException {
81 commit(Level.ERROR, toMap(keyValues.toArray()));
84 public static void sendTrace(Map<String, Object> event) throws AuditLoggingException {
85 commit(Level.TRACE, event);
88 public static void sendTrace(List<Object> keyValues) throws AuditLoggingException {
89 commit(Level.TRACE, toMap(keyValues.toArray()));
92 private static void commit(Level level, Map<String, Object> message) throws AuditLoggingException {
94 AuditLoggingClient client = fromEnv();
95 if (client == null || client.apiClient == null) {
96 // No can do - at least log to file
97 LOGGER.warn("Audit logging server not configured - printing event to log");
98 LOGGER.info(message.toString());
100 AuditLoggingAPIClient apiClient = client.apiClient;
103 apiClient.log(message);
106 apiClient.error(message);
109 apiClient.trace(message);
115 } catch (AuditLoggingException e) {
116 // Just for debugging purposes
117 LOGGER.error("Could not send audit event {} with level {}", message, level, e);
118 // log this locally to a file just in case
119 AuditLogging.log("local", message);