Console Blocking; please help! :(

Discussion in 'Plugin Development' started by Luke_Lax, Dec 20, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Luke_Lax

    Hey there, I recently finished my plugin using the 1.6.4 API, as I don't like to switch mid way, I'd rather deal with the errors after (by mid way I mean 1.7 API came out when I was coding this plugin).

    Anyway, I had this bit of code to block the console spam of:
    Code:
    [WARNING] Luke_Lax was kicked for floating too long!
    As much as I'd like to just set flying to true, I can't :( So my issue with a velocity kicking a player must be dealt this way.
    I digress; My issue lies in this bit of code - however, there is no error, no stackstrace - nada:
    Code:java
    1. Bukkit.getLogger().setFilter(new Filter() {
    2. @Override
    3. public boolean isLoggable(LogRecord record) {
    4. if(record.getMessage() != null)
    5. {
    6. record.getMessage().toLowerCase();
    7. if(record.getMessage().endsWith("was kicked for floating too long!"))
    8. {
    9. return false;
    10. }
    11. }
    12. return true;
    13. }
    14. });


    This goes in the onEnable and worked fine in the 1.6.4 build, but not this latest version - anyone know why? Nothings deprecated or underlined as erroneous

    I appreciate all input =)

    *Rumpy Bumpy*
    Anyone have a clue with this one? I don't :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    Luke_Lax

    I think this is the last time I'll bump since I don't think anyone actually knows :( I'll try tagging filoghost since he has a plugin which does only command blocking, I assume he'll be presented with the same issue (I hope you don't mind me tagging you)
     
  3. Offline

    xTigerRebornx

    Luke_Lax I believe Minecraft changed the way they log stuff. I am not sure if this applies to Console Logging, but it has changed in some way.
     
  4. Offline

    Luke_Lax

    Hmm, but surely if that affected the Bukkit API then something in my code would be deprecated?
     
  5. Offline

    filoghost

    A user reported that my plugin is no longer working. We should find a way to fix this :(
     
    Luke_Lax likes this.
  6. Offline

    Luke_Lax

    I'll keep looking, update me if you find any fix! :)
     
  7. Offline

    NathanWolf

    Well I looked at this for a bit, but did not get too far. Basically it looks like MC now uses log4j2 - I would expect Bukkit to update this as part of the official 1.7.2 release (but maybe not).

    I tried the following, which feels very hacky and also didn't work... and at this point I kinda gave up, but I wanted to post my progress in case it was helpful at all.

    Code:
     
    // Note different Logger imports
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.apache.logging.log4j.core.Filter;
    import org.apache.logging.log4j.core.LogEvent;
    import org.apache.logging.log4j.core.filter.AbstractFilter;
     
    // .. in onEnable:
     
    final Logger logger = LogManager.getLogger();
     
    // .. this feels super duper hacky, but you have to cast to the internal class to access addFilter. Kinda like reaching into CB or NMS :\
    org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger)logger;
     
    // This claims to add a filter, and i think I've implemented it correctly- but perhaps not. There are half a dozen different filter() methods to override.
    coreLogger.addFilter(new AbstractFilter() {
    @Override
               public Filter.Result filter(LogEvent event) {
    String message = event.getMessage().toString();
    message.toLowerCase();
    if (message.endsWith("was kicked for floating too long!")) {
    }
    return Filter.Result.DENY;
    }
       });
    
    Reference:
    http://logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html
     
    blablubbabc likes this.
  8. Offline

    Luke_Lax

    So really filoghost and I will have to wait and hope for Bukkit to put out a 1.7 recommended build that caters for the change and if that doesn't happen, we'll need to find a new 'super hacky' way of dealing with this? Ps calling it super hacky made me laugh :p
     
  9. Offline

    filoghost

    I'm afraid so. I will try to read a bit of NMS or CB code.
     
  10. Offline

    filoghost

    NathanWolf It's not very hacky, Bukkit uses the core filter, so no worries :) Your method works perfectly.
     
    NathanWolf likes this.
  11. Offline

    Luke_Lax

    filoghost
    I can't seem to get it to work, it won't do:
    " org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger)logger;"
     
  12. Offline

    filoghost

  13. Offline

    Luke_Lax

    filoghost
    "Cannot cast from java.util.logging.Logger to org.apache.logging.log4j.core.Logger"
     
  14. Offline

    RawCode

    use full names for classes.
     
  15. Offline

    filoghost

    You have probably already imported the java default logger, which is no longer used by Bukkit.
     
  16. Offline

    BillyGalbreath

    Add me to the list of devs trying to find a workaround for this until Bukkit fixes the API. ;)
     
  17. Offline

    RawCode

    it fixed by hijacking System.out stream...

    easy and simple, but still "deep" black magic.

    Code:
      PrintStreamImpl psi = new PrintStreamImpl(System.out);
    System.setOut(psi); 
    
    Invoke ONCE at any noninit section (on enable will go fine)

    Code:
    public class PrintStreamImpl extends PrintStream{
    
    public PrintStreamImpl(OutputStream out) {
    super(out);
    }
    
    public void println(String x) {
    if (x.contains("A"))return;
    super.println(x);
    }
    
    } 
    
    Will block any message containing "A" from appearing in console expect internal streams (userinput callback and JVM verbose)
     
  18. Offline

    BillyGalbreath

    That works for the chat, but nothing else it seems. Looks like there are some major inconsistencies with Bukkit in how they log things.
    Try filtering "logged" instead of "A" and you wont see any chat with "logged" but you will still see your login message in the console. :(
    Looked like a viable solution until tested.
     
  19. Offline

    RawCode

    for some reason log4j writes directly, setting system.out(err) to null wont cancel messages from it.
    But not all messages are written this way.
     
  20. Offline

    filoghost

    Do not mess with System.out, I used log4j and everything works fine :)
     
  21. Offline

    BillyGalbreath

    Would you mind showing us how you did it? NathanWolf's code compiles, but does nothing.
     
  22. Offline

    filoghost

    Code:java
    1. org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
    2. coreLogger.addFilter(filter);


    where filter implements org.apache.logging.log4j.core.Filter. Then you can just let your IDE generate all the required methods :)
     
    BillyGalbreath likes this.
  23. Offline

    BillyGalbreath

    Thanks! Gonna give this a shot now. ^_^

    Edit: Works like a charm! \o/
     
  24. Offline

    Luke_Lax

    filoghost NathanWolf I'm still confused [pig]

    I just keep getting errors in this, only with "Filter.Result" and "Result.DENY"
    Code:java
    1.  
    2. private static final org.apache.logging.log4j.core.Filter Filter = null;
    3.  
    4. //OnEnable
    5. org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
    6. coreLogger.addFilter(Filter);
    7. coreLogger.addFilter(new AbstractFilter() {
    8. @Override
    9. public Filter.Result filter(LogEvent event) {
    10. String message = event.getMessage().toString();
    11. message.toLowerCase();
    12. if (message.endsWith("was kicked for floating too long!")) {
    13. }
    14. return Filter.Result.DENY;
    15. }
    16. });
     
  25. Offline

    filoghost

    Check that the result was actually Result.DENY using System.out :)

    Btw, you misplaced the parenthesis.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  26. Offline

    BillyGalbreath

    For anyone still having issues, this is what works for me:

    Code:java
    1.  
    2. ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(new Filter() {
    3. @Override
    4. public Result filter(LogEvent event) {
    5. if (event.getMessage().toString().toLowerCase().endsWith("was kicked for floating too long!")) {
    6. return Result.DENY;
    7. }
    8. return null;
    9. }
    10. @Override
    11. public Result filter(Logger arg0, Level arg1, Marker arg2, String arg3, Object... arg4) {
    12. return null;
    13. }
    14. @Override
    15. public Result filter(Logger arg0, Level arg1, Marker arg2, Object arg3, Throwable arg4) {
    16. return null;
    17. }
    18. @Override
    19. public Result filter(Logger arg0, Level arg1, Marker arg2, Message arg3, Throwable arg4) {
    20. return null;
    21. }
    22. @Override
    23. public Result getOnMatch() {
    24. return null;
    25. }
    26. @Override
    27. public Result getOnMismatch() {
    28. return null;
    29. }
    30. });
    31.  
     
Thread Status:
Not open for further replies.

Share This Page