Listen for all System.out.prints?

Discussion in 'Plugin Development' started by Metal Julien, May 28, 2013.

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

    Metal Julien

    Hi, is it possible to listen for the system out prints from bukkit? For example when it prints "Read timed out"

    Thx
    MJ
     
    stuntguy3000 likes this.
  2. Offline

    Timatooth

    I don't think you can exactly create a Listener for it. System.out.println sends data to a thing called stdout which is like a "file"/stream of data. Normally stdout is attached to the screen but can be redirected to a file and then a seperate process can watch that stream for things of concern. Unix example:
    tail -f server.log |grep "[INFO] Read timed out" will send to screen message when RTO occurs.
     
  3. Offline

    skore87

    Actually you can add a listener to it. Specifically it is called a handler. It really depends what you want to do with the data, but it is simple to do.

    Probably the simplest you can do is to create an anonymous handler class like this:
    Code:
            this.getLogger().addHandler(new Handler(){
     
                @Override
                public void close() throws SecurityException {
                    // TODO Auto-generated method stub
                 
                }
     
                @Override
                public void flush() {
                    // TODO Auto-generated method stub
                 
                }
     
                @Override
                public void publish(LogRecord record) {
                    // TODO Auto-generated method stub
                 
                }
             
            });

    On second thought reading your post I think you're actually asking if there is a way to stop a specific message. In that case I believe Timatooth may be correct.
     
    Metal Julien and Timatooth like this.
  4. Offline

    Timatooth

    Ill definitely try this. I presumed that you could only listen to Logger.log(Level.XXX, "Blah") method calls. Not System.out.print statements (but I don't know).
     
  5. Offline

    Metal Julien

    The target would be to restart the whole thing in that case, because my server always hangs him off :)
     
  6. Offline

    skore87

    Also I did a little poking and found the class of the Server's logger handler that is running off my test server.

    org.bukkit.craftbukkit.v1_5_R3.util.TerminalConsoleHandler

    This just happens to be off of v1.5.2R01 build 2771 and likely differs from what you're using.

    If what you're trying to do is simply listen into the logger you should consider looking up some of the javadocs on the subject and looking at some tutorials on it as well. I found this article that gave me the idea on your initial question: http://www.vogella.com/articles/Logging/article.html
     
  7. Offline

    Metal Julien

    skore87 Could you help me with an example? I don't really get it to work :S
     
  8. Offline

    skore87

    It would seem you need to get the Server's logger. The example I gave was the plugin's logger.

    Code:
            this.getServer().getLogger().addHandler(new Handler(){
     
                @Override
                public void close() throws SecurityException {
                   
                }
     
                @Override
                public void flush() {
                   
                }
     
                @Override
                public void publish(LogRecord arg0) {
                    Bukkit.getPlayerExact("skore87").sendMessage("sending:" + arg0.getMessage());
                }
               
            });
           
            System.out.println("Test1");
            this.getLogger().info("Test2");
    yeilds http://imgur.com/iuxsjcB
    With that, you can see that "Test1" does in fact appear on the logger. If you did some reflection, or perhaps removing and replacing the handler, you could prevent those messages from appearing. But that is far out of scope of what I am familiar with. This is just a small proof that you can get it with the logger.

    I did even more digging right now. I feel almost like I am spamming this thread but this should help even further. The specific logger name is "Minecraft-Server". I gave it a new Filter object that simply rejected all the logs sent to it effectively stopping all System.out messages in the console from the plugins. What else this likely broke, I have no idea yet.

    Code:
            Logger.getLogger("Minecraft-Server").setFilter(new Filter(){
     
                @Override
                public boolean isLoggable(LogRecord arg0) {
                    // TODO muahahaha
                    return false;
                }
         
            });
     
            System.out.println("Test1");
            this.getLogger().info("Test2");
    Only "Test2" will appear in the console on this example.

    Update: Don't do the above... lol... Stops a lot of messages you'll be sorry. I'm digging again. I'll update this post soon.

    Update2: I can't seem to find a simple answer to filtering out specifically the System.out messages. At the very least the messages go through this logger and giving it a new filter could work for you though I can't think of any way to distinguish a System.out to some of the other console messages such as "299 Recipes", "Server permissions file permissions.yml is empty, ignoring it", "Reload complete", "randomplayer issued server command: /reload" to name a few. And before you mention to use the getSourceMethod I have already tried and the System.out messages do not display anything for that method.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
    Metal Julien and Timatooth like this.
  9. Offline

    Metal Julien

    Huh, server is dyin :D
     
  10. Offline

    skore87

    LOL I found a stupid solution...

    Code:
            System.setOut(new PrintStream(new OutputStream() {
                @Override
                public void write(int arg0) throws IOException {
         
                }
            }));
    Stops* ALL System.out prints! Source.
     
    Metal Julien likes this.
Thread Status:
Not open for further replies.

Share This Page