Skip to content

Instantly share code, notes, and snippets.

@mudkipdev
Last active December 6, 2025 16:14
Show Gist options
  • Select an option

  • Save mudkipdev/df66b72cfd6d0448c37ca851dc046851 to your computer and use it in GitHub Desktop.

Select an option

Save mudkipdev/df66b72cfd6d0448c37ca851dc046851 to your computer and use it in GitHub Desktop.
Colored Logging in Minestom

Colored Logging

If you do not have a logger added yet, check out this build script generator to find the proper dependency strings to include in build.gradle.kts.

If you want to customize the colors further, check out this resource from W3schools.

TinyLog

  1. Create a file named ColoredConsoleWriter.java.
package com.example;

import org.tinylog.Level;
import org.tinylog.core.LogEntry;
import org.tinylog.writers.AbstractFormatPatternWriter;

import java.io.PrintStream;
import java.util.Map;

public class ColoredConsoleWriter extends AbstractFormatPatternWriter {
    public ColoredConsoleWriter(final Map<String, String> properties) {
        super(properties);
    }

    @Override
    public void write(final LogEntry logEntry) {
        PrintStream stream = logEntry.getLevel() == Level.ERROR ? System.err : System.out;
        String color = switch (logEntry.getLevel()) {
            case TRACE, DEBUG -> "\u001B[90m";
            case WARN -> "\u001B[33m";
            case ERROR -> "\u001B[31m";
            default -> "\u001B[37m";
        };

        stream.print(color + this.render(logEntry) + "\u001B[0m");
    }

    @Override
    public void flush() {
        System.out.flush();
        System.err.flush();
    }

    @Override
    public void close() {

    }
}
  1. In your tinylog.properties file, replace your console writer with the new colored console writer.
locale = en_US

# Before
writerConsole = console
writerConsole.level = info
writerConsole.format = {date:HH:mm} {[{level}]|min-size=7} {message|indent=4}

# After
writerConsole = com.example.ColoredWriter
writerConsole.level = info
writerConsole.format = {date:HH:mm} {[{level}]|min-size=7} {message|indent=4}

# Optional (add this to create log files)
writerFile = rolling file
writerFile.level = debug
writerFile.file = logs/{date:yyyy-MM-dd}.log
writerFile.format = {date:HH:mm} {[{level}]|min-size=7} {message|indent=4}

Logback

  1. Create a file named LogbackHighlightConfig.java.
package com.example;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.color.ANSIConstants;
import ch.qos.logback.core.pattern.color.ForegroundCompositeConverterBase;

public final class LogbackColoredConfig extends ForegroundCompositeConverterBase<ILoggingEvent> {
    @Override
    protected String getForegroundColorCode(ILoggingEvent event) {
        return switch (event.getLevel().toInt()) {
            case Level.TRACE_INT, Level.DEBUG_INT -> "90";
            case Level.WARN_INT -> ANSIConstants.YELLOW_FG;
            case Level.ERROR_INT -> ANSIConstants.RED_FG;
            default -> ANSIConstants.DEFAULT_FG;
        };
    }
}
  1. In your logback.xml, replace your configuration with this:
<configuration>
    <conversionRule conversionWord="highlightex" converterClass="com.example.LogbackColoredConfig"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm a} %highlightex([%level]): %highlightex(%msg%n)</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

The above was adapted from Endercube's LogbackHighlightConfig.java and logback.xml files, licensed under Apache-2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment