Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 1 addition & 2 deletions application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ dependencies {

implementation 'net.dv8tion:JDA:4.4.0_351'

compileOnly 'org.apache.logging.log4j:log4j-api:2.16.0'
runtimeOnly 'org.apache.logging.log4j:log4j-core:2.16.0'
implementation 'org.apache.logging.log4j:log4j-core:2.16.0'
runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.16.0'

implementation 'org.jooq:jooq:3.15.3'
Expand Down
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);
}
}
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;
3 changes: 2 additions & 1 deletion application/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Configuration status="WARN" monitorInterval="30" packages="org.togetherjava.tjbot.logging">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
Expand All @@ -17,6 +17,7 @@
<JsonLayout/>
</Http>
<Async name="AsyncHttp">
<FlaggedFilter/>
<AppenderRef ref="Http"/>
</Async>
</Appenders>
Expand Down
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 {

FlaggedFilter filter;
LogEvent event;

@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));
}
}