This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

(a) What is a logging framework

  1. In the process of system development, some key information will be output for testing or backtracking of abnormal problems.

  2. As the output grew, the information was represented in a variety of forms, so the logging framework was detached and consolidated, in the form of JAR packages

  3. On the market common Logging framework: Java Util Logging, Apache Commons Logging, slf4j, log4j, log4j2, logback

  4. Logging frameworks come in all shapes and sizes, but generally fall into two categories: logging facades and logging implementations, like talking leaders and working coders

(1) Log facade and log implementation comparison

  1. Log facade is a specification that defines input specifications but does not provide specific output functions. Therefore, log facade cannot be used independently

  2. Logging implementations really implement output functionality and have their own input specifications. It can theoretically be used alone without the need for a log facade

  3. In fact, we always use a combination of logging facade and logging implementation. This is because each logging implementation has its own input specification, and if requirements change or functionality migrates, then managing the log output configuration or log transformation becomes redundant.

Like: originally, I have a problem I directly to the leadership, the leadership to arrange specific work people; Now, I directly find the corresponding person, but the project has changed, I need to find several people at the same time, but these people have different thinking logic and processing methods, in this case, it is necessary to spend more experience to communicate, in fact, we assume the responsibility of leaders.

  1. Apache Commons Logging.slf4jisLogging facade , Java Util Logging ,log4j.log4j2.logbackThat’s the logging implementation.

(2) The use of logging framework

For SpringBoot, the default Logging facade is Apache Commons Logging, and the default Logging implementation is LogBAC, but the usual combination is slfj4+ Logback or SLFj4 +log4j2

(1) The use of SLFJ4

  1. The importslf4j-api.jarpackage
  2. Use logging in your code

1). Official recommendation

// In class, XXX stands for the name of the current class. Logger logger = LoggerFactory.getLogger(XXX.class);Copy the code

2). Not recommended

Logger logger = LoggerFactory.getLogger(this.getClass());
Copy the code
  1. If lombok is used, the recommended approach is to annotate @slf4J directly on the class

(2) The use of logback

  1. SpringBoot supports two logback configurations:logback.xmlandlogback-spring.xml

Both logback. XML and logback-spring. XML can be used to configure logback, but the loading order is different.

logback.xml—>application.yml—>logback-spring.xml

Logback.xml loads before application.yml, so if you need to use variables in application.yml, use logback-spring.xml.

  1. Logback – spring XML configuration
<? The XML version = "1.0" encoding = "utf-8"? > <configuration debug="false"> <! <property name="LOG_HOME" value="${LOG_PATH:-.}" /> <! -- Console logs, The console output - > < appender name = "STDOUT" class = "ch. Qos. Logback. Core. ConsoleAppender" > < encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! -- Format output: %d indicates the date,% thread indicates the thread name,% -5level indicates the level of 5 characters from the left,% MSG: Log messages, %n is a newline character --> <pattern>%d{HH:mm: ss.sss} [%thread] %-5level %logger{50} - % MSG %n</pattern> </encoder> </appender> <appender name="FILE_ROLL" class="ch.qos.logback.core.rolling.RollingFileAppender"> <! End - additional log to the original file - > < Prudent > true < / Prudent > < rollingPolicy class = "ch. Qos. Logback. Core. Rolling. TimeBasedRollingPolicy" > <! Generate log files every hour --> <FileNamePattern>${LOG_HOME}/%d{yy-MM-dd}/asdfsdafasdf.%d{yyyy-MM-dd-HH}.%i.log</FileNamePattern> <! - log file retention days - > < MaxHistory > 30 < / MaxHistory > < timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <! If the log file exceeds 10 MB, the log file will start with index 0.  --> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level: level displays 5 character width from left %method method name %L number of lines % MSG: Log messages, % n is a newline -- > < pattern > % d {HH: mm: ss. The SSS} - 5 level thread] [% % % logger. {56} % method: % L - % MSG % n < / pattern > <charset>utf-8</charset> </encoder> </appender> <! - specifies some class output only some level -- -- > < logger name = "Java, SQL PreparedStatement" level = "DEBUG" / > < logger name = "HTTP" org. Apache. level="WARN"/> <logger name="ch.qos.logback" level="WARN"/> <logger name="o.s.c.annotation" level="INFO"/> <logger name="org.springframework.jndi" level="INFO"/> <! <root level="debug"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE_ROLL"/> </root> </configuration>Copy the code

Here we use the variable ${LOG_PATH:-.}, which is defined from application.yml

logging:
  file:
    path: ./logs/
Copy the code

So if the logging needs to be context-specific, logback-spring. XML can be used, and the variables are defined in application-XXx. yml.