preface

Recently xiaobian is really boring, every day looking at the console printing IDEA, ugly to blast, really uncomfortable, affect my paste copy speed, so to a wave of cool operation, in fact, is simply for fun, cool !!!!!! Is it really so? No, that is a must to master the knowledge, learn SpringBoot this do not know, you are OUT, today we will review a wave, integrate logback, print color log and make start banner.

First, banner configuration

First effect:

See that? Cool!!

1.1 Online generation tool for banner image

www.bootschool.net/ascii/

Download the banner. TXT file to the SpringBoot project directory:

1.2 Banner Configuration color

You can set the background color and font color:

${AnsiColor.GREEN}  // Font color
${AnsiBackground.BLACK} // Background color
Copy the code

The configurable properties of font color are shown below:

The background color can be configured as shown below:

Additional configuration parameters (such as SpringBoot version number) :

${spring-boot.version}
Copy the code

1.3 Control of banner startup status

If we want to boot without banner, we can turn it off and set it in the SpringBoot entry class, adding the following code:

/** * background initiator *@author DT
 * @date2021/6/2 21:37 * /
@SpringBootApplication
@MapperScan("com.dt.app.api.*.mapper")
public class ManageApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ManageApplication.class);
        // Set the Banner statusapp.setBannerMode(Banner.Mode.LOG); app.run(args); }}Copy the code
LOG // indicates open
OFF // indicates close
Copy the code

When we set it to OFF, test the effect as shown below:

Logback color log configuration

Effect:

2.1 Importing Dependencies

For Spring Boot projects, only web dependencies are required.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

Many tutorials on the web include the spring-boot-starter-logging dependency. It is not necessary because we are a SpringBoot Web project. When we introduce the Web dependency, we introduce the logging dependency configuration. So we don’t need any additional introductions, as follows:

2.2 logback – spring XML configuration

By default, Spring Boot logs logs using Logback and outputs them to the console at the INFO level. When you run your application, you can see that many INFO level logs have been output without manual configuration.

Logs are from high to low: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALLCopy the code

Spring Boot officially recommends that you use a -spring file name for your log configuration (e.g. Logback-spring. XML instead of logback.xml). Spring Boot can add some Spring Boot-specific configuration items to it

SRC /main/resources is the default naming convention.

You can also change the name, specifying a custom name in the SpringBoot configuration file via the logging.config property.

logging.config=classpath:logging-config.xml
Copy the code

2.3 logback-spring. XML official color configuration

Here we refer to the official recommended configuration mode to configure the color log attribute:

Logback. Qos. Ch/manual/layo…

Console printing color log configuration:

% d {MM – dd yyyy – HH: MM: ss} [% thread] % magenta (% 5 level) %, (50.50 class] [%) > > > % cyan (MSG) % % n print: Time, thread name, log level, Logger name, log content

<! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left5% MSG: log message, %n is newline -->Copy the code

Log output to file:

Log output to a file. As the application runs for longer, more and more logs are generated. It is not a good idea to export them to the same file. RollingFileAppender is used to split file logs:

Here is the complete configuration of xiaobian:

<? xml version="1.0" encoding="UTF-8"? > <configuration> <property name="LOG_PATH" value="logs" />
    <property name="LOG_PATTERN" value="% d {MM - dd yyyy - HH: MM: ss. The SSS} - 40.40-5 level] [% % logger {39} : % MSG % n"/ > <! Console output --> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <! -- Color log --> <layoutclass="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %magenta(%-5level) %green([%-50.50class]) >>> %cyan(%msg) %n </pattern> </layout> </appender> <! <appender name="fileLog"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! -- Log file output file name --> <FileNamePattern>${LOG_PATH}/cms.%d{YYYY-MM-DD}.%i.log</FileNamePattern> <! -- the largest size in the log file - > < timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <! --> <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <! -- Log output level --> <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileLog" />
    </root>

</configuration>
Copy the code

End !!!!!!!!!!!!!!!!

conclusion

Well, today’s content is here, like to light a great friend, pay attention to small make up, every day to launch daily development of dry goods, feel full of harvest and work together towards a full stack technology bosses, hard when you are tired, you need a break, take it, love is a kind of attitude, insist on is the attitude, life is so, programming so, take your time!!!!!!!!!!!!