Solved Logging Filters

Discussion in 'Plugin Development' started by AlvinB, Oct 7, 2016.

Thread Status:
Not open for further replies.
  1. 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?
     
  2. Offline

    I Al Istannen

    @AlvinB
    One way seems to be this:
    <Unoptimized, 5 minute code>
    Code:java
    1. ReflectResponse<Object> fieldValue = ReflectionUtil.getFieldValue(
    2. "LOGGER",
    3. ReflectionUtil.getClass(NameSpace.NMS, "PlayerConnection").get(),
    4. null
    5. );
    6. if (fieldValue.isValuePresent()) {
    7. System.out.println("Injecting");
    8. org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) fieldValue.getValue();
    9. logger.addFilter(new Filter() {
    10. @Override
    11. public Result getOnMismatch() {
    12. return Result.ACCEPT;
    13. }
    14.  
    15. @Override
    16. public Result getOnMatch() {
    17. return Result.DENY;
    18. }
    19.  
    20. @Override
    21. public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, String s, Object... objects) {
    22. return s.contains("issued server command:") ? Result.DENY : Result.ACCEPT;
    23. }
    24.  
    25. @Override
    26. public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Object o, Throwable throwable) {
    27. return Result.NEUTRAL;
    28. }
    29.  
    30. @Override
    31. public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Message message, Throwable throwable) {
    32. return message.getFormattedMessage().contains("issued server command:") ? Result.DENY : Result.ACCEPT;
    33. }
    34.  
    35. @Override
    36. public Result filter(LogEvent logEvent) {
    37. return logEvent.getMessage().getFormattedMessage().contains("issued server command:") ? Result.DENY : Result.ACCEPT;
    38. }
    39. });
    40. }


    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.
     
  3. @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
    1. Logger logger = (Logger) BukkitReflectionUtils.getDeclaredStaticField(BukkitReflectionUtils.getNMSClass("PlayerConnection"), "LOGGER");
    2. logger.addFilter(new Filter() {
    3. @Override
    4. public Result getOnMismatch() {
    5. return Result.ACCEPT;
    6. }
    7.  
    8. @Override
    9. public Result getOnMatch() {
    10. return Result.DENY;
    11. }
    12.  
    13. @Override
    14. public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, String s, Object... objects) {
    15. return s.matches(".* issued server command: /snake move .*") ? Result.DENY : Result.ACCEPT;
    16. }
    17.  
    18. @Override
    19. public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Object o, Throwable throwable) {
    20. return Result.ACCEPT;
    21. }
    22.  
    23. @Override
    24. public Result filter(org.apache.logging.log4j.core.Logger logger, Level level, Marker marker, Message message, Throwable throwable) {
    25. return message.getFormattedMessage().matches(".* issued server command: /snake move .*") ? Result.DENY : Result.ACCEPT;
    26. }
    27.  
    28. @Override
    29. public Result filter(LogEvent logEvent) {
    30. return logEvent.getMessage().getFormattedMessage().matches(".* issued server command: /snake move .*") ? Result.DENY : Result.ACCEPT;
    31. }
    32. });
     
  4. Offline

    I Al Istannen

    @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! ;)
     
  5. @I Al Istannen
    Ah, I'll fix it when it breaks :p

    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 :(
     
  6. Offline

    I Al Istannen

    @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 :(
     
  7. @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.
     
  8. Offline

    I Al Istannen

    @AlvinB
    :D
    Yea, the small things. Happens to me more than I would like to admit... ;)
     
Thread Status:
Not open for further replies.

Share This Page