diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultDbToLogConverter.java b/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultDbToLogConverter.java index 1be7bc6e7..07e47a988 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultDbToLogConverter.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultDbToLogConverter.java @@ -1,20 +1,12 @@ package org.motechproject.eventlogging.converter.impl; import org.joda.time.DateTime; -import org.motechproject.commons.api.MotechException; import org.motechproject.event.MotechEvent; import org.motechproject.eventlogging.converter.EventToLogConverter; -import org.motechproject.eventlogging.matchers.LogMappings; -import org.motechproject.eventlogging.matchers.DbLoggableEvent; import org.motechproject.eventlogging.domain.EventLog; -import org.motechproject.eventlogging.matchers.KeyValue; -import org.motechproject.eventlogging.matchers.LoggableEvent; +import org.motechproject.eventlogging.matchers.MappedLoggableEvent; import org.springframework.stereotype.Component; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - /** * Implementaton of the {@link EventToLogConverter} interface. * This class is responsible for converting {@link MotechEvent}s to the @@ -34,54 +26,16 @@ public EventLog convertToLog(MotechEvent eventToLog) { /** * Converts an event to a {@link EventLog} object, which represents a single - * logged event in database. Uses {@link DbLoggableEvent} implementation of LoggableEvent + * logged event in database. Uses {@link MappedLoggableEvent} implementation of LoggableEvent * to configure the returned object. * * @param eventToLog the incoming motech event to be converted to a database event log * @param loggableEvent contains data about an event to log, used to configure returned log instance * @return EventLog object that is used to log the event to the database - * @throws org.motechproject.commons.api.MotechException if the passed LoggableEvent is not an instance of DbLoggableEvent + * @throws org.motechproject.commons.api.MotechException if the passed LoggableEvent is not an instance of MappedLoggableEvent */ - public EventLog configuredConvertEventToDbLog(MotechEvent eventToLog, LoggableEvent loggableEvent) { - if (!(loggableEvent instanceof DbLoggableEvent)) { - throw new MotechException("Error: unexpected loggable event type " + loggableEvent.getClass().getName()); - } - DbLoggableEvent dbLoggableEvent = (DbLoggableEvent) loggableEvent; - - LogMappings mappings = dbLoggableEvent.getMappings(); - - List keyValueList = mappings.getMappings(); - - List exclusions = mappings.getExclusions(); - - List inclusions = mappings.getInclusions(); - - Map initialParameters = eventToLog.getParameters(); - - Map finalParameters = new LinkedHashMap(initialParameters); - - for (KeyValue keyValue : keyValueList) { - if (initialParameters.containsKey(keyValue.getStartKey())) { - if (keyValue.getStartValue().equals(initialParameters.get(keyValue.getStartKey()))) { - finalParameters.put(keyValue.getEndKey(), keyValue.getEndValue()); - exclusions.add(keyValue.getStartKey()); - } - } - } - - for (String excludeParameter : exclusions) { - if (initialParameters.containsKey(excludeParameter)) { - finalParameters.remove(excludeParameter); - } - } - - for (String includeParameter : inclusions) { - if (initialParameters.containsKey(includeParameter)) { - finalParameters.put(includeParameter, initialParameters.get(includeParameter)); - } - } - - EventLog eventLog = new EventLog(eventToLog.getSubject(), finalParameters, DateTime.now()); + public EventLog configuredConvertEventToDbLog(MotechEvent eventToLog, MappedLoggableEvent loggableEvent) { + EventLog eventLog = new EventLog(eventToLog.getSubject(), loggableEvent.filterParams(eventToLog), DateTime.now()); return eventLog; } diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultFileToLogConverter.java b/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultFileToLogConverter.java index 309e8fe48..c1f27be7f 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultFileToLogConverter.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/converter/impl/DefaultFileToLogConverter.java @@ -3,6 +3,7 @@ import org.joda.time.DateTime; import org.motechproject.event.MotechEvent; import org.motechproject.eventlogging.converter.EventToLogConverter; +import org.motechproject.eventlogging.matchers.MappedLoggableEvent; import org.springframework.stereotype.Component; import java.util.Map; @@ -17,11 +18,25 @@ public class DefaultFileToLogConverter implements EventToLogConverter { @Override public String convertToLog(MotechEvent event) { - StringBuilder log = new StringBuilder("EVENT: "); + return convertToLogString(event.getSubject(), event.getParameters()); + } - log.append(event.getSubject() + " at TIME: " + DateTime.now()); + /** + * Converts an event into a {@link String} with parameters configured by {@link MappedLoggableEvent}. + * @param event Motech event to be converted into a file event log + * @param loggableEvent Used to configure parameter conversion + * @return String used to log into file. + */ + public String convertToLogMapped(MotechEvent event, MappedLoggableEvent loggableEvent) { + Map finalParameters = loggableEvent.filterParams(event); - Map parameters = event.getParameters(); + return convertToLogString(event.getSubject(), finalParameters); + } + + private String convertToLogString(String subject, Map parameters) { + StringBuilder log = new StringBuilder("EVENT: "); + + log.append(subject + " at TIME: " + DateTime.now()); if (parameters.size() > 0) { log.append(" with PARAMETERS: "); @@ -33,4 +48,5 @@ public String convertToLog(MotechEvent event) { return log.toString(); } + } diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/DbEventLogger.java b/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/DbEventLogger.java index 437616d9e..f112d2f92 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/DbEventLogger.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/DbEventLogger.java @@ -2,10 +2,10 @@ import org.motechproject.event.MotechEvent; import org.motechproject.eventlogging.converter.impl.DefaultDbToLogConverter; -import org.motechproject.eventlogging.matchers.DbLoggableEvent; import org.motechproject.eventlogging.domain.EventLog; -import org.motechproject.eventlogging.matchers.LoggableEvent; import org.motechproject.eventlogging.loggers.EventLogger; +import org.motechproject.eventlogging.matchers.LoggableEvent; +import org.motechproject.eventlogging.matchers.MappedLoggableEvent; import org.motechproject.eventlogging.service.EventLogService; /** @@ -36,8 +36,8 @@ public void log(MotechEvent eventToLog) { if (loggableEvent.isLoggableEvent(eventToLog)) { if (eventConverter != null) { EventLog eventLog; - if (loggableEvent instanceof DbLoggableEvent) { - eventLog = eventConverter.configuredConvertEventToDbLog(eventToLog, loggableEvent); + if (loggableEvent instanceof MappedLoggableEvent) { + eventLog = eventConverter.configuredConvertEventToDbLog(eventToLog, (MappedLoggableEvent) loggableEvent); } else { eventLog = eventConverter.convertToLog(eventToLog); } diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/FileEventLogger.java b/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/FileEventLogger.java index f7a39357f..f914ae6e6 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/FileEventLogger.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/loggers/impl/FileEventLogger.java @@ -4,6 +4,7 @@ import org.motechproject.eventlogging.converter.impl.DefaultFileToLogConverter; import org.motechproject.eventlogging.matchers.LoggableEvent; import org.motechproject.eventlogging.loggers.EventLogger; +import org.motechproject.eventlogging.matchers.MappedLoggableEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,7 +62,12 @@ public void log(MotechEvent eventToLog) { for (LoggableEvent loggableEvent : getLoggableEvents()) { if (loggableEvent.isLoggableEvent(eventToLog)) { if (eventConverter != null) { - String logString = eventConverter.convertToLog(eventToLog); + String logString; + if (loggableEvent instanceof MappedLoggableEvent) { + logString = eventConverter.convertToLogMapped(eventToLog, (MappedLoggableEvent) loggableEvent); + } else { + logString = eventConverter.convertToLog(eventToLog); + } log(logString); } else { return; diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/matchers/DbLoggableEvent.java b/event-logging/src/main/java/org/motechproject/eventlogging/matchers/DbLoggableEvent.java deleted file mode 100644 index a6d775d67..000000000 --- a/event-logging/src/main/java/org/motechproject/eventlogging/matchers/DbLoggableEvent.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.motechproject.eventlogging.matchers; - -import java.util.List; - -/** - * Version of {@link LoggableEvent} class used when working with DbEventLogger. Additionaly to LoggableEvent it - * stores {@link LogMappings} used for manipulating event parameters before persisting log in the database. - */ -public class DbLoggableEvent extends LoggableEvent { - - private LogMappings mappings; - - /** - * Creates an instance of DbLoggableEvent using passed parameters - * - * @param eventSubjects list of event subjects to log - * @param flags event flags used for filtering events by parameters - * @param mappings {@link LogMappings} object used for replacing, including and excluding parameters before logging to the database - */ - public DbLoggableEvent(List eventSubjects, List flags, LogMappings mappings) { - super(eventSubjects, flags); - this.mappings = mappings; - } - - public LogMappings getMappings() { - return mappings; - } - - public void setMappings(LogMappings mappings) { - this.mappings = mappings; - } -} diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/matchers/KeyValue.java b/event-logging/src/main/java/org/motechproject/eventlogging/matchers/KeyValue.java index 8517721be..212e8ae61 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/matchers/KeyValue.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/matchers/KeyValue.java @@ -1,5 +1,7 @@ package org.motechproject.eventlogging.matchers; +import java.util.Map; + /** * Class representing mapping from one parameter key-value pair to another. * Used in {@link org.motechproject.eventlogging.converter.impl.DefaultDbToLogConverter} @@ -72,4 +74,22 @@ public void setEndValue(Object endValue) { this.endValue = endValue; } + public static KeyValue buildFromMap(Map map) { + String startKey = null; + String startValue = null; + String endKey = null; + String endValue = null; + + for (Map.Entry entry : map.entrySet()) { + if (startKey == null) { + startKey = entry.getKey(); + startValue = entry.getValue(); + } else { + endKey = entry.getKey(); + endValue = entry.getValue(); + } + } + + return new KeyValue(startKey, startValue, endKey, endValue, true); + } } diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/matchers/MappedLoggableEvent.java b/event-logging/src/main/java/org/motechproject/eventlogging/matchers/MappedLoggableEvent.java new file mode 100644 index 000000000..ca73ab67e --- /dev/null +++ b/event-logging/src/main/java/org/motechproject/eventlogging/matchers/MappedLoggableEvent.java @@ -0,0 +1,70 @@ +package org.motechproject.eventlogging.matchers; + +import org.motechproject.event.MotechEvent; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Version of {@link LoggableEvent} class used when working with mappings. Additionaly to LoggableEvent it + * stores {@link LogMappings} used for manipulating event parameters before persisting log. + */ +public class MappedLoggableEvent extends LoggableEvent { + + private LogMappings mappings; + + /** + * Creates an instance of MappedLoggableEvent using passed parameters + * + * @param eventSubjects list of event subjects to log + * @param flags event flags used for filtering events by parameters + * @param mappings {@link LogMappings} object used for replacing, including and excluding parameters before logging to the database + */ + public MappedLoggableEvent(List eventSubjects, List flags, LogMappings mappings) { + super(eventSubjects, flags); + this.mappings = mappings; + } + + public LogMappings getMappings() { + return mappings; + } + + public void setMappings(LogMappings mappings) { + this.mappings = mappings; + } + + public Map filterParams(MotechEvent event) { + Map initialParameters = event.getParameters(); + + LogMappings logMappings = getMappings(); + List keyValueList = logMappings.getMappings(); + List exclusions = logMappings.getExclusions(); + List inclusions = logMappings.getInclusions(); + Map finalParameters = new LinkedHashMap(initialParameters); + + for (KeyValue keyValue : keyValueList) { + if (initialParameters.containsKey(keyValue.getStartKey())) { + if (keyValue.getStartValue().equals(initialParameters.get(keyValue.getStartKey()))) { + finalParameters.put(keyValue.getEndKey(), keyValue.getEndValue()); + exclusions.add(keyValue.getStartKey()); + } + } + } + + for (String excludeParameter : exclusions) { + if (initialParameters.containsKey(excludeParameter)) { + finalParameters.remove(excludeParameter); + } + } + + for (String includeParameter : inclusions) { + if (initialParameters.containsKey(includeParameter)) { + finalParameters.put(includeParameter, initialParameters.get(includeParameter)); + } + } + + return finalParameters; + } + +} diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/repository/AllEventMappings.java b/event-logging/src/main/java/org/motechproject/eventlogging/repository/AllEventMappings.java index 600f5db8f..c24bfe1bb 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/repository/AllEventMappings.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/repository/AllEventMappings.java @@ -2,15 +2,22 @@ import com.google.gson.reflect.TypeToken; import org.motechproject.commons.api.json.MotechJsonReader; +import org.motechproject.eventlogging.matchers.KeyValue; +import org.motechproject.eventlogging.matchers.LogMappings; +import org.motechproject.eventlogging.matchers.LoggableEvent; +import org.motechproject.eventlogging.matchers.MappedLoggableEvent; import org.motechproject.eventlogging.matchers.MappingsJson; import org.motechproject.config.SettingsFacade; +import org.motechproject.eventlogging.matchers.ParametersPresentEventFlag; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import java.io.InputStream; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * Class used for retrieving event mappings from the json configuration file. @@ -52,4 +59,56 @@ public List getAllMappings() { return mappings; } + + /** + * Returns loggable events from event mappings stored in configuration file. + * @return list of loggable events + */ + public List converToLoggableEvents() { + List allMappings = getAllMappings(); + List loggableEvents = new ArrayList<>(); + for (MappingsJson mapping : allMappings) { + if (mapping.getMappings() == null && mapping.getIncludes() == null && mapping.getExcludes() == null) { + LoggableEvent event = new LoggableEvent(mapping.getSubjects(), mapping.getFlags()); + loggableEvents.add(event); + } else { + MappedLoggableEvent mappedEvent = new MappedLoggableEvent(mapping.getSubjects(), null, null); + + List mappings = null; + + if (mapping.getMappings() != null) { + List> mappingList = mapping.getMappings(); + mappings = new ArrayList<>(); + + List subjects = mapping.getSubjects(); + + if (subjects != null) { + for (Map map : mappingList) { + KeyValue keyValue = KeyValue.buildFromMap(map); + mappings.add(keyValue); + } + } + } + + List inclusions = mapping.getIncludes(); + List exclusions = mapping.getExcludes(); + + LogMappings logMappings = new LogMappings(mappings, exclusions, inclusions); + + mappedEvent.setMappings(logMappings); + + List eventFlags = mapping.getFlags(); + + if (eventFlags != null) { + mappedEvent.setFlags(eventFlags); + } + + + loggableEvents.add(mappedEvent); + } + } + + return loggableEvents; + } + } diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/service/Constants.java b/event-logging/src/main/java/org/motechproject/eventlogging/service/Constants.java new file mode 100644 index 000000000..831495abf --- /dev/null +++ b/event-logging/src/main/java/org/motechproject/eventlogging/service/Constants.java @@ -0,0 +1,14 @@ +package org.motechproject.eventlogging.service; + + +public final class Constants { + + private Constants() { + + } + + public static final String DB_ENABLED_PROPERTY = "eventlogging.db.enabled"; + public static final String FILE_ENABLED_PROPERTY = "eventlogging.file.enabled"; + public static final String LOGFILE_PATH_PROPERTY = "eventlogging.logfile.path"; + +} diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/DbEventLoggingService.java b/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/DbEventLoggingService.java index 11232dbfc..b38abc70b 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/DbEventLoggingService.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/DbEventLoggingService.java @@ -2,13 +2,8 @@ import org.motechproject.event.MotechEvent; import org.motechproject.eventlogging.converter.impl.DefaultDbToLogConverter; -import org.motechproject.eventlogging.matchers.DbLoggableEvent; -import org.motechproject.eventlogging.matchers.KeyValue; -import org.motechproject.eventlogging.matchers.LogMappings; -import org.motechproject.eventlogging.matchers.LoggableEvent; -import org.motechproject.eventlogging.matchers.MappingsJson; -import org.motechproject.eventlogging.matchers.ParametersPresentEventFlag; import org.motechproject.eventlogging.loggers.impl.DbEventLogger; +import org.motechproject.eventlogging.matchers.LoggableEvent; import org.motechproject.eventlogging.repository.AllEventMappings; import org.motechproject.eventlogging.service.EventLogService; import org.motechproject.eventlogging.service.EventLoggingService; @@ -20,7 +15,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; /** @@ -64,48 +58,8 @@ public DbEventLoggingService(AllEventMappings allEventMappings) { @PostConstruct private DbEventLogger createDefaultEventLogger() { - List allMappings = allEventMappings.getAllMappings(); defaultDbEventLogger = new DbEventLogger(eventLogService, defaultDbToLogConverter); - List loggableEvents = new ArrayList<>(); - for (MappingsJson mapping : allMappings) { - if (mapping.getMappings() == null && mapping.getIncludes() == null && mapping.getExcludes() == null) { - LoggableEvent event = new LoggableEvent(mapping.getSubjects(), mapping.getFlags()); - loggableEvents.add(event); - } else { - List mappings = null; - - DbLoggableEvent dbLoggableEvent = new DbLoggableEvent(mapping.getSubjects(), null, null); - - if (mapping.getMappings() != null) { - List> mappingList = mapping.getMappings(); - mappings = new ArrayList<>(); - - List subjects = mapping.getSubjects(); - - if (subjects != null) { - for (Map map : mappingList) { - KeyValue keyValue = constructKeyValue(map); - mappings.add(keyValue); - } - } - } - - List inclusions = mapping.getIncludes(); - List exclusions = mapping.getExcludes(); - - LogMappings logMappings = new LogMappings(mappings, exclusions, inclusions); - - dbLoggableEvent.setMappings(logMappings); - - List eventFlags = mapping.getFlags(); - - if (eventFlags != null) { - dbLoggableEvent.setFlags(eventFlags); - } - - loggableEvents.add(dbLoggableEvent); - } - } + List loggableEvents = allEventMappings.converToLoggableEvents(); defaultDbEventLogger.addLoggableEvents(loggableEvents); return defaultDbEventLogger; @@ -183,22 +137,4 @@ public void setDefaultDbToLogConverter(DefaultDbToLogConverter defaultDbToLogCon this.defaultDbToLogConverter = defaultDbToLogConverter; } - private KeyValue constructKeyValue(Map map) { - String startKey = null; - String startValue = null; - String endKey = null; - String endValue = null; - - for (Map.Entry entry : map.entrySet()) { - if (startKey == null) { - startKey = entry.getKey(); - startValue = entry.getValue(); - } else { - endKey = entry.getKey(); - endValue = entry.getValue(); - } - } - - return new KeyValue(startKey, startValue, endKey, endValue, true); - } } diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/EventLoggingServiceManagerImpl.java b/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/EventLoggingServiceManagerImpl.java index be13f9db4..d7810b1d1 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/EventLoggingServiceManagerImpl.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/EventLoggingServiceManagerImpl.java @@ -1,15 +1,19 @@ package org.motechproject.eventlogging.service.impl; +import org.motechproject.config.SettingsFacade; import org.motechproject.event.MotechEvent; import org.motechproject.event.listener.EventListener; import org.motechproject.event.listener.EventListenerRegistryService; import org.motechproject.event.listener.annotations.MotechListenerEventProxy; +import org.motechproject.eventlogging.service.Constants; import org.motechproject.eventlogging.service.EventLoggingService; import org.motechproject.eventlogging.service.EventLoggingServiceManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import javax.annotation.PostConstruct; import java.lang.reflect.Method; @@ -34,12 +38,29 @@ public class EventLoggingServiceManagerImpl implements EventLoggingServiceManage @Autowired private DbEventLoggingService dbEventLoggingService; + @Autowired + private FileEventLoggingService fileEventLoggingService; + + private SettingsFacade settingsFacade; + + @Autowired + public void setSettingsFacade(@Qualifier("eventLoggingSettings") SettingsFacade settingsFacade) { + this.settingsFacade = settingsFacade; + } + /** * Registers default service for event logging, which is DbEventLoggingService */ @PostConstruct public void registerDefaultService() { - registerEventLoggingService(dbEventLoggingService); + if (Boolean.parseBoolean(settingsFacade.getProperty(Constants.DB_ENABLED_PROPERTY))) { + registerEventLoggingService(dbEventLoggingService); + } + + if (Boolean.parseBoolean(settingsFacade.getProperty(Constants.FILE_ENABLED_PROPERTY)) + && !StringUtils.isEmpty(settingsFacade.getProperty(Constants.LOGFILE_PATH_PROPERTY))) { + registerEventLoggingService(fileEventLoggingService); + } } @Override diff --git a/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/FileEventLoggingService.java b/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/FileEventLoggingService.java index e652d746e..f9da3d536 100644 --- a/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/FileEventLoggingService.java +++ b/event-logging/src/main/java/org/motechproject/eventlogging/service/impl/FileEventLoggingService.java @@ -1,11 +1,20 @@ package org.motechproject.eventlogging.service.impl; +import org.motechproject.config.SettingsFacade; import org.motechproject.event.MotechEvent; -import org.motechproject.eventlogging.matchers.LoggableEvent; +import org.motechproject.eventlogging.converter.impl.DefaultFileToLogConverter; import org.motechproject.eventlogging.loggers.EventLogger; import org.motechproject.eventlogging.loggers.impl.FileEventLogger; +import org.motechproject.eventlogging.matchers.LoggableEvent; +import org.motechproject.eventlogging.repository.AllEventMappings; +import org.motechproject.eventlogging.service.Constants; import org.motechproject.eventlogging.service.EventLoggingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; +import javax.annotation.PostConstruct; +import java.io.File; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -15,10 +24,20 @@ * Implementation of {@link org.motechproject.eventlogging.service.EventLoggingService} * It is used to log events to files. */ +@Service public class FileEventLoggingService implements EventLoggingService { private List fileEventLoggers = new ArrayList<>(); + @Autowired + private DefaultFileToLogConverter defaultFileToLogConverter; + + @Autowired + private AllEventMappings allEventMappings; + + private SettingsFacade settingsFacade; + + /** * Creates an instance of FileEventLoggingService without any loggers. */ @@ -38,6 +57,22 @@ public FileEventLoggingService(List fileEventLoggers) { } } + @Autowired + public void setSettingsFacade(@Qualifier("eventLoggingSettings") SettingsFacade settingsFacade) { + this.settingsFacade = settingsFacade; + } + + @PostConstruct + public void configureFileLoggingService() { + List loggingFiles = new ArrayList<>(); + String logFilePath = settingsFacade.getProperty(Constants.LOGFILE_PATH_PROPERTY); + loggingFiles.add(new File(logFilePath)); + + List loggableEvents = allEventMappings.converToLoggableEvents(); + FileEventLogger logger = new FileEventLogger(loggableEvents, loggingFiles, defaultFileToLogConverter); + fileEventLoggers.add(logger); + } + @Override public void logEvent(MotechEvent event) { for (FileEventLogger fileEventLogger : fileEventLoggers) { diff --git a/event-logging/src/main/resources/META-INF/motech/applicationEventLogging.xml b/event-logging/src/main/resources/META-INF/motech/applicationEventLogging.xml index deed7bf17..a52b5788d 100644 --- a/event-logging/src/main/resources/META-INF/motech/applicationEventLogging.xml +++ b/event-logging/src/main/resources/META-INF/motech/applicationEventLogging.xml @@ -9,6 +9,11 @@ + + + classpath:event-logging-defaults.properties + + classpath:event-mappings.json diff --git a/event-logging/src/main/resources/event-logging-defaults.properties b/event-logging/src/main/resources/event-logging-defaults.properties new file mode 100644 index 000000000..d22c9e8e1 --- /dev/null +++ b/event-logging/src/main/resources/event-logging-defaults.properties @@ -0,0 +1,3 @@ +eventlogging.db.enabled=true +eventlogging.file.enabled=false +eventlogging.logfile.path= \ No newline at end of file diff --git a/event-logging/src/test/java/org/motechproject/eventlogging/service/impl/DbEventLoggingServiceTest.java b/event-logging/src/test/java/org/motechproject/eventlogging/service/impl/DbEventLoggingServiceTest.java index b71762310..9f2f1afe7 100644 --- a/event-logging/src/test/java/org/motechproject/eventlogging/service/impl/DbEventLoggingServiceTest.java +++ b/event-logging/src/test/java/org/motechproject/eventlogging/service/impl/DbEventLoggingServiceTest.java @@ -3,12 +3,13 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; +import org.motechproject.eventlogging.loggers.impl.DbEventLogger; import org.motechproject.eventlogging.matchers.LogMappings; -import org.motechproject.eventlogging.matchers.DbLoggableEvent; import org.motechproject.eventlogging.matchers.LoggableEvent; +import org.motechproject.eventlogging.matchers.MappedLoggableEvent; import org.motechproject.eventlogging.matchers.MappingsJson; import org.motechproject.eventlogging.matchers.ParametersPresentEventFlag; -import org.motechproject.eventlogging.loggers.impl.DbEventLogger; import org.motechproject.eventlogging.repository.AllEventMappings; import java.util.ArrayList; @@ -117,6 +118,7 @@ public void shouldSetAllLoggableEventsCorrectly() { mappings.add(mapping2); when(allEventMappings.getAllMappings()).thenReturn(mappings); + Mockito.doCallRealMethod().when(allEventMappings).converToLoggableEvents(); dbEventLoggingService = new DbEventLoggingService(allEventMappings); @@ -137,13 +139,13 @@ public void shouldSetAllLoggableEventsCorrectly() { assertNotNull(event2); - assertFalse(event.getClass().equals(DbLoggableEvent.class)); + assertFalse(event.getClass().equals(MappedLoggableEvent.class)); assertTrue(event.getClass().equals(LoggableEvent.class)); - assertTrue(event2.getClass().equals(DbLoggableEvent.class)); + assertTrue(event2.getClass().equals(MappedLoggableEvent.class)); assertFalse(event2.getClass().equals(LoggableEvent.class)); - DbLoggableEvent castedEvent = (DbLoggableEvent) event2; + MappedLoggableEvent castedEvent = (MappedLoggableEvent) event2; LogMappings logMappings = castedEvent.getMappings();