Home Java javaTutorial Detailed explanation about Spring Boot's log management

Detailed explanation about Spring Boot's log management

Aug 22, 2017 pm 04:43 PM
boot spring manage

PrefaceTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Spring Boot uses Commons Logging in all internal logs , but the default configuration also provides support for commonly used logs, TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
such as: Java Util Logging, Log4J, Log4J2 and Logback. Each Logger can be configured to use the console or file to output log content. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Log output formatTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2016-08-19 10:22:04.233 INFO 7368 --- [   main] com.juzi.AsyncTest      : Started AsyncTest in 10.084 seconds (JVM running for 12.545)
Copy after login

The output content elements are as follows:TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

  • ##Time and date - accurate to milliseconds

  • Log level - ERROR, WARN, INFO, DEBUG or TRACE

  • Process ID

  • Separator ― ― Identifies the start of the actual log

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

  • Logger name ― Normally Use the class name of the source code

  • Log content

Console outputTFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

In Spring Boot, ERROR, WARN, and INFO level logs are configured by default to be output to the console.

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

We can switch to DEBUG level in two ways:

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

1. Add the debug flag after running the command, such as:

$ java -jar myapp.jar debug TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

2. Configure in

application.properties debug=true, when this property is set to true, the core Logger (including embedded containers, hibernate, spring) will output more content, but your own application logs will not be output at DEBUG level. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Colorful OutputTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

If your terminal supports ANSI, setting colored output will make the logs more readable. Supported by setting the

spring.output.ansi.enabled parameter in application.properties. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

1.NEVER: Disable ANSI-colored output (default item)

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

2.DETECT: Will check whether the terminal supports ANSI, if so, use color output (recommended item)

TFhHTML5 Chinese Learning Net - HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

3.ALWAYS: Always use ANSI-colored format output, if the terminal does not support it , there will be a lot of interfering information, and it is not recommended to use

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

File OutputTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

The default configuration of Spring Boot will only be output to the console and will not be recorded in a file. However, we usually need to record it in a file when using it in a production environment.

TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

To increase file output, you need to configure

logging.file or in application.properties logging.pathProperty. TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

1.

logging.file, set the file, which can be an absolute path or a relative path. For example: logging.file=my.logTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2.

logging.path, set the directory, the spring.log file will be created in the directory, and the log content will be written, such as: logging.path =/var/logTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

The log file will be truncated when the size is 10Mb and a new log file will be generated. The default level is: ERROR, WARN, INFO *

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Level ControlTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

In Spring Boot, you only need to configure it in

application.properties to complete the level control of logging. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Configuration format:

logging.level.*=LEVELTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

1.logging.level: Log level control prefix, * is the package name or Logger nameTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2.LEVEL: Options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFFTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Example: TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

##logging .level.com.juzi=DEBUG com.juziAll classes under the package are output at DEBUG levelTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

logging.level.root=WARN The root log is output at WARN levelTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Custom log configurationTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Since the log service is generally initialized before the ApplicationContext is created, it does not have to pass Spring configuration file control.

TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

According to different log systems, you can organize the configuration file name according to the following rules, and it will be loaded correctly:

TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

1.Logback: logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy logback log configuration

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2.Log4j: log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j .xml

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

3.Log4j2: log4j2-spring.xml , log4j2.xml

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network##TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
4.JDK (Java Util Logging ): logging.properties

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Spring Boot official recommendation is to give priority to the file name with -spring as your log configuration (such as using logback-spring .xml, not logback.xml)

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Customized output format

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning NetworkIn Spring Boot, you can control the output format by configuring the following parameters in

application.properties

: TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network 1.

logging.pattern.console

: Define the style of output to the console (JDK Logger is not supported)TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network
2.

logging.pattern.file

: Define the style of output to the file (JDK Logger is not supported)

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

The above is the detailed content of Detailed explanation about Spring Boot's log management. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Windows ISO file too large BootCamp error [Fixed] Windows ISO file too large BootCamp error [Fixed] Feb 19, 2024 pm 12:30 PM

If you get the error message "The Windows ISO file is too large" when using BootCampAssistant on a Mac computer, this may be because the ISO file size exceeds the limit supported by BootCampAssistant. The solution to this problem is to use other tools to compress the ISO file size to ensure that it can be processed in BootCamp Assistant. BootCampAssistant is a convenient tool provided by Apple for installing and running Windows operating system on Mac computers. It helps users set up a dual-boot system, allowing them to easily choose to use MacOS or Wind at startup

A new programming paradigm, when Spring Boot meets OpenAI A new programming paradigm, when Spring Boot meets OpenAI Feb 01, 2024 pm 09:18 PM

In 2023, AI technology has become a hot topic and has a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception. With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "SpringAI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "SpringAI", developers can more easily build applications with AI functions, making them easier to use and operate.

Use Spring Boot and Spring AI to build generative artificial intelligence applications Use Spring Boot and Spring AI to build generative artificial intelligence applications Apr 28, 2024 am 11:46 AM

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

What are the implementation methods of spring programmatic transactions? What are the implementation methods of spring programmatic transactions? Jan 08, 2024 am 10:23 AM

How to implement spring programmatic transactions: 1. Use TransactionTemplate; 2. Use TransactionCallback and TransactionCallbackWithoutResult; 3. Use Transactional annotations; 4. Use TransactionTemplate in combination with @Transactional; 5. Customize the transaction manager.

What to do if the right-click menu management cannot be opened in Windows 10 What to do if the right-click menu management cannot be opened in Windows 10 Jan 04, 2024 pm 07:07 PM

When we use the win10 system, when we use the mouse to right-click the desktop or the right-click menu, we find that the menu cannot be opened and we cannot use the computer normally. At this time, we need to restore the system to solve the problem. Win10 right-click menu management cannot be opened: 1. First open our control panel, and then click. 2. Then click under Security and Maintenance. 3. Click on the right to restore the system. 4. If it still cannot be used, check whether there is something wrong with the mouse itself. 5. If you are sure there is no problem with the mouse, press + and enter. 6. After the execution is completed, restart the computer.

How to set transaction isolation level in Spring How to set transaction isolation level in Spring Jan 26, 2024 pm 05:38 PM

How to set the transaction isolation level in Spring: 1. Use the @Transactional annotation; 2. Set it in the Spring configuration file; 3. Use PlatformTransactionManager; 4. Set it in the Java configuration class. Detailed introduction: 1. Use the @Transactional annotation, add the @Transactional annotation to the class or method that requires transaction management, and set the isolation level in the attribute; 2. In the Spring configuration file, etc.

Application of JUnit unit testing framework in Spring projects Application of JUnit unit testing framework in Spring projects Apr 18, 2024 pm 04:54 PM

JUnit is a widely used Java unit testing framework in Spring projects and can be applied by following steps: Add JUnit dependency: org.junit.jupiterjunit-jupiter5.8.1test Write test cases: Use @ExtendWith(SpringExtension.class) to enable extension, use @Autowired inject beans, use @BeforeEach and @AfterEach to prepare and clean, and mark test methods with @Test.

Spring Security permission control framework usage guide Spring Security permission control framework usage guide Feb 18, 2024 pm 05:00 PM

In back-end management systems, access permission control is usually required to limit different users' ability to access interfaces. If a user lacks specific permissions, he or she cannot access certain interfaces. This article will use the waynboot-mall project as an example to introduce how common back-end management systems introduce the permission control framework SpringSecurity. The outline is as follows: waynboot-mall project address: https://github.com/wayn111/waynboot-mall 1. What is SpringSecurity? SpringSecurity is an open source project based on the Spring framework, aiming to provide powerful and flexible security for Java applications.

See all articles