Solved Reading the Minecraft Logger

Discussion in 'Plugin Development' started by thefiscster510, Dec 24, 2012.

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

    thefiscster510

    Hi :3. So, I'm trying to create a really simple gui.. and I can't figure out how to get the logging from the bukkit.jar.. I tried using a ProcessBuilder and getting the output from the process, but it just returns a bunch of blank lines.. Can someone explain to me how i might get this..?
     
  2. Read up on Java's logger API. Basically, you add a handler to the standard minecraft logger (that you get through Bukkit.getLogger()), which allows you to handle all messages sent to it.
    Just look on the interwebz on tutorials for Logger handlers, there are plenty.
     
  3. Offline

    MP5K

    Hello thefiscster510,
    what about:
    1. bundling the gui into a plugin
    2. using the PlayerChatEvent / AsycPlayerChatEvent to get the Chat Messages,
    3. save the Message in a list

    i hope i could help you :)

    and what about the Plugin loggers? Like JavaPlugin.getLogger(); ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  4. Offline

    thefiscster510

  5. It's not only about chat messages, in a GUI you generally want to view everything that is displayed in the console ...

    They should be children of the main logger, and thus forward their (formatted) outputs to that one. At least that's what would make sense to me, didn't actually test that.

    What's important for you is the "Handlers". Javadocs: http://docs.oracle.com/javase/6/docs/api/java/util/logging/Handler.html
     
  6. Offline

    thefiscster510

    Oka
    Okay, I learn best from code examples. I've been going over that Javadoc for about an hour, and can't seem to figure out how to do what i want to do..

    What i want to do, is simply do
    PHP:
    System.out.println("TEST: " whateverlinethatbukkitoutputs);
    whenever bukkit sends a line to the console..

    Like, i want access to the line that bukkit is sending to my command promt as a string that i can throw around....

    I tried doing this with a InputStream from a ProcessBuilder, but that looks like this
    Code:
    210 recipes
    27 acievements
    >
    >
    >
    >
    >
    >
    >
    And i think that's because it's reading the input line from the bukkit jar..
     
  7. The theory is pretty simple:
    As an analogy, imagine everything that bukkit sends to your console goes through a pipe. On the one side, everything goes in (a string "hello world" for example). In the pipe, some magic happens that you don't really need to care about. Then, on the other side, the finished string comes out ("03:15:19 [INFO] hello world") and is displayed on your console.
    What you do is you go to the end of the pipe and take all the things that come out of it and take a look at them, then putting them back again.

    That's not hard to understand, I hope?

    Now, "you looking at everything at the end of the pipe" is bascially what you do when you add a handler to a logger. Every message (LogRecord) that is logged gets passed to your handler where you can do whatever you want with it.


    Here's what you do:
    • create a class that extends java.util.logging.Handler
    • override the publish method
    • make it do what you want with the LogRecord (which contains the sent and formatted message)
    I can't give you complete code examples as I've never really implemented exactly that. Here's about what it should look like (from the top of my head):

    Code:
    // where you initialize your things
    Bukkit.getLogger().addHandler(new YourFunkyHandler());
     
    // in the YourFunkyHandler class
    @Override
    public void publish(LogRecord record) {
        // do something with the record
        // possibly it's record.getMessage(), but that thing probably isn't formatted; try to figure out the correct way, can't help you there
    }
    It's not really more than that.

    EDIT: It's worth noting that all of this is only based on me reading some theory. I've never really worked too much with Loggers myself, so I can't say for sure that this is the best way or practice to do it ...
     
    thefiscster510 likes this.
  8. Offline

    md_5

    Whilst reviewing some plugins on DBO right now I got frustrated at the amount of plugins using the implementation specific Minecaft logger. Please please please use Bukkit.getServer().getLogger(), or plugin.getLogger().
    Thanks!
     
  9. Thanks for the heads-up, wasn't aware of that method to get the server logger.
    I think that so many plugins don't use it is because it hasn't been there forever, and most of the (old) tutorials still teach the wrong method (same with some other things as well, unfortunately ...).

    I personally learnt to use that Logger.getLogger thing in the very beginning once, and never bothered about how to log things again - until I at least saw that a plugin-specific logger was added with an automatic prefix.
    Good to know that the main logger has an API method as well (even though that's been there for a longer time :confused:).
     
  10. Offline

    thefiscster510

    Thanks :) That's exactly what I needed. Worked Perfect.

    I was entirely unaware of that method.. Like Bone008, I learned a while ago.. But thanks, I'll be sure to start using that :)
     
Thread Status:
Not open for further replies.

Share This Page