Logging - The proper way ?

Discussion in 'Plugin Development' started by number1_Master, Mar 21, 2013.

?

HOw do you log to the console?

  1. Way 1

    1 vote(s)
    33.3%
  2. Way 2

    2 vote(s)
    66.7%
Thread Status:
Not open for further replies.
  1. Offline

    number1_Master

    As many of you know, you can log to the console in three ways:
    Code:java
    1. /* Way 1 */
    2. System.out.println("Message");
    3.  
    4. /* Way 2 */
    5. Logger log = Logger.getLogger("Minecraft");
    6. log.info("Message!");
    7.  
    8. /* Way 3 */
    9. Logger log = Bukkit.getServer().getLogger();
    10. log.info("Message!");


    How do you log to the console? I normally use way 3, but for quick debugging way 1. When I was new to the Craftbukkit API, I used way 2.

    EDIT: I accidently hit return when creating the poll. The poll was created without me adding the options 'Way 3' and 'Other.' Just respond with what way you log to the console!
     
  2. Offline

    zeeveener

    Code:java
    1. public class Chat(){
    2. private static Logger log = Logger.getLogger("Minecraft");
    3.  
    4. public static void console(String msg){
    5. log.info("[PluginName] " + msg);
    6. }
    7.  
    8. public static void console(String[] msg){
    9. for(String s : msg){
    10. log.info("[PluginName] " + s);
    11. }
    12. }
    13. }
    14.  
    15. //From another class
    16. Chat.console("Hello World!");


    This allows me to create multiple types of message output as well as include sending messages/errors to players.
     
  3. Offline

    number1_Master

    I also have a class named "Log."
     
  4. I use JavaPlugin.getLogger().info("Hi");
    this has advantages that [pluginname] is automaticly is added to the log messages
     
    gomeow and number1_Master like this.
  5. Bukkit offers getLogger() for a reason, it's best that you use that one.

    You can also use getConsoleSender().sendMessage(); to send colored console messages.

    But for flexibility just use Bukkit/getServer().getLogger() and do not store it in a field.
     
  6. Offline

    number1_Master

    Why don't you store it into a field? I do that, considering I have a separate class for logging.
     
  7. number1_Master
    I forgot to be specific, I meant fields in the main class :p
    getLogger() returns null if you're calling it before onLoad() because it needs the plugin name which is not available at that point.

    Examples:
    Code:
    private Logger log = getLogger();
    
    @Override
    public void onEnable()
    {
        log.info("stuff"); // bad, NullPointerException, getLogger() is called when class is loaded so plugin name is not availalbe.
    
        log = getLogger(); // good
        log.info("stuff"); // good
    
        getLogger().info("stuff"); // good
    }
     
  8. Offline

    number1_Master

    I see what you are saying.
     
  9. Offline

    LucasEmanuel

    This is what i use:
    https://github.com/Chilinot/Surviva...vivalgamesmultiverse/utils/ConsoleLogger.java

    It allows me to listen to my plugins messages ingame aswell as see what class/object said what. The output is also colour-coded, both in console and ingame.

    An example how it is initialized:
    Code:
    public ChestManager(Main instance) {
        this.plugin = instance;
        this.logger = new ConsoleLogger(this.plugin, "ChestManager");
     
        this.randomizedchests = new ArrayList<Location>();
        this.loadItemList();
     
        this.generator = new Random(System.currentTimeMillis());
     
        this.logger.debug("Initiated");
    }
     
    number1_Master likes this.
  10. Offline

    number1_Master

    Very interesting. For the custom plugins on my server, I also added color codes so this way I can clearly see what it is going on in the console!
     
  11. Looking at your code,
    Code:java
    1.  
    2. for(String playername : listeners) {
    3. Player player = plugin.getServer().getPlayer(playername);
    4.  
    5. if(player != null) {
    6. player.sendMessage(label + " [" + this.name + "] - " + ChatColor.WHITE + msg);
    7. }
    8. else {
    9. listeners.remove(playername);
    10. }
    11. }
    as far I know, if you edit a list while looping over it gives an concurent modifed exception, did you test for it?
     
  12. Offline

    LucasEmanuel

    No, that logger was something i made when bukkit added ansi to their library. Haven't looked at it much since then. Thanks for pointing it out, ill fix it when i can :)

    EDIT:
    Problem is now fixed, also added a synchronized wrapper around the set since i like being able to use the loggers in separate threads. :)
     
Thread Status:
Not open for further replies.

Share This Page