[development experience] SpringBoot log SLF4j+Logback log modularization

         If you read this article, you must have been tortured by checking logs ...
1. The log of the current class is saved in a separate file
2. Specific logs are in specific files
3. Print according to module
summary


         If you read this 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, you can locate the log information more quickly by modularizing the log and distinguishing files. For example: order log, request log, response log, inter service call log, etc.

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

For example, the orderService log should be saved separately.

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

         Set topic to order in the annotation; In addition, add a logger tag in logback.xml and 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 location appender. The value of ref in appender ref should be consistent with the name attribute of appender.

<appender name="order"> <file>$/order.log</file> <rollingPolicy> <!-- Rollback by day --> <fileNamePattern>$/%d/order.%d.log</fileNamePattern> <!-- Log Maximum history 7 days --> <maxHistory>7</maxHistory> </rollingPolicy> <encoder> <pattern>$</pattern> </encoder> </appender>

         As described above, the log of the current day is printed in order.log, and the historical log is saved by day, up to 7 days. The print format of the log is log.pattern, There are many online, you can set it yourself)

2. Specific logs are in specific files

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

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

         Create a log object through the LoggerFactory.getLogger method. Passing in order indicates that it is an order specific print log object. Log printing can be performed in this way in the necessary steps of an order, such as creating an order, modifying an order, etc.

         Add a logger in logback.xml. Its name should also be order. Set the location of log printing through appender Ref. just like the door-to-door appender configuration, you can configure it yourself.

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

3. Print according to module

         The order module, commodity module and user module print logs according to the fixed package name. Take the order module as an example, add a logger whose name is the path of the order package, and add an appender ref to set its print path. When the value of name is the class path, the table name extracts such logs.

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

summary

         All extraction is around the tag and logger. Its name can be topic name, common name, package name and 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 basic log. Generally, the log level of its own business system is info. If it refers to other frameworks, the log level is warn. There are five levels of logback, namely trace < debug < info < warn < error

         By default, the logs extracted separately from the file will duplicate the logs with the root tag. That is, the logger tag will inherit the root tag by default. If you want the logs not to duplicate, you can set the additivity to false.

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

27 September 2021, 10:07 | Views: 4417

Add new comment

For adding a comment, please log in
or create account

0 comments