56 lines
2.0 KiB
Java
56 lines
2.0 KiB
Java
|
package eu.eudat.core.logger.common;
|
||
|
|
||
|
import org.json.JSONObject;
|
||
|
import org.springframework.core.env.Environment;
|
||
|
import types.LoggingOutputType;
|
||
|
|
||
|
import java.util.Collection;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
import java.util.concurrent.ConcurrentHashMap;
|
||
|
import java.util.concurrent.Executors;
|
||
|
import java.util.concurrent.ScheduledExecutorService;
|
||
|
import java.util.concurrent.TimeUnit;
|
||
|
|
||
|
/**
|
||
|
* Created by ikalyvas on 5/30/2018.
|
||
|
*/
|
||
|
public abstract class AbstractBatchLogger {
|
||
|
private Map<String, Object> concurrentHashMap = new ConcurrentHashMap<String, Object>();
|
||
|
|
||
|
public AbstractBatchLogger(Environment environment) {
|
||
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
||
|
|
||
|
executor.scheduleAtFixedRate(() -> this.outputData(), Long.parseLong(environment.getProperty("http-logger.initial-delay")), Long.parseLong(environment.getProperty("http-logger.delay")), TimeUnit.SECONDS);
|
||
|
}
|
||
|
|
||
|
public abstract LoggingOutputType logOutputType();
|
||
|
|
||
|
public <T> void put(String key, T data) {
|
||
|
this.concurrentHashMap.put(key, data);
|
||
|
}
|
||
|
|
||
|
public void clear() {
|
||
|
this.concurrentHashMap.clear();
|
||
|
}
|
||
|
|
||
|
private Map<String, Object> getAndClear() {
|
||
|
Map<String, Object> map = new HashMap<>();
|
||
|
map.putAll(this.concurrentHashMap);
|
||
|
this.concurrentHashMap.keySet().removeAll(map.keySet());
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
public String tranformLog() {
|
||
|
Map<String ,Collection<Object>> transformedMap = new HashMap<>();
|
||
|
transformedMap.put("data",this.getAndClear().values());
|
||
|
if (this.logOutputType().equals(LoggingOutputType.JSON))
|
||
|
return this.concurrentHashMap.values().size() > 0 ? new JSONObject(transformedMap).toString() : null;
|
||
|
if (this.logOutputType().equals(LoggingOutputType.FILE))
|
||
|
return this.concurrentHashMap.toString(); //TODO actual implementation of file Logger
|
||
|
else throw new RuntimeException("Unsupported LoggingOutputType type");
|
||
|
}
|
||
|
|
||
|
public abstract void outputData();
|
||
|
}
|