-
-
Notifications
You must be signed in to change notification settings - Fork 105
Create Custom Filter for the AsyncAppender #327
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8ed7632
Create Custom Appender wrapping the AsyncAppender
0ac2b7a
Create Custom Appender wrapping the AsyncAppender
af7a257
Switch over to custom Filter, since functionality is more applicable
34be9b8
package-info
45d400c
Add test for new Functionality
f9f822b
Sonar Lint
a6bca16
Spring Version Update (#324)
krankkkk ec7986e
rebase onto Dev
69ec4e1
Merge branch 'develop' into feature/custom_appender
krankkkk 1a68b5f
adding final & private to Test Class
krankkkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
application/src/main/java/org/togetherjava/tjbot/logging/FlaggedFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.togetherjava.tjbot.logging; | ||
|
|
||
| import org.apache.logging.log4j.core.Core; | ||
| import org.apache.logging.log4j.core.Filter; | ||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
| import org.apache.logging.log4j.core.config.plugins.PluginAttribute; | ||
| import org.apache.logging.log4j.core.config.plugins.PluginFactory; | ||
| import org.apache.logging.log4j.core.filter.AbstractFilter; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
|
|
||
| /** | ||
| * A custom Filter for Log4j2, which only lets an event pass through if a Logging Flag is set in the | ||
| * environment. Intended to be used for local development for devs do not want to also run the | ||
| * logviewer project. No errors in console or Log should appear, if the Flag is not set and the | ||
| * logviewer is not running. | ||
| */ | ||
| @Plugin(name = "FlaggedFilter", category = Core.CATEGORY_NAME, elementType = Filter.ELEMENT_TYPE) | ||
| public class FlaggedFilter extends AbstractFilter { | ||
|
|
||
| /** | ||
| * The environment Variable that needs to bet set in order for this Filter to let events through | ||
| */ | ||
| public static final String LOGGING_FLAG = "TJ_APPENDER"; | ||
|
|
||
| /** | ||
| * Create a FlaggedFilter. | ||
| * | ||
| * @param onMatch The action to take on a match. | ||
| * @param onMismatch The action to take on a mismatch. | ||
| */ | ||
| public FlaggedFilter(@NotNull Result onMatch, @NotNull Result onMismatch) { | ||
| super(onMatch, onMismatch); | ||
| } | ||
|
|
||
| /** | ||
| * The actual filtering occurs here. If the Flag {@link #LOGGING_FLAG} is not set returns | ||
| * {@link Result#DENY} so nothing goes through. If the Flag is set it returns | ||
| * {@link Result#NEUTRAL} so other configured Filter still work. | ||
| * | ||
| * @param event The Event to log. | ||
| * @return {@link Result#DENY} if the Flag is not set, else {@link Result#NEUTRAL} | ||
| */ | ||
| @Override | ||
| public Result filter(LogEvent event) { | ||
| return isLoggingEnabled() ? Result.NEUTRAL : Result.DENY; | ||
| } | ||
|
|
||
| boolean isLoggingEnabled() { | ||
| return System.getenv().containsKey(LOGGING_FLAG); | ||
| } | ||
|
|
||
| /** | ||
| * Required by the Log4j2 - Plugin framework in order to create an instance of this Filter. | ||
| * | ||
| * @param onMatch The action to take on a match. | ||
| * @param onMismatch The action to take on a mismatch. | ||
| * @return The created FlaggedFilter. | ||
| */ | ||
| @PluginFactory | ||
| public static FlaggedFilter createFilter( | ||
| @NotNull @PluginAttribute(value = "onMatch", defaultString = "NEUTRAL") Result onMatch, | ||
| @NotNull | ||
| @PluginAttribute(value = "onMismatch", defaultString = "DENY") Result onMismatch) { | ||
| return new FlaggedFilter(onMatch, onMismatch); | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
application/src/main/java/org/togetherjava/tjbot/logging/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** | ||
| * This package is for custom logging plugins of the bot. | ||
| */ | ||
| package org.togetherjava.tjbot.logging; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
application/src/test/java/org/togetherjava/tjbot/logging/FilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package org.togetherjava.tjbot.logging; | ||
|
|
||
| import org.apache.logging.log4j.core.Filter; | ||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.apache.logging.log4j.core.impl.Log4jLogEvent; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| class FilterTest { | ||
Zabuzard marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| FlaggedFilter filter; | ||
| LogEvent event; | ||
Zabuzard marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| this.filter = FlaggedFilter.createFilter(Filter.Result.NEUTRAL, Filter.Result.DENY); | ||
| this.event = Log4jLogEvent.newBuilder().build(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldPassFilter() { | ||
| FlaggedFilter spy = Mockito.spy(this.filter); | ||
| Mockito.when(spy.isLoggingEnabled()).thenReturn(true); | ||
| Assertions.assertEquals(Filter.Result.NEUTRAL, spy.filter(this.event)); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void shouldNotPassFilter() { | ||
| FlaggedFilter spy = Mockito.spy(this.filter); | ||
| Mockito.when(spy.isLoggingEnabled()).thenReturn(false); | ||
| Assertions.assertEquals(Filter.Result.DENY, spy.filter(this.event)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.