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
JVM performance vs other languagesJVM performance vs other languagesMay 14, 2025 am 12:16 AM

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

Java Platform Independence: Examples of useJava Platform Independence: Examples of useMay 14, 2025 am 12:14 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

JVM Architecture: A Deep Dive into the Java Virtual MachineJVM Architecture: A Deep Dive into the Java Virtual MachineMay 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM: Is JVM related to the OS?JVM: Is JVM related to the OS?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceJava: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceMay 14, 2025 am 12:05 AM

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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),