package io.ebeaninternal.server.changelog;
import io.ebean.event.changelog.BeanChange;
import io.ebean.event.changelog.ChangeLogListener;
import io.ebean.event.changelog.ChangeSet;
import io.ebean.event.changelog.ChangeType;
import io.ebean.plugin.Plugin;
import io.ebean.plugin.SpiServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.StringWriter;
import java.util.Properties;
public class DefaultChangeLogListener implements ChangeLogListener, Plugin {
protected static final Logger logger = LoggerFactory.getLogger(DefaultChangeLogListener.class);
private static final Logger changeLog = LoggerFactory.getLogger("io.ebean.ChangeLog");
private ChangeJsonBuilder jsonBuilder;
private int defaultBufferSize = 400;
public DefaultChangeLogListener() {
}
@Override
public void configure(SpiServer server) {
jsonBuilder = new ChangeJsonBuilder(server.json());
Properties properties = server.getServerConfig().getProperties();
if (properties != null) {
String bufferSize = properties.getProperty("ebean.changeLog.bufferSize");
if (bufferSize != null) {
defaultBufferSize = Integer.parseInt(bufferSize);
}
}
}
@Override
public void online(boolean online) {
}
@Override
public void shutdown() {
}
@Override
public void log(ChangeSet changeSet) {
for (BeanChange beanChange : changeSet.getChanges()) {
try {
StringWriter writer = new StringWriter(getBufferSize(beanChange));
jsonBuilder.writeBeanJson(writer, beanChange, changeSet);
changeLog.info(writer.toString());
} catch (Exception e) {
logger.error("Exception logging beanChange " + beanChange.toString(), e);
}
}
}
private int getBufferSize(BeanChange beanChange) {
return ChangeType.DELETE == beanChange.getEvent() ? 250 : defaultBufferSize;
}
}