Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the configuration and output method of SpringBoot log

2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the configuration and output method of SpringBoot log". In daily operation, I believe many people have doubts about the configuration and output method of SpringBoot log. The editor consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "what is the configuration and output method of SpringBoot log". Next, please follow the editor to study!

Default configuration

Spring Boot uses SLF4J+Logback to log by default, and provides the default configuration, even if we do not make any additional configuration, we can use SLF4J+Logback for log output.

Common log configurations include log level, log input and output format, and so on.

Log level

The output of the log is hierarchical, and when the level of a log message is greater than or equal to the level of the configuration file, the log is recorded.

The common log levels are as follows (priority increases in turn).

The serial number log level describes 1trace tracking, indicating the trajectory of the program. 2debug debugging is generally regarded as the lowest level in practical applications, while trace is rarely used. 3info outputs important information and is widely used. 4warn warning, use more. 5error error messages are frequently used. Output format

We can modify the output format of the log through the following common log parameters, such as the following table.

Serial number output format description 1% d {yyyy-MM-dd HH:mm:ss, SSS} log production time, time output to milliseconds 2%-5level output log level,-5 means left alignment and fixed output of 5 characters, if not enough to fill in the name of 03%logger or% clogger on the right, 4%thread or% t output the current thread name 5% p log output format 6%message or% msg or% m log content That is, logger.info ("message") 7% n newline character 8%class or% C output Java class name 9%file or% F output file name 10% L output error line number 11%method or% M output method name 12% l the number of lines on which the output statement is located, including class name, method name, file name, line number 13hostName local machine name 14hostAddress local ip address

Example 1

Let's take an example to see which default logging configurations are provided by Spring Boot.

1. Write the Java test class in Spring Boot with the following code.

Package net.biancheng.www;import org.junit.jupiter.api.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestclass SpringbootLoggingApplicationTests {Logger logger = LoggerFactory.getLogger (getClass ()) / * Test log output * SLF4J log level from small to large trace > debug > info > warn > error * / @ Test void logTest () {/ / log level from low to high logger.trace ("trace level log"); logger.debug ("debug level log"); logger.info ("info level log"); logger.warn ("warn level log") Logger.error ("error level log");}}

two。 Execute the test, and the console output is shown below.

Figure 1:Spring Boot log level

According to the output results of the console, the default level of Spring Boot log is info, and the log output contains the following elements by default:

Time date

Log level

Process ID

Delimiter:-

Thread name: enclosed in square brackets (may truncate console output)

Logger name

Log content

Modify the default log configuration

We can modify the default configuration such as Spring Boot log level and display format through the global configuration file (application.properties/yml) according to our own needs.

In application.properties, modify the default configuration of the Spring Boot log as follows.

# logging.level.net.biancheng.www=trace# sets the location of log output by relative path (project root directory\ my-log\ mylog\ spring.log) # logging.file.path=my-log/myLog# absolute path outputs log files to [disk root directory where the project resides\ springboot\ logging\ my\ spring.log] logging.file.path=/spring-boot/logging# console log output format Logging.pattern.console=%d {yyyy-MM-dd hh:mm:ss} [% thread]%-5level% logger {50} -% msg%n# log file output format logging.pattern.file=%d {yyyy-MM-dd} = = [% thread] = =%-5level = =% logger {50} = = -% msg%n

Execute the test code, and the results are as follows.

Figure 2:Spring Boot log modifies default configuration

As you can see from figure 2, the output format of the logs in the console is consistent with the logging.pattern.console configuration in application.properties.

Check the local log file spring.log, whose log output is shown in the following figure.

Figure 3: local log file spring.log

As you can see from figure 3, the log output format in the local log file is consistent with the logging.pattern.file configuration in application.properties.

Custom log configuration

In the Spring Boot configuration file application.porperties/yml, you can modify some default configuration of the log, but this way can only modify the individual log configuration. If you want to modify more configuration or use more advanced features, you need to configure through the log implementation framework's own configuration file.

Spring officially provides the configuration files required for each log implementation framework. Users only need to place the specified configuration files under the classpath of the project.

Log framework configuration files Logbacklogback-spring.xml, logback-spring.groovy, logback.xml, logback.groovyLog4j2log4j2-spring.xml, log4j2.xmlJUL (Java Util Logging) logging.properties

As you can see from the table above, the configuration files of the logging framework are basically divided into two categories:

A normal log profile, that is, a profile without a srping identity, such as logback.xml

A log configuration file with a spring representation, such as logback-spring.xml.

These two log profiles are very different in use, so let's introduce them respectively below.

General log profile

After we put logback.xml, log4j2.xml and other ordinary log configuration files without spring identification under the classpath of the project, these configuration files will skip Spring Boot and be directly loaded by the log framework. Through these configuration files, we can achieve the purpose of customizing the log configuration.

Example

1. Add logback.xml to the classpath of the Spring Boot project (under the resources directory), and the configuration file is as follows.

% d {yyyy-MM-dd HH:mm:ss} [% thread] *%-5level% logger {50} -% msg%n ${LOG_HOME} / ${appName} .log ${LOG_HOME} / ${appName} -% d {yyyy-MM-dd } -% i.log 365 100MB% d {yyyy-MM-dd HH:mm:ss} [% thread]-[%-5level] [% logger {50}:% Line] -% msg%n

two。 Start the project and start the test program, the results are as follows.

. _ _ _

/ _ _

(()\ _ |'_ |'_ | |'_\ / _ `|\

\ / _) | | _) | | (_ | |)

'| _ |. _ _ | _ | | _ | _ | | _, | /

= | _ | = | _ _ / /

:: Spring Boot:: (v2.5.0)

2021-05-24 14:51:11 [main] * INFO n.biancheng.www.SpringBootLoggingApplicationTests-Starting SpringBootLoggingApplicationTests using Java 1.8.0 eclipse workSpace4 131 on LAPTOP-C67MRMAG with PID 20080 (started by 79330 in D:\ eclipse workSpace4\ spring-boot-logging)

2021-05-24 14:51:11 [main] * DEBUG n.biancheng.www.SpringBootLoggingApplicationTests-Running with SpringBoot v2.5.0, Spring v5.3.7

2021-05-24 14:51:11 [main] * INFO n.biancheng.www.SpringBootLoggingApplicationTests-The following profiles are active: dev

2021-05-24 14:51:13 [main] * INFO n.biancheng.www.SpringBootLoggingApplicationTests-Started SpringBootLoggingApplicationTests in 2.058 seconds (JVM running for 3.217)

2021-05-24 14:51:13 [main] * DEBUG n.biancheng.www.SpringBootLoggingApplicationTests-debug level log

2021-05-24 14:51:13 [main] * INFO n.biancheng.www.SpringBootLoggingApplicationTests-info level log

2021-05-24 14:51:13 [main] * WARN n.biancheng.www.SpringBootLoggingApplicationTests-warn level log

2021-05-24 14:51:13 [main] * ERROR n.biancheng.www.SpringBootLoggingApplicationTests-error level log

Log profile with spring identity

Spring Boot recommends that users use profiles with spring identities such as logback-spring.xml, log4j2-spring.xml, and so on. After this configuration file is placed in the project classpath, it is not directly loaded by the logging framework, but is parsed by Spring Boot, so that you can use Profile, the advanced feature of Spring Boot, to use different logging configurations in different environments.

Example

1. Change the logback.xml file name to logback-spring.xml and the configuration of the log output format in the configuration file to one that uses the Profile feature.

two。 Before the configuration content is modified, the log output format is configured as follows.

. % d {yyyy-MM-dd HH:mm:ss} [% thread] *%-5level% logger {50} -% msg%n.

3. Modify the configuration of logback-spring.xml to use different log output formats in different environments through the Profile function, as follows.

. % d {yyyy-MM-dd HH:mm:ss.SSS}-> [% thread]-- >%-5level% logger {50} -% msg%n% d {yyyy-MM-dd HH:mm:ss.SSS} = [% thread] = =%-5level% logger {50} -% msg%n.

4. In the application.yml of the Spring Boot project, activate the Profile of the development environment (dev) with the following configuration.

# default configuration server: port: 808 switch configuration spring: profiles: active: dev---# development environment server: port: 8081spring: config: activate: on-profile: dev---# test environment server: port: 8082spring: config: activate: on-profile: test---# production environment server: port: 8083spring: config: activate: on-profile: prod

5. Start Spring Boot and execute the test code, and the console output is as follows.

Figure 4:dev environment log output

6. Modify the configuration in appplication.yml and activate the Profile in the test environment (test) as follows.

# default configuration server: port: 808 switch configuration spring: profiles: active: test---# development environment server: port: 8081spring: config: activate: on-profile: dev---# test environment server: port: 8082spring: config: activate: on-profile: test---# production environment server: port: 8083spring: config: activate: on-profile: prod

7. Restart Spring Boot and execute the test code, and the console output is as follows.

Figure 5:test environment log output

At this point, on the "SpringBoot log configuration and output method is what is the end of the study, I hope to be able to solve your doubts." The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report