logback summary

It is very simple to integrate logback with spring-boot, just write one logback.xml.

1. Root node configuration

<configuration debug="false" scan="true" scanPeriod="10 seconds">

There are 3 properties
- scan, true, which means reload the configuration file if the configuration file changes. Defaults to true.
- scanPeriod, which takes effect when scan is true, indicates the time interval for monitoring configuration file modifications. The default is 1min.
- debug, if true, print the logback internal log information, and check the logback status in real time. Defaults to true.

1-1, child nodes and

    <contextName>MyAppContext</contextName>
    <!-- 日志生成相对路径,后面可以改为绝对路径 -->
    <property name="LOG_HOME" value="logs"/>

contextName represents the logger context name.
Property is used to set properties, where name, represents the name of the variable, and value, represents the value defined by the variable. This property can be referenced later by ${}.

2. Set logger, root.
2-1. The logger sets the print level of a package or a class and specifies the corresponding.

    <logger name="org.springframework" level="warn" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
  • name: used to specify a package name or a class name affected by this logger.
  • level: used to describe the print level of the logger, and logs higher than this level will be printed.
  • additivity: Indicates whether to pass printing information to the superior logger. Defaults to true.

2-2, root is the top-level logger, you only need to specify the level. root can contain one or more appender-refs

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>

3. Set the appender.

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="UTF-8" class="xxxx">
            <Pattern>[%p] [%thread : %T] %d %C:%L ===>> %m%n</Pattern>
        </encoder>
    </appender>

The appender is a subcomponent and is the component responsible for writing logs. There are two required attributes, name and class.

  • name specifies the name of the appender.
  • class specifies the fully qualified name of the appender.

Common appenders are as follows:

3-1. ConsoleAppender
outputs logs to the console and has the following sub-nodes.

  • : Format the log.
  • : String System.out or System.err, default System.out;

Example:

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
    <encoder>  
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>  
    </encoder>  
  </appender>  

3-2. FileAppender
adds the log to the text, with the following sub-nodes:

  • file: The name of the file to be written.
  • append: Whether the log is added to the tail, the default is true.
  • encoder: print format.
  • prudent: Safe to write to the file, especially when multiple appenders write to the file. low efficiency. Defaults to false.

Example:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">  
    <file>testFile.log</file>  
    <append>true</append>  
    <encoder>  
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>  
    </encoder>  
  </appender>  

3-3, RollingFileAppender
rolling record log, has the following child nodes.

  • file/append/encoder同上
  • rollingPolicy: When rolling occurs, the behavior is determined by this attribute. There are the following policies.

Strategy:

TimeBasedRollingPolicy. The most commonly used scrolling strategy, which triggers scrolling based on time. Common attributes are
- fileNamePattern: file naming pattern.
- maxHistory: Maximum number of archived files.

Example

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
     <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">      
        < fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern> 
      <maxHistory>30</maxHistory> 
   </rollingPolicy> 
   <encoder> 
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> 
   </encoder> 
 </appender> 

In addition, there are other strategies for scrolling according to file size, which will not be expanded here.

4. The encoder
is responsible for converting the log information into a character array. The second is to write the character array to the output stream.
Currently PatternLayoutEncoder is the only useful and default encoder, with a node that sets the input format of the log. Use "%" plus "converter" method, if you want to output "%", you must use "\" to escape "\%".

See the document for common rules:
http://aub.iteye.com/blog/1103685
https://blog.csdn.net/xyr05288/article/details/73655597

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837350&siteId=291194637