[Development experience] SpringBoot log SLF4j+Logback log modularization


        If you read my article, you must have been tortured by checking logs in the production environment. When there is no log query tool like ELK in the production environment, the log information can be located more quickly through the modularization of the log and the distinction between files. For example: order log, request log, response log, mutual call log between services, etc.

1. The log of the current class is saved in a separate file

For example, the log of orderService should be saved separately.

@Service		
@Slf4j(topic = "order")
public class OrderServiceImpl{
    
    

}

topicSet it as         in the annotation order; and logback.xmladd loggera tag in it, set the log level to info(the log level is set by itself).

<logger name="order" level="info">
    <appender-ref ref="api-order"/>
</logger>

        Set its print position appender. appender-refThe refvalue in must be consistent with appenderthe nameproperty of .

<appender name="order" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.path}/order.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- 按天回滚  -->
        <fileNamePattern>${log.path}/%d{yyyy-MM-dd}/order.%d{yyyy-MM-dd}.log</fileNamePattern>
        <!-- 日志最大的历史7天 -->
        <maxHistory>7</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>${log.pattern}</pattern>
    </encoder>
</appender>

        As described in the above settings, the log of the current day is printed in order.log, and the historical log is saved by day, and the log for a maximum of 7 days is saved. The print format of log.patternthe log is (for the print format of the log, there are many online, you can set it by yourself )

2. A specific log is in a specific file

        If you want to record the entire process of the entire order, it is not feasible to print in one class, because the logic of this order may be written in multiple classes. Therefore, multiple places need to be printed. The printing method is as follows:

private Logger orderlog = LoggerFactory.getLogger("order");

        Create a log object with the LoggerFactory.getLoggermethod . The incoming orderrepresentation is an order-specific print log object. In the necessary steps of the order, such as creating an order, order modification and other steps, log printing can be performed in this way.

logback.xmladding logger.         _ It nameshould do the same order. By appender-refsetting the log print location. Just like the door-to- door appenderconfiguration, you can configure it yourself.

<logger name="order" level="info">
    <appender-ref ref="order"/>
</logger>

3. Print according to the module

        The order module, commodity module, user module, etc. print logs according to the fixed package name. Take the order module as an example, add a new one logger, which nameis orderthe path of the package, and add and appender-refset its print path. When namethe value is classpath, the table name will extract the logs of this class.

    <logger name="com.order.service" level="info">
        <appender-ref ref="order"/>
    </logger>

Summarize

        All extraction is done with the logger tag. Its name can be topic name, common name, package name, class name.

  1. The topic name is set by the annotation @Slf4j(topic = "order").
  2. The common name is set by LoggerFactory.getLogger("order").
  3. The package name and class name can be set in the logger tag.

        Each logger tag can set its own log basics. Generally, the log level of its own business system is info. If it refers to other frameworks, the log level is generally warn. There are 5 levels of logback, namely TRACE < DEBUG < INFO < WARN < ERROR

        Logs extracted to a file alone will by default be rootduplicated with tags' logs . That is to say logger, the label will inherit from the rootlabel by default. If you want the log not to be repeated, you can set additivityit to false.

<logger name="order" level="info" additivity="false"/>

Guess you like

Origin blog.csdn.net/qq_30285985/article/details/120518978