Last active
June 12, 2022 14:18
-
-
Save SecretX33/0fefa5543e25c638aaadee7916767ceb to your computer and use it in GitHub Desktop.
How to create and register a Logger filter into Spigot/Paper server
This file contains 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
//... | |
dependencies { | |
// add Log4j as compileOnly (provided) dependency on your favorite build tool | |
compileOnly "org.apache.logging.log4j:log4j-core:2.17.2" | |
} |
This file contains 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
import org.apache.logging.log4j.Level; | |
import org.apache.logging.log4j.Marker; | |
import org.apache.logging.log4j.core.Filter; | |
import org.apache.logging.log4j.core.LogEvent; | |
import org.apache.logging.log4j.core.Logger; | |
import org.apache.logging.log4j.core.filter.AbstractFilter; | |
import org.apache.logging.log4j.message.Message; | |
import org.jetbrains.annotations.NotNull; | |
import org.jetbrains.annotations.Nullable; | |
public class MyLogFilter extends AbstractFilter { | |
private static final String STRING_WE_DISLIKE = "Disconnecting com.mojang.authlib.GameProfile@"; | |
/** | |
* This determines if our filter runs <i>before</i> (if {@code true}) or <i>after</i> (if {@code false}) | |
* the string placeholders are replaced. | |
* | |
* Usually there's no need to change this. | |
*/ | |
private static final boolean USE_RAW_STRING = false; | |
public MyLogFilter() { | |
// register this filter with the intention of filtering out any message that matches | |
// this filter (DENY), else we don't have any particular opinion about it (NEUTRAL) | |
// | |
// super(onMatch, onMismatch) | |
super(Filter.Result.DENY, Filter.Result.NEUTRAL); | |
} | |
/** | |
* Here is where we decide if we want to filter out the message or not. Returning {@link Filter.Result#DENY} | |
* (our {@link AbstractFilter#onMatch onMatch}) will filter out the message, and {@link Filter.Result#NEUTRAL} | |
* (our {@link AbstractFilter#onMismatch onMismatch}) will leave it alone. | |
*/ | |
@NotNull | |
private Result doFilter(@Nullable String message) { | |
if (message == null || !message.startsWith(STRING_WE_DISLIKE)) { | |
return onMismatch; | |
} | |
return onMatch; | |
} | |
/** | |
* Since this implementation inherits from an abstract filter class, we need to override these four | |
* methods to make sure that our filter will be applied to all messages.<br><br> | |
* | |
* Almost all parameter of all methods below are <b>nullable</b>, you don't need to touch them since | |
* we already delegate their results to our important, custom filter method above. | |
*/ | |
@Override | |
public Result filter(LogEvent event) { | |
Message msg = event == null ? null : event.getMessage(); | |
String message = msg == null ? null : (USE_RAW_STRING | |
? msg.getFormat() | |
: msg.getFormattedMessage()); | |
return doFilter(message); | |
} | |
@Override | |
public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) { | |
return doFilter(msg == null ? null : msg.toString()); | |
} | |
@Override | |
public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) { | |
return doFilter(msg); | |
} | |
@Override | |
public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) { | |
String message = msg == null ? null : (USE_RAW_STRING | |
? msg.getFormat() | |
: msg.getFormattedMessage()); | |
return doFilter(message); | |
} | |
} |
This file contains 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
import yourpkg.yourplugin.filter.MyLogFilter; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.core.Filter; | |
import org.apache.logging.log4j.core.Logger; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class MyMainClass extends JavaPlugin { | |
@Override | |
public void onEnable() { | |
// do your things, then register the filters | |
registerLoggerFilters(new MyLogFilter()); | |
} | |
private void registerLoggerFilters(Filter... filters) { | |
org.apache.logging.log4j.Logger rootLogger = LogManager.getRootLogger(); | |
if (!(rootLogger instanceof Logger)) { | |
// in case the root logger is not the expected instance of Logger, just return | |
// because there is something wrong | |
return; | |
} | |
Logger logger = (Logger) rootLogger; | |
for (Filter filter : filters) { | |
// register all filters onto the root logger | |
logger.addFilter(filter); | |
} | |
} | |
} |
This file contains 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
//... | |
<!-- add Log4j as provided (compileOnly) dependency on your favorite build tool --> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-core</artifactId> | |
<version>2.17.2</version> | |
<scope>provided</scope> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment