Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4377 - Configure include and exclude patterns for logged events #4490

Merged
merged 12 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void onApplicationEvent(ApplicationEvent aEvent)

var adapter = maybeAdapter.get();

if (properties.getExcludeEvents().contains(adapter.getEvent(aEvent))) {
if (!properties.shouldLogEvent(adapter.getEvent(aEvent))) {
serwarde marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,32 @@ public interface EventLoggingProperties

boolean isEnabled();

Set<String> getExcludeEvents();
/**
* @return Set of regex include patterns
*/
Set<String> getIncludePatterns();

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

/**
* @return Set of regex exclude patterns
*/
Set<String> getExcludePatterns();

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

/**
* @param aExcludeEvents
* events never to be written to the event log.
* @param eventName
* Name of the event to check.
* @return true if the event should be logged, false otherwise.
*/
void setExcludeEvents(Set<String> aExcludeEvents);
boolean shouldLogEvent(String eventName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package de.tudarmstadt.ukp.inception.log.config;

import java.util.Set;
import java.util.regex.Pattern;

import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand All @@ -32,14 +33,12 @@ public class EventLoggingPropertiesImpl
{
private boolean enabled;

private Set<String> excludeEvents = Set.of( //
// Do not log this by default - hardly any information value
AfterCasWrittenEvent.class.getSimpleName(), //
AvailabilityChangeEvent.class.getSimpleName(), //
"RecommenderTaskNotificationEvent", //
BeforeDocumentOpenedEvent.class.getSimpleName(), //
PreparingToOpenDocumentEvent.class.getSimpleName(), //
"BrokerAvailabilityEvent", //
private Set<String> includePatterns = Set.of(".*"); // Default include everything
reckart marked this conversation as resolved.
Show resolved Hide resolved

private Set<String> excludePatterns = Set.of(AfterCasWrittenEvent.class.getSimpleName(),
AvailabilityChangeEvent.class.getSimpleName(), "RecommenderTaskNotificationEvent",
BeforeDocumentOpenedEvent.class.getSimpleName(),
PreparingToOpenDocumentEvent.class.getSimpleName(), "BrokerAvailabilityEvent",
"ShutdownDialogAvailableEvent");

@Override
Expand All @@ -55,14 +54,34 @@ public void setEnabled(boolean aEnabled)
}

@Override
public Set<String> getExcludeEvents()
public Set<String> getIncludePatterns()
{
return includePatterns;
}

@Override
public void setIncludePatterns(Set<String> includePatterns)
serwarde marked this conversation as resolved.
Show resolved Hide resolved
{
this.includePatterns = includePatterns;
}

@Override
public Set<String> getExcludePatterns()
{
return excludePatterns;
}

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

@Override
public void setExcludeEvents(Set<String> aExcludeEvents)
public boolean shouldLogEvent(String eventName)
{
excludeEvents = aExcludeEvents;
return includePatterns.stream().anyMatch(pattern -> Pattern.matches(pattern, eventName))
serwarde marked this conversation as resolved.
Show resolved Hide resolved
&& excludePatterns.stream()
.noneMatch(pattern -> Pattern.matches(pattern, eventName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Licensed to the Technische Universität Darmstadt under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Technische Universität Darmstadt
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;

import de.tudarmstadt.ukp.inception.annotation.events.BeforeDocumentOpenedEvent;
import de.tudarmstadt.ukp.inception.annotation.events.PreparingToOpenDocumentEvent;
import de.tudarmstadt.ukp.inception.documents.event.AfterCasWrittenEvent;

class EventLoggingPropertiesImplTest
{

private EventLoggingPropertiesImpl properties;

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

@Test
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";

// Assert
assertThat(properties.shouldLogEvent(eventAfterCasWritten)).isFalse();
assertThat(properties.shouldLogEvent(eventAvailabilityChange)).isFalse();
assertThat(properties.shouldLogEvent(eventRecommenderTaskNotification)).isFalse();
assertThat(properties.shouldLogEvent(eventBeforeDocumentOpened)).isFalse();
assertThat(properties.shouldLogEvent(eventPreparingToOpenDocument)).isFalse();
assertThat(properties.shouldLogEvent(eventBrokerAvailability)).isFalse();
assertThat(properties.shouldLogEvent(eventShutdownDialogAvailable)).isFalse();
}

@Test
public void shouldLogEvent_EventNotInExcludeLists_ReturnsTrue()
{
// Test events
String eventAfterDocumentOpened = "AfterDocumentOpenedEvent";
serwarde marked this conversation as resolved.
Show resolved Hide resolved

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

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

// Test events
String eventAfterDocumentOpened = "AfterDocumentOpenedEvent";

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

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

// Test events
String eventAfterDocumentOpened = "AfterDocumentOpenedEvent";
String eventDocumentStateChanged = "DocumentStateChangedEvent";

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

}
Loading