Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Spring Boot – Using a Color Startup Banner

$
0
0

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 was 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.


Viewing all articles
Browse latest Browse all 4535

Trending Articles