search
HomeJavajavaTutorialDetailed explanation about Spring Boot's log management

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)

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
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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function