Spring boot-Log Configuration

Source: Internet
Author: User
Tags log log naming convention time and date custom name java util log4j

slf4j log4j logback Relationship: slf4j (Simple Logging facade for Java) is a log façade of Java that implements some common APIs for the log framework, Log4j and Logback are specific log frames. They can be used alone, or they can be bundled with SLF4J. Used alone. Call the framework's own method to output log information, respectively. Bind the slf4j to use together. Call the SLF4J API to enter the log information, using the underlying log framework regardless of the configuration file (which requires the underlying framework) obviously we do not recommend using the log framework alone. Assuming that log4j is already in use in the project, we load a class library at this time, and this class library relies on another log framework. This time we need to maintain two log frames, which is a very troublesome thing. The use of slf4j is different, because the application calls the abstraction layer of the API, and the underlying log framework is irrelevant, so you can change the log framework arbitrarily. advantages of using the SLF4J binding log system
The perspective of software engineering. Abstract, decoupled, easy to maintain. Please refer to the example above. Grammatical design angle. SLF4J has the {} placeholder, and log4j needs to use "+" to concatenate strings, which is not conducive to reading, but also consumes memory (heap memories) A detailed description can refer to: http://www.importnew.com/7450.html Log4j and Logback
Log4j is an open source log framework for Apache. and logback relative to log4j, update a little, is designed by the author of log4j implementation, the first version is 2011 launched. In terms of design and implementation, Logback has relatively many improvements relative to log4j. But the use of the two is almost a small difference. Here are the advantages of Logback: Faster execution speed full test logback-classic very natural implementation of SLF4J rich extension documents can be used using XML configuration file or groovy automatically reload the configuration file gracefully from I/O error recovery Automatically purge old log archive files automatically compress archived log files for more benefits, refer to the official documentation. English version of the above, from the perspective of performance, can be moved from log4j to Logback as soon as possible.


Default log Logback:

By default, Spring boot uses logback to log logs and output to the console using the info level. You should already see a lot of info-level logs when running applications and other examples.

As you can see from the diagram above, the log output content elements are as follows: Time and date: Accurate to millisecond log level: ERROR, WARN, INFO, DEBUG or TRACE process ID delimiter:-Identifies the start thread name of the actual log: enclosed in square brackets (may truncate console output) Logger Name: typically use the source code's class name log content to add log dependencies

If Spring-boot-starter-logging is added to maven dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId> Spring-boot-starter-logging</artifactid>
</dependency>

However, we do not need to add this dependency directly in the actual development.
You will find that Spring-boot-starter contains the spring-boot-starter-logging, which is the spring boot default log frame Logback. Console Output

The log level is divided from low to High:

TRACE < DEBUG < INFO < WARN < ERROR < FATAL.

If set to WARN, information that is less than WARN is not output.
The log output from the error, warn, and info levels is configured by default in Spring boot to the console.
You can also enable "debug" mode by launching your application--debug flag (recommended for development) in the following two ways: Add the--DEBUG flag after running the command, such as: $ Java-jar Springtest.jar--debug When you configure Debug=true in Application.properties, this property is set to True, the core logger (including embedded containers, Hibernate, Spring) will output more, but you Logs that you apply are not output to the debug level .


If it is cumbersome to write this line of code each time, you can use annotations, but you need to use Lombok:

Add Dependencies:

Note
compile ' org.projectlombok:lombok:1.16.18 '

Install Lombok plugin: Go to File > Settings > Plugins Click on Browse repositories ... Search for Lombok Plugin Click on Install Plugin Restart Android Studio allow annotation processing, Settings, Compiler, Annotation Pr Ocessors

You can use the {} placeholder to stitch strings without using "+" "to concatenate strings. file Output

By default, Spring boot outputs the log to the console and does not write to the log file.

Use spring boot like to configure in application.properties or APPLICATION.YML, so that you can only configure simple scenes, save paths, log formats, and more, complex scenarios (the log that distinguishes info and error, Generate a log file every day, etc.) can not meet, only custom configuration, the following will be demonstrated.



By default, a Spring.log file is generated in the path you set.

If you want to write a log file other than console output, you need to set the Logging.file or Logging.path property in Application.properties. Logging.file, the settings file can be either an absolute path or a relative path. such as: Logging.file=my.log Logging.path, set up the directory, the directory will be created spring.log files, and write the contents of the log, such as: Logging.path=/var/log
If you configure only logging.file, a xxx.log log file is generated under the current path of the project.
If you only configure Logging.path, generate a log file in the/var/log folder for Spring.log

Note: Both cannot be used at the same time, and only logging.file effective if used simultaneously
By default, the size of the log file is sliced once at 10MB, creating a new log file with the default level: ERROR, WARN, INFO level control

All supported logging systems can set the logging level in the spring environment (for example, in application.properties)
The format is: ' logging.level.* = Level '

Logging.level: Log level control prefix, * for package name or logger name
Level: Option Trace, DEBUG, INFO, WARN, ERROR, FATAL, OFF

Example:

Logging.level.com.dudu=debug:com.dudu Package All classes output Logging.level.root=warn:root log at DEBUG level
WARN level output


Custom Log Configuration

Depending on the log system, you can organize the configuration file name according to the following rules, it can be loaded correctly: Logback:logback-spring.xml, Logback-spring.groovy, Logback.xml, Logback.groovy Log4j:log4j-spring.properties, Log4j-spring.xml, Log4j.properties, Log4j.xml Log4j2:log4j2-spring.xml, Log4j2.xml JDK (Java Util Logging): logging.properties

Spring Boot official recommends using a file name with-spring as your log configuration (such as using logback-spring.xml instead of Logback.xml), a log profile named Logback-spring.xml, Spring Boot can add some spring boot-specific configuration items to it (mentioned below).
The default naming convention, and it can be placed under src/main/resources

If you want to fully control the log configuration, but do not want to use Logback.xml as the name of the Logback configuration, application.yml can specify a custom name through the Logging.config attribute:

Logging.config=classpath:logging-config.xml

Although it is generally not necessary to change the name of the configuration file, this feature is useful if you want to use different log configurations for different run-time profiles.
This property is generally not required, but is used directly in the Logback-spring.xml springprofile configuration, do not need <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.