Solved getRootLogger() stops server?

Discussion in 'Spigot Plugin Development' started by KarimAKL, Jun 5, 2019.

  1. I have this constructor:
    Code (Java):
    LogFilter() {
        System.out.println("LogFilter: 1");
        ((Logger) LogManager.getRootLogger()).addFilter(this);
        System.out.println("LogFilter: 2");
    }
    The first of the two messages get printed to the console, the other doesn't.
    When i use the 'getRootLogger' method the server stops with this result:
    Code (Text):
    [22:02:49 INFO]: [Test] Enabling Test v1.0
    [22:02:49 INFO]: LogFilter: 1
    >Press any key to continue . . .
    I don't know why it just stops like that, i haven't had that before.
    Now if i change it to 'getLogger' instead of 'getRootLogger' it starts the server correctly but then i don't get the messages i want, any idea why this happens?
    I think this is enough information, just let me know if you need anything else.
     
  2. Bump.
    I haven't worked with log4j before so i don't really know anything about this.
    But looking at other sources it would seem this should work, i can't see any reason for it not to work. :/
     
  3. Post your imports? I believe the correct one for the cast is
    Code (Java):
    org.apache.logging.log4j.core.Logger
     
  4. Right, forgot about that.
    Here is the imports:
    Code (Java):
    import org.apache.logging.log4j.Level;
    import org.apache.logging.log4j.LogManager;
    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.message.Message;
     
  5. Bump, does anyone know what's causing this?
     
  6. md_5

    Administrator Developer

    Its probably to do with your filter...
     
  7. In that case why does it not cause errors when using 'getLogger()' instead of 'getRootLogger()'?
    But just in case, here is the filter class:
    Code (Java):
    package me.karim.test;

    import org.apache.logging.log4j.Level;
    import org.apache.logging.log4j.LogManager;
    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.message.Message;

    public class LogFilter implements Filter {
       
        LogFilter() {
            System.out.println("LogFilter: 1");
            ((Logger) LogManager.getRootLogger()).addFilter(this);
            System.out.println("LogFilter: 2");
        }
       
        private Result filter(String message) {
           
            return Result.NEUTRAL;
        }
       
        @Override
        public Result filter(LogEvent event) {
            System.out.println("1");
            return filter(event.getMessage().getFormattedMessage());
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object... arg4) {
            System.out.println("2");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4) {
            System.out.println("3");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, Object message, Throwable arg4) {
            System.out.println("4");
            return filter(message.toString());
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, Message message, Throwable arg4) {
            System.out.println("5");
            return filter(message.getFormattedMessage());
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5) {
            System.out.println("6");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6) {
            System.out.println("7");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6,
                Object arg7) {
            System.out.println("8");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6,
                Object arg7, Object arg8) {
            System.out.println("9");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6,
                Object arg7, Object arg8, Object arg9) {
            System.out.println("10");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6,
                Object arg7, Object arg8, Object arg9, Object arg10) {
            System.out.println("11");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6,
                Object arg7, Object arg8, Object arg9, Object arg10, Object arg11) {
            System.out.println("12");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6,
                Object arg7, Object arg8, Object arg9, Object arg10, Object arg11, Object arg12) {
            System.out.println("13");
            return filter(message);
        }
       
        @Override
        public Result filter(Logger arg0, Level arg1, Marker arg2, String message, Object arg4, Object arg5, Object arg6,
                Object arg7, Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13) {
            System.out.println("14");
            return filter(message);
        }
       
        @Override
        public State getState() {return State.STARTED;}
       
        @Override
        public void initialize() {}
       
        @Override
        public boolean isStarted() {return true;}
       
        @Override
        public boolean isStopped() {return false;}
       
        @Override
        public void start() {}
       
        @Override
        public void stop() {}
       
        @Override
        public Result getOnMatch() {
            return Result.NEUTRAL;
        }
       
        @Override
        public Result getOnMismatch() {
            return Result.NEUTRAL;
        }
    }
    I'm just printing a number for each filter so that i can see which one i have to work with, then i'm returning Result.NEUTRAL because i haven't added any if statements yet, i want to get it working before i do that. (i'm guessing the messages i want filtered only goes through one 'filter()' method)
     
  8. md_5

    Administrator Developer

    Well putting a println in a log filter is gonna trigger an infinite loop because the println will itself be filtered triggering a new println...
     
    • Like Like x 1
  9. Oh, didn't think about that one. Any suggestion on how i would get to know the method used if i can't print it?
     
  10. MiniDigger

    Supporter

    I am not exactly sure what you are trying to do, but I think this will directly log to system out:
    Code (Text):
    private final PrintStream sout new PrintStream(new FileOutputStream(FileDescriptor.out));
    sout.println("test");

    but if you just want to know with one is getting called (hint: they all can be called ;)), just attach a debugger and set breakpoints.
     
  11. Yeah, the debug messages were causing it, thanks for the help, it works now. :)
    I wanted to remove messages in the console of my own choice, specifically the two messages from disallowing the PlayerLoginEvent.