1. Introduction

One of the endearing features of Spring Boot is its startup banner. Over the years, Spring Boot has evolved to support various types of banners. For example, both text and background color support were added for banners in Spring Boot 1.3.

In this quick tutorial, we'll look at Spring Boot's color banner support and how to use it.

2. Changing Background Color

To add a background color to a Spring Boot banner, we simply need to prefix lines of banner.txt with the desired color code, using the AnsiBackground class.

For example, let's create a banner.txt file to make the entire background red:

${AnsiBackground.RED}
  ___         _   _      _ 
 / __|  ___  | | (_)  __| |
 \__ \ / _ \ | | | | / _` |
 |___/ \___/ |_| |_| \__,_|
${AnsiBackground.DEFAULT}

In fact, we can use as many background colors as we want in a single banner.

For example, we could set each line to its own background color. We simply prefix each line with the desired color:

${AnsiBackground.RED}    ____             _             __
${AnsiBackground.BLUE}   / __ \  ____ _   (_)   ____    / /_   ____  _      __
${AnsiBackground.YELLOW}  / /_/ / / __ `/  / /   / __ \  / __ \ / __ \| | /| / /
${AnsiBackground.GREEN} / _, _/ / /_/ /  / /   / / / / / /_/ // /_/ /| |/ |/ /
${AnsiBackground.MAGENTA}/_/ |_|  \__,_/  /_/   /_/ /_/ /_.___/ \____/ |__/|__/
${AnsiBackground.DEFAULT}

It's important to remember that all of our application logging will use the last background color specified in banner.txt. Therefore, it's a best practice to always end the banner.txt file with the default color.

3. Changing Text Color

To change the color of the text, we can use the AnsiColor class. Just like the AnsiBackground class, it has predefined color constants we can choose from.

We simply prefix each group of characters with the desired color:

${AnsiColor.RED}.------.${AnsiColor.BLACK}.------.
${AnsiColor.RED}|A.--. |${AnsiColor.BLACK}|K.--. |
${AnsiColor.RED}| (\/) |${AnsiColor.BLACK}| (\/) |
${AnsiColor.RED}| :\/: |${AnsiColor.BLACK}| :\/: |
${AnsiColor.RED}| '--'A|${AnsiColor.BLACK}| '--'K|
${AnsiColor.RED}`------'${AnsiColor.BLACK}`------'
${AnsiColor.DEFAULT}

As with background color, it's important that the last line of the banner always resets the color to default.

4. ANSI 8-Bit Color

One of the new features in Spring Boot 2.2 is support for ANSI 8-bit colors. Instead of being limited to a handful of predefined colors, we can specify both text and background colors using the full range of 256 colors.

To utilize the new colors, both the AnsiColor and AnsiBackground properties now accept a numerical value instead of a color name:

${AnsiColor.1}${AnsiBackground.233}  ______  __________ .___ ___________
${AnsiBackground.235} /  __  \ \______   \|   |\__    ___/
${AnsiBackground.237} >      <  |    |  _/|   |  |    |
${AnsiBackground.239}/   --   \ |    |   \|   |  |    |
${AnsiBackground.241}\______  / |______  /|___|  |____|
${AnsiBackground.243}       \/         \/
${AnsiBackground.DEFAULT}${AnsiColor.DEFAULT}

Notice that we can mix both text and background properties however we want. We can even mix the new 8-bit color codes and older color constants in the same banner.

5. Conclusion

In this article, we've seen how to change both the text and background colors of the Spring Boot banner.

We also saw how newer versions of Spring Boot support ANSI 8-bit color codes.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
esteban smith
esteban smith
8 months ago

Hello, and place that file where pom.xml resides, top-level project? or within classpath?
merci

Loredana Crusoveanu
8 months ago
Reply to  esteban smith

Hi Esteban,

The banner should be placed in the static resources folder, which, for a Spring Boot Maven application, is src/main/resources by default. You can find more info on this in the article about banners in Spring Boot, which is linked in the intro here: https://www.baeldung.com/spring-boot-custom-banners

Thanks.

MICHAEL taylor
MICHAEL taylor
8 months ago

thanks, but you havent specified where to put the banner.txt file

Loredana Crusoveanu
8 months ago
Reply to  MICHAEL taylor

Hi Michael,

The banner should be placed in the static resources folder, which, for a Spring Boot Maven application, is src/main/resources by default.

You can find more info on this in the Spring Boot banners article linked in the intro: https://www.baeldung.com/spring-boot-custom-banners We didn’t repeat this information in this post, since this is focused on the update to banners which now allows using colors.

Cheers.

Comments are closed on this article!