Solved Question about java util logger

Discussion in 'Spigot Plugin Development' started by Nemo_64, Mar 16, 2020.

  1. I have seen a lot of plugins that use the class Logger to log on the console. Is this the way to go and not the Bukkit.getConsoleSender().sendMessage()?
    And how do you use this class?
     
  2. Honestly why would you want to recreate the wheel? Bukkit getConsole is doing its job quite good. Also, your main class that is extending JavaPlugin has a getLogger() function. The only difference between using a logger and the Bukkit getConsole is that the logger has levels (warning, fine, info, etc.)
     
  3. Oh I understand, thanks
     
  4. Choco

    Moderator

    For plugin logging information, use your plugin's logger. Do not use Bukkit.getConsoleSender().sendMessage(). The most common use of #getConsoleSender() is to get an instance of the console when wanting to execute commands from the server. The reason it has a #sendMessage() method is because you can send it a message much like you can send a player a message. For instance, a CommandSender is passed to CommandExecutors and you can send a message to it without having to check whether or not it's a player or a console.
    Code (Java):
    public class MyCommand implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            sender.sendMessage("Hello world!");
            return true;
        }

    }
    The above works for both the console and players (or any other entity as well as they also implement CommandSender - they can be /execute'd).

    TL;DR: If you're logging information, use your plugin's logger. Do not directly send a message to the console sender.
     
    • Like Like x 1