Spring Boot uses Commons Logging for all internal Logging, but retains the underlying Logging implementation. Provides default configurations for Java Util Logging, Log4J2, and Logback. In each case, the logger is pre-configured to use console output, and optional file output is also provided.

By default, if “Starters” is used, Logback is used for logging. It also includes appropriate Logback routing to ensure that any dependent libraries using Java Util Logging, Commons Logging, Log4J, or SLF4J work properly

Java has a number of logging frameworks available. If the above list seems confusing, don’t worry. In general, you don’t need to change the logging dependencies, and the Spring Boot defaults work fine.

Log format

The default log output for Spring Boot is similar to the following example:

Output the following items:

  1. Date and time: millisecond precision, easy to sort.
  2. Log level: ERROR, WARN, INFO, DEBUG, or TRACE.
  3. The process ID.
  4. A — delimiter used to distinguish the start of the actual log message.
  5. Thread name: enclosed in square brackets (may truncate console output).
  6. Logger name: This is usually the source class name (usually abbreviated).
  7. Log message.

Logback does not have a FATAL level. It maps to ERROR.

Console output

The default logging configuration will echo messages to the console on write. By default, error-level, WARn-level, and INFO messages are logged. You can also enable debug mode by starting the application with the –debug flag.

$ java -jar myapp.jar --debug
Copy the code

You can also specify debug=true in application.properties

With debugging mode enabled, some core loggers (embedded containers, Hibernate and Spring Boot) are selected to output more information. Enabling DEBUG mode does not configure the application to log all messages at the DEBUG level.

Alternatively, you can enable the “trace” mode by starting the application with the –trace flag (or trace=true in application.properties). Doing so enables trace logging for the core logger of your choice (embedded container, Hibernate schema generation, and the whole Spring combination).

Debug =true in application.properties if you want to print more information. The debugging information will be printed out and if you want to trace all of the information trace=true in application.properties that information will be printed out

Color coded output

If your terminal supports ANSI, use color output to improve readability. You can put the spring. The output. The ANSI. Enabled set to support values to override automatic detection.

Color coding is configured using the % CLR conversion word. In its simplest form, the converter colors the output according to the log level, as shown in the following example:

%clr(%5p)
Copy the code

The following table describes the mapping of log levels to colors by default:

%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}
Copy the code

The following colors and styles are supported:

  • blue
  • cyan
  • faint
  • green
  • magenta
  • red
  • yellow

The output file

By default, Spring Boot logs only to the console and does not write to a log file. If you are writing log files in addition to console output, you need to set the logging.file or logging.path property (for example, in application.properties)

logging.file.name logging.file.path describe The instance
There is no There is no Only console records.
The specific file There is no Writes to the specified log file. The name can be an exact location or the folder does not exist relative to the current directory and will be automatically created. My. The log or/user/log/my log
There is no Specific directory Writes spring.log to the specified directory. The name can be an exact location or the folder does not exist relative to the current directory and will be automatically created. /var/log

Note that in the latest SpringBoot2.2.2 it is recommended to use logging.file.name and logging.file.path while logging.file and logging.path have been deprecated, but the specific use is the same. The main difference is that logging.file.path can only generate the default file spring.log in the specified directory, while logging.file.name must specify the name of the file otherwise it will not take effect

Log files rotate when they reach 10 MB and, like console output, log ERROR -, WARN -, and INFO levels by default. You can change the size limit using the logging.file.max-size property. Previously rotated files will be archived indefinitely unless the logging.file.max-history property has been set.

The logging system is initialized early in the application life cycle. Therefore, the logging properties are not found in the properties file loaded via the @propertysource annotation.

Logging properties are independent of the actual logging infrastructure. Therefore, Spring don’t Boot management specific configuration keys (for example for Logback Logback. ConfigurationFile).

The level of logging

All supported logging systems can set logger levels in the Spring Environment (for example, in application.properties) using logging.level.=, where level is one of TRACE, DEBUG, INFO, warning, Error, fatal or closed. Root loggers can be configured using logging.level.root.

The following example shows the potential logging Settings in Application.properties:

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
Copy the code

The value can be OFF, FATAL, ERROR, WARN, INFO DEBUG, or TRACE, or ALL. You can control the switch of the corresponding log level in the application. For example, the INFO level is set here. All DEBUG level logs in the application are not printed.

Log group

It is often useful to be able to group together related loggers so that they can be configured simultaneously. For example, you can usually change the logging level for all the Tomcat-related loggers, but you can’t easily remember the top-level packages.

To do this, Spring Boot allows you to define logging groups in the Spring Environment. For example, here’s how to define the “tomcat” group by adding it to application.properties:

logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
Copy the code

Once defined, you can use a single line to change all logger levels in the reshuffle:

logging.level.tomcat=TRACE
Copy the code

Spring Boot includes the following predefined logging groups available out of the box:

The name of the recorder
web org.springframework.core.codec, org.springframework.http, org.springframework.web
sql org.springframework.jdbc.core, org.hibernate.SQL

Customize log configuration

The various logging systems can be activated by including the appropriate libraries in the classpath and can be further customized by providing the appropriate configuration file in the root directory of the classpath or at the location specified by the following Spring Environment properties: logging.config.

You can use the org. Springframework. Boot. Logging. LoggingSystem system property forced the Spring boot using a specific logging system. The value should be the fully qualified class name of the LoggingSystem implementation. You can also completely disable Spring Boot’s logging configuration with the value None.

Because logging is initialized before the ApplicationContext is created, there is no control over the logging of @propertysources in the Spring @Configuration file. The only way to change the logging system or disable it completely is through the system properties.

Depending on your logging system, the following files will be loaded:

Recording system custom
Logback logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
Log4j2 log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging) logging.properties

If possible, we recommend that you use the -spring variant for logging configuration (for example, logback-spring.xml instead of logback.xml). Spring does not have full control over logging initialization if you use the standard configuration location.

Java Util Logging has known class loading issues that cause problems when run from “executable Jars”. If possible, we recommend that you avoid using it when running from an executable JAR.

Use SLF4j

How do I use SLF4j in my system

In future development, calls to logging methods should not be made directly to the logging implementation class, but to methods in the logging abstraction layer

This is different from using the log JAR package alone

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
  Logger logger = LoggerFactory.getLogger(HelloWorld.class);
  logger.info("Hello World"); }}Copy the code

The log configuration

SpringBoot configures the logging for us by default;

Log output format: %d indicates the date and time, %thread indicates the thread name, and %‐5lEvel: The level is displayed from the left5Character width %logger{50} indicates the longest name of logger50Characters, otherwise split by period. ‐‐> logging.pattern. Console = %d{YYYY-mm-dd HH: MM: ss.sss} [%thread] %-5level ----- %logger{50} ---> %msg%n

Copy the code

SpringBoot modifies the default configuration properties file for logs

# to specify those packages log accordingly level print root said all package logging. Level. The root = debug logging.level.com.fashvn.ctmsdata=DEBUG # print console configuration log format Logging.pattern. Console = %d{HH:mm:ss} - %logger{50} - % MSG %n # Configure log file content output format: logging.pattern. File = % CLR (%d{YYYy-MM-dd {yellow}%n {yellow}%n {yellow}%n {yellow}%n {yellow}%n {yellow}%n {yellow}%n {yellow}%n Logging.file. max-size=1024 # The default number of log files to be archiving is 7 Logging.file.max-history # Default false logging.file.clean-history-on-start=true # Log output format logging.pattern. Level =%5pCopy the code

reference

www.springcloud.cc/spring-boot…

www.javazhiyin.com/22888.html

www.jianshu.com/p/805a75405…

Blog.csdn.net/shiyibodec/…

Blog.csdn.net/menghuannvx…

www.jianshu.com/p/f67c721ee…

www.jianshu.com/p/f67c721ee…