Skip to content

Commit

Permalink
#4377 - Configure include and exclude patterns for logged events
Browse files Browse the repository at this point in the history
- Auto-format
  • Loading branch information
reckart committed Feb 2, 2024
1 parent 83411cb commit 0eaa21d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public void onApplicationEvent(ApplicationEvent aEvent)
if (!maybeAdapter.isPresent()) {
return;
}


var adapter = maybeAdapter.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public interface EventLoggingProperties
Set<String> getIncludePatterns();

/**
* @param includePatterns Set of regex include patterns
* @param includePatterns
* Set of regex include patterns
*/
void setIncludePatterns(Set<String> includePatterns);

Expand All @@ -41,14 +42,15 @@ public interface EventLoggingProperties
Set<String> getExcludePatterns();

/**
* @param excludePatterns Set of regex exclude patterns
* @param excludePatterns
* Set of regex exclude patterns
*/
void setExcludePatterns(Set<String> excludePatterns);

/**
* @param eventName Name of the event to check.
* @param eventName
* Name of the event to check.
* @return true if the event should be logged, false otherwise.
*/
boolean shouldLogEvent(String eventName);
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,60 @@
import de.tudarmstadt.ukp.inception.documents.event.AfterCasWrittenEvent;

@ConfigurationProperties("event-logging")
public class EventLoggingPropertiesImpl implements EventLoggingProperties {
private boolean enabled;
public class EventLoggingPropertiesImpl
implements EventLoggingProperties
{
private boolean enabled;

private Set<String> includePatterns = Set.of(".*"); // Default include everything

private Set<String> excludePatterns = Set.of(
AfterCasWrittenEvent.class.getSimpleName(),
AvailabilityChangeEvent.class.getSimpleName(),
"RecommenderTaskNotificationEvent",
BeforeDocumentOpenedEvent.class.getSimpleName(),
PreparingToOpenDocumentEvent.class.getSimpleName(),
"BrokerAvailabilityEvent",
"ShutdownDialogAvailableEvent");
private Set<String> includePatterns = Set.of(".*"); // Default include everything

@Override
public boolean isEnabled() {
return enabled;
}
private Set<String> excludePatterns = Set.of(AfterCasWrittenEvent.class.getSimpleName(),
AvailabilityChangeEvent.class.getSimpleName(), "RecommenderTaskNotificationEvent",
BeforeDocumentOpenedEvent.class.getSimpleName(),
PreparingToOpenDocumentEvent.class.getSimpleName(), "BrokerAvailabilityEvent",
"ShutdownDialogAvailableEvent");

@Override
public void setEnabled(boolean aEnabled) {
enabled = aEnabled;
}
@Override
public boolean isEnabled()
{
return enabled;
}

@Override
public Set<String> getIncludePatterns() {
return includePatterns;
}
@Override
public void setEnabled(boolean aEnabled)
{
enabled = aEnabled;
}

@Override
public void setIncludePatterns(Set<String> includePatterns) {
this.includePatterns = includePatterns;
}
@Override
public Set<String> getIncludePatterns()
{
return includePatterns;
}

@Override
public Set<String> getExcludePatterns() {
return excludePatterns;
}
@Override
public void setIncludePatterns(Set<String> includePatterns)
{
this.includePatterns = includePatterns;
}

@Override
public void setExcludePatterns(Set<String> excludePatterns) {
this.excludePatterns = excludePatterns;
}
@Override
public Set<String> getExcludePatterns()
{
return excludePatterns;
}

@Override
public boolean shouldLogEvent(String eventName) {
return includePatterns.stream().anyMatch(pattern -> Pattern.matches(pattern, eventName))
&& excludePatterns.stream().noneMatch(pattern -> Pattern.matches(pattern, eventName));
}
@Override
public void setExcludePatterns(Set<String> excludePatterns)
{
this.excludePatterns = excludePatterns;
}

@Override
public boolean shouldLogEvent(String eventName)
{
return includePatterns.stream().anyMatch(pattern -> Pattern.matches(pattern, eventName))
&& excludePatterns.stream()
.noneMatch(pattern -> Pattern.matches(pattern, eventName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package de.tudarmstadt.ukp.inception.log.config;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.availability.AvailabilityChangeEvent;
Expand All @@ -26,29 +29,29 @@
import de.tudarmstadt.ukp.inception.annotation.events.PreparingToOpenDocumentEvent;
import de.tudarmstadt.ukp.inception.documents.event.AfterCasWrittenEvent;

import java.util.Set;
class EventLoggingPropertiesImplTest
{

class EventLoggingPropertiesImplTest {

private EventLoggingPropertiesImpl properties;

@BeforeEach
public void setUp() {
public void setUp()
{
properties = new EventLoggingPropertiesImpl();
}

@Test
public void shouldLogEvent_defaultExcludeInternalList_ReturnsFalse() {
public void shouldLogEvent_defaultExcludeInternalList_ReturnsFalse()
{
// Test events
String eventAfterCasWritten = AfterCasWrittenEvent.class.getSimpleName();
String eventAvailabilityChange = AvailabilityChangeEvent.class.getSimpleName();
String eventRecommenderTaskNotification = "RecommenderTaskNotificationEvent";
String eventBeforeDocumentOpened = BeforeDocumentOpenedEvent.class.getSimpleName();
String eventPreparingToOpenDocument = PreparingToOpenDocumentEvent.class.getSimpleName();
String eventBrokerAvailability = "BrokerAvailabilityEvent";
String eventShutdownDialogAvailable = "ShutdownDialogAvailableEvent";


String eventAfterCasWritten = AfterCasWrittenEvent.class.getSimpleName();
String eventAvailabilityChange = AvailabilityChangeEvent.class.getSimpleName();
String eventRecommenderTaskNotification = "RecommenderTaskNotificationEvent";
String eventBeforeDocumentOpened = BeforeDocumentOpenedEvent.class.getSimpleName();
String eventPreparingToOpenDocument = PreparingToOpenDocumentEvent.class.getSimpleName();
String eventBrokerAvailability = "BrokerAvailabilityEvent";
String eventShutdownDialogAvailable = "ShutdownDialogAvailableEvent";

// Assert
assertThat(properties.shouldLogEvent(eventAfterCasWritten)).isFalse();
assertThat(properties.shouldLogEvent(eventAvailabilityChange)).isFalse();
Expand All @@ -58,18 +61,20 @@ public void shouldLogEvent_defaultExcludeInternalList_ReturnsFalse() {
assertThat(properties.shouldLogEvent(eventBrokerAvailability)).isFalse();
assertThat(properties.shouldLogEvent(eventShutdownDialogAvailable)).isFalse();
}

@Test
public void shouldLogEvent_EventNotInExcludeLists_ReturnsTrue() {
public void shouldLogEvent_EventNotInExcludeLists_ReturnsTrue()
{
// Test events
String eventAfterDocumentOpened = "AfterDocumentOpenedEvent";

// Assert
assertThat(properties.shouldLogEvent(eventAfterDocumentOpened)).isTrue();
}

@Test
public void shouldLogEvent_setExcludeWorksAndEventsGetExcludedTrue() {
public void shouldLogEvent_setExcludeWorksAndEventsGetExcludedTrue()
{
// Set exclude patterns
properties.setExcludePatterns(Set.of("AfterDocumentOpenedEvent"));

Expand All @@ -79,9 +84,10 @@ public void shouldLogEvent_setExcludeWorksAndEventsGetExcludedTrue() {
// Assert
assertThat(properties.shouldLogEvent(eventAfterDocumentOpened)).isFalse();
}

@Test
public void shouldLogEvent_setIncludeWorksAndOnlyEventsSetIncludedWork() {
public void shouldLogEvent_setIncludeWorksAndOnlyEventsSetIncludedWork()
{
// Set include patterns
properties.setIncludePatterns(Set.of("AfterDocumentOpenedEvent"));

Expand All @@ -94,5 +100,4 @@ public void shouldLogEvent_setIncludeWorksAndOnlyEventsSetIncludedWork() {
assertThat(properties.shouldLogEvent(eventDocumentStateChanged)).isFalse();
}


}

0 comments on commit 0eaa21d

Please sign in to comment.