[Tutorial] How to detect server console output

Discussion in 'Resources' started by MinecraftShamrock, Jun 3, 2013.

Thread Status:
Not open for further replies.
  1. Hi there.
    This is a tutorial on how to detect any console outputs from the server.

    You need to get the Bukkit Logger and add a Handler.
    That looks like this:

    Code:
    Bukkit.getServer().getLogger().addHandler(new Handler() {
                @Override
                public void close() throws SecurityException {/*Ignore this method*/}
     
                @Override
                public void flush() {/*Ignore this method*/}
     
                @Override
                public void publish(LogRecord logRecord) {
                    //Do something with the logRecord
                    //Interesting methods of the logRecord object may be:
                    logRecord.getLevel().toString();//SEVERE / WARNING / INFO etc
                    logRecord.getMillis();      //The the unix timestamp of the logRecord
                    logRecord.getMessage();        //The logged message
                }
            });
    
    You can ignore the close() and flush() methods.
    The publish() method is always called when a console output is done.

    IMPORTANT: It's important to import java.util.logging.Handler and not javax.xml.ws.Handler

    If you have any questions on this feel free to leave a question or a comment.
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Thank you! :D!
     
  4. Online

    timtower Administrator Administrator Moderator

  5. What do you mean by "almost everything"?
    Do you mean add handlers for nearly all bukkit proccesses
     
  6. Online

    timtower Administrator Administrator Moderator

    That is what I mean
     
  7. Yes, you can.
    That's what makes bukkit so awesome!
    :D
     
    timtower likes this.
  8. Online

    timtower Administrator Administrator Moderator

    Damn, I like bukkit more every day
     
  9. Offline

    jb_aero

    If you set a LogRecord to Level.OFF, shouldn't that keep the message from going to console? I'm trying to remove "Processing Shard" from my FTB server.
     
  10. Well, maybe it keeps the message from being shown in the console but it will show up in the server.log file.
     
  11. Offline

    jb_aero

    It still does show up in console, that's my problem xP
     
  12. Offline

    Minecrell

    jb_aero I think you can use a filter for that. The only problem is that you can only set one filter for every logger. So if another plugin or Bukkit already is using it it will be overwriten. I'm not sure but you can try it:
    Code:java
    1. Bukkit.getServer().getLogger().setFilter(new java.util.logging.Filter() {
    2. @Override
    3. public boolean isLoggable(LogRecord log) {
    4. // Return if the logger should log it
    5. return true;
    6. }
    7. });
     
    jb_aero likes this.
  13. Online

    timtower Administrator Administrator Moderator

  14. Offline

    Minecrell

    A log handler? :p
     
  15. Online

    timtower Administrator Administrator Moderator

    Nahh, wanted an check for an entity move :p
     
    Minecrell likes this.
  16. Offline

    Minecrell

    Ah... But why do you post this here? :confused:
     
  17. Online

    timtower Administrator Administrator Moderator

    Minecrell
    That's why :p
     
  18. Offline

    Minecrell

    Ah lol :D
     
    timtower likes this.
  19. Well, this problem is related to FTB.
    This tutorial is only about how to detect the console output.
    You have to contact FTB or sth like that and ask them for don't log everything.
     
  20. Offline

    Skyost

    And for remove it ?
     
  21. Offline

    jb_aero

    Minecrell 's Filter tip did the trick actually
     
    MinecraftShamrock likes this.
  22. Yes.
     
  23. Remember to remove the handler at the onDisable() or you get your self a memory leak.

    Also, a fun note, the close is called when the server owners shuts down the server wrong, but not if they use the stop, you can use this to detect crashes that terminate the java process
     
  24. Yes. That's important. But I think that if the server shuts down there is not really time for something like memory leaks.
    But still everybody should use it.
     
  25. Offline

    Me4502


    So that could be used to save stuff that should be saved on close that wasn't due t negligent server admin? (Eg, donors, voters)
     
  26. Yes. But if you are using sometihng like an outputstream to write to a file you wouldn't jave to save it. You'd only have to close the outputStream.
     
  27. Offline

    Me4502


    Yeah, I know. I am just saying its a possible use case.
     
  28. Offline

    BaranCODE

    Thanks a lot for this code!
     
  29. Offline

    mickedplay

    MinecraftShamrock Doesn't work for 1.7.9, or do I use the wrong code?

    Code:
    package play.mickedplay.clr;
    import java.util.logging.Handler;
    import java.util.logging.LogRecord;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    public class CLR extends JavaPlugin
    {
     public void onEnable()
     {
      Bukkit.getServer().getLogger().addHandler(new Handler()
      {
                @Override
                public void close() throws SecurityException
                {}
     
                @Override
                public void flush()
                {}
     
                @Override
                public void publish(LogRecord logRecord)
                {
                    //Do something with the logRecord
                    //Interesting methods of the logRecord object may be:
                    logRecord.getLevel().toString();//SEVERE / WARNING / INFO etc
                    logRecord.getMillis();      //The the unix timestamp of the logRecord
                    logRecord.getMessage();        //The logged message
                }
            });
     }
     
     public void onDisable()
     {
      
     }
    }
    
     
  30. Offline

    Cirno

    Try using this instead; 1.7.x introduced Log4J which replaced this.
    Code:java
    1. Logger logger = (Logger) LogManager.getRootLogger();
    2. logger.setLevel(Level.ALL);
    3. logger.addFilter(hijackFilter = new Filter() {
    4.  
    5. @Override
    6. public Result filter(LogEvent event){
    7. //do stuff with event
    8. return null;
    9. }
    10.  
    11. @Override
    12. public Result filter(Logger paramLogger, Level paramLevel, Marker paramMarker, String paramString, Object... paramArrayOfObject){
    13. return null;
    14. }
    15.  
    16. @Override
    17. public Result filter(Logger paramLogger, Level paramLevel, Marker paramMarker, Object paramObject, Throwable paramThrowable){
    18. return null;
    19. }
    20.  
    21. @Override
    22. public Result filter(Logger paramLogger, Level paramLevel, Marker paramMarker, Message paramMessage, Throwable paramThrowable){
    23. return null;
    24. }
    25.  
    26. @Override
    27. public Result getOnMatch(){
    28. return null;
    29. }
    30.  
    31. @Override
    32. public Result getOnMismatch(){
    33. return null;
    34. }
    35.  
    36. });
     
Thread Status:
Not open for further replies.

Share This Page