Article directory
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{
}
topic
Set it as in the annotation order
; and logback.xml
add logger
a 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-ref
The ref
value in must be consistent with appender
the name
property 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.pattern
the 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.getLogger
method . The incoming order
representation 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.xml
adding logger
. _ It name
should do the same order
. By appender-ref
setting the log print location. 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 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 name
is order
the path of the package, and add and appender-ref
set its print path. When name
the 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.
- The topic name is set by the annotation @Slf4j(topic = "order").
- The common name is set by LoggerFactory.getLogger("order").
- 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 root
duplicated with tags' logs . That is to say logger
, the label will inherit from the root
label by default. If you want the log not to be repeated, you can set additivity
it to false
.
<logger name="order" level="info" additivity="false"/>