Porting a plugin to Sponge - How do I capture the server terminal output?

I’m working on porting my plugin JPanel to sponge, but I’m struggling to port one of the features to Sponge. Specifically, how to send the output of the console via a websocket to the panel.

In bukkit, I used private static final org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger(); to get the main server logger, then in the websocket class I attached the following Appender to the log in order to capture each line of the console and send it to the web socket:

private class LogHandler extends AbstractAppender {
    private ConsoleSocket socket;

    public LogHandler(ConsoleSocket socket) {
        super("ConsoleSocket", null, null);
        this.socket = socket;
        start();
    }

    @Override
    public void append(LogEvent event) {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        String message = event.getMessage().getFormattedMessage();
        appendMessage(dateFormat.format(date) + " [" + event.getLevel() + "] " + message);
    }
}

However, in Sponge this code doesn’t seem to work properly. It only appears to get logs from the plugin, and not the rest of the server. What would be the correct way of doing this?

I checked the source code. It seems like there is no method for it in the API.

Both implementations of the SpongeAPI seem to use this logger:

LogManager.getLogger("Sponge")

Yeah, I’m not really sure how this would be handled. I’m doing some work on improving logging, so I’ll take a look at making redirecting output easy – it’d be done through the ConsoleSource (game.getServer().getConsole()).

2 Likes

I figured out something that works!

This doesn’t capture the whole console yet, but it does capture most of it.

The four lines of code currently used are:

((org.apache.logging.log4j.core.Logger)LogManager.getLogger("FML")).addAppender(new LogHandler(this));
((org.apache.logging.log4j.core.Logger)LogManager.getLogger("mixin")).addAppender(new LogHandler(this));
((org.apache.logging.log4j.core.Logger)LogManager.getLogger("net.minecraft")).addAppender(new LogHandler(this));
((org.apache.logging.log4j.core.Logger)LogManager.getRootLogger()).addAppender(new LogHandler(this));
1 Like

JPanel looks great. Can’t wait to see how it works with Sponge.

1 Like

Awesome! I plan to add some stuff to logging that will give you a proper way to do that (intercepting output & stuff) and allow logging Text output (so formatting logging is more embedded in the system).

That’ll allow more flexible filtering and capturing of log messages.

1 Like

Alright, I’ll keep an eye out for that.