So, I'm trying to implement a logging filter to prevent certain commands from being shown as "<player> issued server command: <command>", but I cannot figure out a way to do so. After some digging I found out that these messages are sent through the apache LogManager.getLogger() logger, instead of the regular bukkit one. I can't find a way to add a filter to these apache loggers though. So, how do I make a filter to an apache logger?
@AlvinB One way seems to be this: <Unoptimized, 5 minute code> Code:java ReflectResponse<Object> fieldValue = ReflectionUtil.getFieldValue( "LOGGER", ReflectionUtil.getClass(NameSpace.NMS, "PlayerConnection").get(), null);if (fieldValue.isValuePresent()) { System.out.println("Injecting"); org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) fieldValue.getValue(); logger.addFilter(new Filter() { @Override public Result getOnMismatch() { return Result.ACCEPT; } @Override public Result getOnMatch() { return Result.DENY; } @Override public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, String s, Object... objects) { return s.contains("issued server command:") ? Result.DENY : Result.ACCEPT; } @Override public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Object o, Throwable throwable) { return Result.NEUTRAL; } @Override public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Message message, Throwable throwable) { return message.getFormattedMessage().contains("issued server command:") ? Result.DENY : Result.ACCEPT; } @Override public Result filter(LogEvent logEvent) { return logEvent.getMessage().getFormattedMessage().contains("issued server command:") ? Result.DENY : Result.ACCEPT; } });} I don't know if you need all filter methods. It just fetches the logger from the PlayerConnection and adds a Filter. "log4j.core.Logger" allows that.
@I Al Istannen Ah, I was trying to use "org.apache.logging.log4j.Logger", instead of "org.apache.logging.log4j.core.Logger".. Thanks for the help! Also, this was the code I ended up using: Code:java Logger logger = (Logger) BukkitReflectionUtils.getDeclaredStaticField(BukkitReflectionUtils.getNMSClass("PlayerConnection"), "LOGGER"); logger.addFilter(new Filter() { @Override public Result getOnMismatch() { return Result.ACCEPT; } @Override public Result getOnMatch() { return Result.DENY; } @Override public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, String s, Object... objects) { return s.matches(".* issued server command: /snake move .*") ? Result.DENY : Result.ACCEPT; } @Override public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Object o, Throwable throwable) { return Result.ACCEPT; } @Override public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Message message, Throwable throwable) { return message.getFormattedMessage().matches(".* issued server command: /snake move .*") ? Result.DENY : Result.ACCEPT; } @Override public Result filter(LogEvent logEvent) { return logEvent.getMessage().getFormattedMessage().matches(".* issued server command: /snake move .*") ? Result.DENY : Result.ACCEPT; } });
@AlvinB It is declared as org.apache.logging.log4j.Logger (which is an interface), but the actual logger used is the core one. May change though, along with the name of the logger. An easy fix for the name would be to loop through the static private fields of PlayerConnection and find the first one assignable from the Logger interface. But it is unlikely this will ever happen, so you probably don't need to account for that. Glad I could help though! PS: Was that chat snake game yours? Looks like it. And it is cool!
@I Al Istannen Ah, I'll fix it when it breaks And yes, the snake game is mine, I'm actually quite proud of the idea. Sadly it is so laggy it's barely playable if you aren't on localhost
@AlvinB The idea is cool I see :/ Yea, not much you can do against it sadly. You could try to lower the snake speed, but you have probably thought of things like this. Hmm
@I Al Istannen Actually, I hadn't thought of that.. derp.. It's really simple too.. I'll just make the speed of it configurable.