Spring Boot – Using a Color Startup Banner – Spring Boot – 使用彩色启动横幅

最后修改: 2019年 12月 29日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

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.

Spring Boot的一个令人喜爱的功能是其启动横幅。多年来,Spring Boot已经发展到支持各种类型的横幅。例如,在Spring Boot 1.3中为横幅添加了文本和背景颜色支持。

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

在这个快速教程中,我们将了解Spring Boot的彩色横幅支持以及如何使用它。

2. Changing Background Color

2.改变背景颜色

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.

要为Spring Boot横幅添加背景色,我们只需使用AnsiBackground类,在banner.txt行前缀上所需的颜色代码

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

例如,让我们创建一个banner.txt文件,使整个背景变成红色。

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

spring boot color banner solid background

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}

spring boot color banner rainbow background

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.

重要的是要记住,我们所有的应用程序日志将使用banner.txt中指定的最后一种背景颜色。因此,最好的做法是始终以默认颜色结束banner.txt文件

3. Changing Text Color

3.改变文本颜色

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.

要改变文本的颜色,我们可以使用AnsiColor类。就像AnsiBackground类一样,它有预定义的颜色常数,我们可以从中选择。

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}

spring boot color text

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

4.ANSI 8位彩色

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.

Spring Boot 2.2 的新功能之一是支持ANSI 8 位颜色。我们不再局限于少数预定义的颜色,我们可以使用全部256种颜色来指定文本和背景颜色

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

为了利用新的颜色,AnsiColorAnsiBackground属性现在都接受一个数值而不是一个颜色名称。

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

spring boot color banner 8 bit ansi

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.

请注意,我们可以随心所欲地混合文本和背景属性。我们甚至可以在同一个横幅上混合使用新的8位颜色代码和旧的颜色常数。

5. Conclusion

5.总结

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

在这篇文章中,我们看到了如何改变Spring Boot横幅的文字和背景颜色。

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

我们还看到较新版本的Spring Boot是如何支持ANSI 8位色码的。