Spring Boot coloured log lines

Sometimes, especially during debugging, it would be helpful to mark single lines in the log in colours. There is a simple way to achieve this with Spring Boot and logback.

<dependency>
    <groupId>io.github.luiinge</groupId>
    <artifactId>slf4j-ansi</artifactId>
    <version>1.0.0</version>
</dependency>

With this dependency in place you can extend your logger to achieve this. Here a sample setup:

private val log = AnsiLogger.of(LoggerFactory.getLogger(javaClass))
init {
    val customStyles = Properties();
    customStyles["resource"] = "green,bold";
    AnsiLogger.setStyles(customStyles);
}

This allows you to mark a resource in your log that will be green and bold in the log output. Usage would look like this:

log.info("header:\n{resource}", "Authorization: Basic $base64Str")

Everything within the curly braces marked as {resource} will be green and bold.