[New] Implemented simple OperationsLog to storing messages
This commit is contained in:
parent
bab203e418
commit
6171e53ff4
|
@ -63,6 +63,11 @@ public class Tags {
|
||||||
public static final String RESOURCE_TYPE = "ResourceType";
|
public static final String RESOURCE_TYPE = "ResourceType";
|
||||||
public static final String STATE_ID = "StateId";
|
public static final String STATE_ID = "StateId";
|
||||||
|
|
||||||
|
public static final String LOCATOR = "Locator";
|
||||||
|
public static final String SEVERITY = "Severity";
|
||||||
|
public static final String KEY = "Key";
|
||||||
|
public static final String MESSAGE = "Message";
|
||||||
|
|
||||||
public static class Json {
|
public static class Json {
|
||||||
|
|
||||||
// elements
|
// elements
|
||||||
|
@ -107,6 +112,11 @@ public class Tags {
|
||||||
public static final String CREATED_BY = "createdBy";
|
public static final String CREATED_BY = "createdBy";
|
||||||
public static final String DELETED = "deleted";
|
public static final String DELETED = "deleted";
|
||||||
|
|
||||||
|
public static final String LOCATOR = "locator";
|
||||||
|
public static final String SEVERITY = "severity";
|
||||||
|
public static final String KEY = "key";
|
||||||
|
public static final String MESSAGE = "message";
|
||||||
|
|
||||||
// miscellaneous
|
// miscellaneous
|
||||||
|
|
||||||
public static final String ELEMENTS = "elements";
|
public static final String ELEMENTS = "elements";
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package li.strolch.handler.operationslog;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import li.strolch.model.Locator;
|
||||||
|
import li.strolch.model.Tags.Json;
|
||||||
|
import li.strolch.utils.I18nMessage;
|
||||||
|
|
||||||
|
public class LogMessage extends I18nMessage {
|
||||||
|
|
||||||
|
private final Locator locator;
|
||||||
|
private final LogSeverity severity;
|
||||||
|
|
||||||
|
public LogMessage(Locator locator, LogSeverity logSeverity, String bundleId, String key) {
|
||||||
|
super(bundleId, key);
|
||||||
|
this.locator = locator;
|
||||||
|
this.severity = logSeverity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LogSeverity getSeverity() {
|
||||||
|
return this.severity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Locator getLocator() {
|
||||||
|
return this.locator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LogMessage value(String key, String value) {
|
||||||
|
super.value(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject toJson() {
|
||||||
|
|
||||||
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
|
||||||
|
jsonObject.addProperty(Json.KEY, getKey());
|
||||||
|
jsonObject.addProperty(Json.MESSAGE, formatMessage());
|
||||||
|
jsonObject.addProperty(Json.SEVERITY, this.severity.getSeverity());
|
||||||
|
jsonObject.addProperty(Json.LOCATOR, this.locator.toString());
|
||||||
|
JsonObject values = new JsonObject();
|
||||||
|
for (String key : getValues().stringPropertyNames()) {
|
||||||
|
values.addProperty(key, getValues().getProperty(key));
|
||||||
|
}
|
||||||
|
jsonObject.add(Json.VALUES, values);
|
||||||
|
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package li.strolch.handler.operationslog;
|
||||||
|
|
||||||
|
public enum LogSeverity {
|
||||||
|
INFO("Info"), //
|
||||||
|
WARN("Warn"), //
|
||||||
|
ERROR("Error"), //
|
||||||
|
EXCEPTION("Exception");
|
||||||
|
|
||||||
|
private String severity;
|
||||||
|
|
||||||
|
private LogSeverity(String severity) {
|
||||||
|
this.severity = severity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSeverity() {
|
||||||
|
return this.severity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LogSeverity from(String value) {
|
||||||
|
for (LogSeverity type : values()) {
|
||||||
|
if (type.severity.equals(value))
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("No severity exists for " + value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package li.strolch.handler.operationslog;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
|
import li.strolch.agent.api.StrolchComponent;
|
||||||
|
import li.strolch.model.Locator;
|
||||||
|
import li.strolch.runtime.configuration.ComponentConfiguration;
|
||||||
|
|
||||||
|
public class OperationsLog extends StrolchComponent {
|
||||||
|
|
||||||
|
private LinkedHashMap<Locator, LogMessage> logMessages;
|
||||||
|
|
||||||
|
public OperationsLog(ComponentContainer container, String componentName) {
|
||||||
|
super(container, componentName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(ComponentConfiguration configuration) throws Exception {
|
||||||
|
|
||||||
|
int maxMessages = configuration.getInt("maxMessages", 10000);
|
||||||
|
|
||||||
|
this.logMessages = new LinkedHashMap<Locator, LogMessage>() {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean removeEldestEntry(java.util.Map.Entry<Locator, LogMessage> eldest) {
|
||||||
|
return size() > maxMessages;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
super.initialize(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMessage(LogMessage logMessage) {
|
||||||
|
this.logMessages.put(logMessage.getLocator(), logMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LogMessage getMessage(Locator locator) {
|
||||||
|
return this.logMessages.get(locator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<LogMessage> getMessages() {
|
||||||
|
return this.logMessages.entrySet().stream().map(e -> e.getValue()).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package li.strolch.utils;
|
||||||
|
|
||||||
|
import static li.strolch.utils.helper.StringHelper.EMPTY;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
import li.strolch.utils.dbc.DBC;
|
||||||
|
import li.strolch.utils.helper.StringHelper;
|
||||||
|
|
||||||
|
public class I18nMessage {
|
||||||
|
|
||||||
|
private String bundleId;
|
||||||
|
private String key;
|
||||||
|
private Properties values;
|
||||||
|
|
||||||
|
public I18nMessage(String bundleId, String key) {
|
||||||
|
DBC.INTERIM.assertNotEmpty("bundleId must be set!", bundleId);
|
||||||
|
DBC.INTERIM.assertNotEmpty("key must be set!", key);
|
||||||
|
this.bundleId = bundleId;
|
||||||
|
this.key = key;
|
||||||
|
this.values = new Properties();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBundleId() {
|
||||||
|
return this.bundleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return this.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Properties getValues() {
|
||||||
|
return this.values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public I18nMessage value(String key, String value) {
|
||||||
|
DBC.INTERIM.assertNotEmpty("key must be set!", key);
|
||||||
|
DBC.INTERIM.assertNotEmpty("value must be set!", value);
|
||||||
|
this.values.setProperty(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String formatMessage() {
|
||||||
|
ResourceBundle bundle = ResourceBundle.getBundle(this.bundleId);
|
||||||
|
String string = bundle.getString(this.key);
|
||||||
|
return StringHelper.replacePropertiesIn(this.values, EMPTY, string);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue