Four, SpringBoot-- logging and related configuration

1 logging framework
there is a lot of logging framework on the market. JUL (the java.util.logging), the JCL (the Apache
Commons Logging), Log4j, Log4j2, Logback, SLF4j, the logging-like JBoss.
Spring Boot content using the frame portion JCL, spring-boot-starter- logging using
slf4j + logback form of, Spring Boot can be automatically adapted (jul, log4j2, logback) and
simplify the configuration

Here Insert Picture Description
Facade log: log abstraction layer
SpringBoot: bottom layer is the Spring framework, Spring Framework default is the JCL; '
SpringBoot SLF4j selection and the logback;
Here Insert Picture Description
2 SLF4j use

1) How to use the system in SLF4j https://www.slf4j.org
time later developed, call logging method should not directly implement the class to call log, but logs abstraction layer which method call;

Slf4j introduced to the system inside the jar and logback achieved jar
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”);
}

}

Here Insert Picture Description
Implementation framework Each log has its own configuration file. Use slf4j later,Profiles made of logs or implementation framework itself profile;

3 legacy of
a (slf4j + logback): Spring (commons-logging), Hibernate (jboss-logging), MyBatis, xxxx
unified logging, even with other frameworks and I use slf4j unified output?
How to get all the logs systems are unified to slf4j;
1) the system to exclude other logging framework;
2) intermediate bag to replace the existing logging framework;
3) we import other implementations slf4j

4 SpringBoot log relationship

dependency>
org.springframework.boot
spring‐boot‐starter

SpringBoot use it for logging;

org.springframework.boot spring‐boot‐starter‐logging

In pom maven project file, select the right Diagrams-show dependcies dependency graph can be seen as follows

Dependencies bottom
Here Insert Picture Description
Here Insert Picture Description
summarized:
. 1), the bottom layer is used SpringBoot slf4j + logback manner logging
2), SpringBoot also the logs are replaced with other SLF4J;
. 3), the intermediate replacement packet conversion

Here Insert Picture Description

4)、If we want to introduce other frameworks must take the default log rely on this frame is removed out of exclusion label
Spring-frame for the logging is Commons;

org.springframework
Spring-Core


Commons the logging-
Commons the logging-



SpringBoot automatically adapted to all logs, and use the bottom of the way when slf4j + logback log, introduce other frame, just need to rely on the framework to exclude logging framework;

5 General Global Settings (format, path level)

1) the case logging.file, logging.path are not arranged only in the console output
2) only logging.file = my.log arranged to output a log file my.log
3) arranged only logging.path = specified directory / var / spring.log log output to a file in the specified directory

Here Insert Picture Description

4) Test Demo
Here Insert Picture Description
Here Insert Picture Description
L) log level in descending order of the trace <Debug <info <The warn <error
LL) springboot default log level is info, logging.level.com.tang arranged after the package following log debug com.tang output level becomes Debug
LLL) logging.file default generated under the current project log files, log files are generated under log.file = c path again after the specified path: /springboot.log
lv) logging.pattern.console = console output log format
default output format thread time + + log level thread id + name + name + method of log information
Here Insert Picture Description
log output format:
% D represents the date and time,
% represents thread thread name,
% -5level: level 5 show left character width
% logger {50} represents the logger name up to 50 characters, otherwise divided according to the period.
% msg: log messages,
% n-newline
->
% {D the MM-dd-YYYY HH: mm: ss.SSS} [Thread%]% {50} Logger -5level% -% MSG% n-
V) the logging .pattern.file = log file in a specified format output

6 Specify the log configuration file
for each log frame on the classpath delegated own configuration files; SpringBoot not use his default configuration of the
Here Insert Picture Description

logback.xml: logging framework directly be identified;
the logback-spring.xml: logging framework not directly loadable configuration log entry, by the log analysis SpringBoot configuration, may be used SpringBoot advanced features Profile
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/m0_38143867/article/details/93022897