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

How to realize the spring boot 2-day Chronicle

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to achieve spring boot 2". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve spring boot 2".

Spring Boot has no mandatory logging dependencies other than commons-loggingAPI, and you have many optional logging implementations. To use Logback, you need to include it and jcl-over-slf4j (it implements Commons Logging API). The easiest way is by relying on spring-boot-starter-logging 's starters. For a web application, you only need to add a spring-boot-starter-web dependency because it depends on logging starter. For example, use Maven:

Org.springframework.bootspring-boot-starter-web

Spring Boot has an LoggingSystem abstraction to try to configure the logging system through the classpath context. If Logback is available, it is preferred. If the only thing you need to do is set different log levels, you can do this by using the logging.level prefix in application.properties, such as:

Logging.level.org.springframework.web=DEBUGlogging.level.org.hibernate=ERROR

You can also use logging.file to set the location of the log file (except for the console, it will be output to the console by default).

For a more fine-grained configuration of the logging system, you need to use the native configuration format supported by LoggingSystem. By default, Spring Boot loads the native configuration from the default location of the system (for example, classpath:logback.xml for Logback), but you can use the logging.config property to set the location of the configuration file.

Configure Logback

If you put logback.xml in the classpath root directory, it will be loaded from here (or logback-spring.xml will take full advantage of the template features provided by Boot). Spring Boot provides a default basic configuration, and if you just set the log level, you can include it, such as:

If you look at base.xml in spring-boot jar, you will see many useful system properties that LoggingSystem has created for you, such as:

${PID}, the current process id.

${LOG_FILE}, if logging.file is set in the external configuration of Boot.

${LOG_PATH}, if logging.path is set (indicates the directory where the log files are generated).

${LOG_EXCEPTION_CONVERSION_WORD}, if logging.exception-conversion-word is set in the external configuration of Boot.

Spring Boot also provides the use of a custom Logback converter to output some beautiful color ANSI log information (not log files) on the console, as specified in the default base.xml configuration.

If Groovy is under classpath, you can also use logback.groovy to configure Logback.

Configure logback to export only to files

If you want to disable console logging and only write the output to a file, you need a custom logback-spring.xml that imports file-appender.xml instead of console-appender.xml:

You also need to add logging.file to application.properties:

Logging.file=myapplication.log configuration Log4j

If Log4j 2 appears under classpath, Spring Boot configures it as a log. If you are using starters for dependent assembly, this means that you need to exclude Logback and then include log4j 2. If you don't use starters, you need to provide jcl-over-slf4j dependencies (at least) in addition to adding Log4j 2.

The easiest way is probably through starters, although it needs to eliminate some dependencies, for example, in Maven:

Org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-loggingorg.springframework.bootspring-boot-starter-log4j2

Note Log4j starters collects dependencies to meet the needs of normal logging (for example, java.util.logging is used in Tomcat, but Log4j 2 is used as output). Take a specific look at the example of Actuator Log4j 2 to see how it can be used in practice.

Configure Log4j2 using YAML or JSON

In addition to its default XML configuration format, Log4j 2 also supports YAML and JSON configuration files. To configure Log4j 2 using other profile formats, you need to add appropriate dependencies to classpath and name the profile in a way that matches the selected format:

Format depends on the file name YAMLcom.fasterxml.jackson.core:jackson-databindcom.fasterxml.jackson.dataformat:jackson-dataformat-yamllog4j2.yamllog4j2.ymlJSONcom.fasterxml.jackson.core:jackson-databindlog4j2.jsonlog4j2.jsn thank you for reading, the above is "how to achieve spring boot 2" content, after the study of this article, I believe that you have a deeper understanding of how to achieve this problem of spring boot 2, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report