JEFF Media Developer Blog

Java & Spigot development

Menu
  • Blog
  • Main Website
  • Privacy Policy
Menu

How to read or block Spigot’s console output

Posted on June 20, 2023June 20, 2023 by mfnalex

Spigot uses Log4j to log console output, hence making it easy to listen to incoming console messages, or to block certain console messages from appearing.

Prequisities

You need to have Log4J on your classpath. We simply add the dependency to our pom.xml using the provided scope, as Spigot already contains those classes on runtime:

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.19.0</version> <!-- This is the version Spigot 1.20.1 uses -->
  <scope>provided</scope>
</dependency>

Create a custom Log Filter

Now we create a custom log filter by extending Log4J’s AbstractFilter that overrides the filter(LogEvent) method. In this example, we simply forward all console messages to all players. As it’s important that you are using the correct imports (usually org.apache.logging.log4j.core.*), I have included them in the following code examples:

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class MyLogFilter extends AbstractFilter {
    @Override
    public Result filter(LogEvent event) {

        String message = event.getMessage().getFormattedMessage();
        String sender = event.getLoggerName();

        for(Player player : Bukkit.getOnlinePlayers()) {
            player.sendMessage("", ChatColor.GOLD + "[" + sender + "]", ChatColor.RESET + message);
        }

        return Result.NEUTRAL;
    }
}

There’s a few things I gotta explain about that:

  1. We do not use Bukkit.broadcastMessage(String) to forward the log messages to all players, because broadcastMessage(String) itself gets logged to console, so we’d end up with infinite recursion.
  2. The sender is the name of the logger, which will usually be the class name that created this logger.
  3. At the end, we return Result.NEUTRAL. If we’d return Result.ACCEPT, this message would be sent to console, no matter whether other filters would block this message. If we’d return Result.DENY, this message would not be sent to console. Using Result.NEUTRAL however, we simply don’t change the state of this message, so the console behaves exactly as if we were not listening to it.

Attach the custom Log Filter

Now all we gotta do is to instantiate our custom Filter and attach it to the root logger. Again, it’s very important that you are using the correct imports!

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.Filter;
import org.bukkit.plugin.java.JavaPlugin;

public class ServerDebug extends JavaPlugin {

    @Override
    public void onEnable() {
        Logger rootLogger = (Logger) LogManager.getRootLogger();
        Filter myFilter = new MyLogFilter();
        myFilter.start();
        rootLogger.addFilter(myFilter);
    }

}

Let’s try it out

That’s basically everything. Let’s try it out – I wrote a tiny command using ACF that just outputs two messages to console, once using the plugin’s logger, and once using the global Bukkit logger:

@CommandAlias("serverdebug")
public class DebugCommand extends BaseCommand {

    private final ServerDebug plugin;

    public DebugCommand(ServerDebug plugin) {
        this.plugin = plugin;
    }

    @Default
    public void onOutput(CommandSender sender) {
        plugin.getLogger().info("This is a message printed by a plugin using plugin.getLogger().info(\"...\")");
        Bukkit.getLogger().info("This is message printed by a plugin using Bukkit.getLogger().info(\"...\")");
    }

}

Here’s the console output:

And here you can see our filter properly forwarding all the console messages:

As you see, I also joined the game with my alt account, and now we can notice that the vanilla game uses three different loggers when a player logs in:

  1. LoginListener to print the UUID
  2. MinecraftServer to send the actual join message (which here appears twice, once because it got logged to console, and the colored message is the actual in-game join message)
  3. PlayerList, which informs us about the IP address, entity ID, and location

How to filter (“block”) certain console messages

As mentioned earlier, we can use the return value in Filter#filter(LogEvent) to filter out unwanted messages. For example, let’s filter out all messages that contain the String mfnalex:

public class MyLogFilter extends AbstractFilter {
    @Override
    public Result filter(LogEvent event) {

        String message = event.getMessage()
                .getFormattedMessage();

        if(message.contains("mfnalex")) {
            return Result.DENY; // mfnalex is a ninja
        }

        return Result.NEUTRAL;
    }
}

Quickly using the /say command from console confirms that it’s working as intended:

Issues when using Paper?

If you’re running Paper instead of Spigot, you’ll notice a ton of weird symbols in the console because of the color codes Paper adds to the console output. For example, let’s enter the /version command in console:

The actual message that gets passed to our filter also contains all those nasty color codes:

I don’t know if there’s an easy way to filter those out, or make Paper stop adding distracting colors into the console output. So, if you do use Paper, you’ll have to figure out yourself how to prevent this.

Discord

Join my Discord Server for feedback or support. Just check out the channel #programming-help 🙂

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Solution: Creating a Datastore on Proxmox Backup Server fails at “Chunkstore create” (“File exists” or “Too many links”)
  • Don’t disable dependency-reduced-pom.xml
  • Maven Multi-Module setup for supporting different NMS versions
  • How to read or block Spigot’s console output
  • Why IntelliJ complains about your pom.xml file

Recent Comments

  1. mfnalex on Creating custom heads in Spigot 1.18.1+
  2. Timon Coucke on Maven Multi-Module setup for supporting different NMS versions
  3. 3ricL on Creating custom heads in Spigot 1.18.1+
  4. Ryan Leach on Why the GPL does NOT directly apply to all Spigot plugins
  5. Azzy on NMS: Use Mojang maps for your Spigot plugins with Maven or Gradle

Archives

  • December 2024
  • August 2023
  • July 2023
  • June 2023
  • February 2023
  • January 2023
  • October 2022
  • August 2022
  • March 2022
  • December 2021
  • October 2021
  • August 2021
©2025 JEFF Media Developer Blog
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT