RocketMQ version: 4.9.2

SpringBoot version: 2.6.2

Log tool: logback

In the normal Development of Java projects, logging is a long way to record some key nodes in the running process of the project and collect business data. SLF4J plays an important role in Java logging. It’s used in most projects. It’s just possible in different ways:

As you can see from the figure above, there are some common examples of logging implementations. Logback Log4j is one of the more common and commonly used ones. And then for storage we basically export the logs to the local file where the application is stored.

The log store above, Socket and MQ, is not exactly log storage, but a log dump method. The final log landing is landing to ES, file, database (not ES)

Here we will focus on logging to MQ to make your project big. Don’t just put log data in a file.

1. Environment preparation

1.1 RocketMQ environment construction

It is recommended to directly use Docker to build the ENVIRONMENT of MQ, which is fast, convenient and easy to worry about, and the subsequent use is also more convenient. See RocketMQ Docker Deployment for a tutorial on setup.

1.2 Development environment construction

Here we use the SpringBoot Web project to implement. Two Spring project initializers are recommended here

  • start.spring.io/

    A scaffold on the Spring website, which can be used with the latest projects related to Spring Boot.

  • Start.aliyun.com/bootstrap.h…

    Ali scaffolding site, this Spring Boot version is relatively backward. Not updated to the latest.

The next step is import project development

2. Log access RocketMQ

2.1 Import RockerMQ LoggerAppender to POM files

Import the project generated at the scaffolding site above into IDEA and add it to the POM.xml file

<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-logappender</artifactId> The < version > 4.9.2 < / version > < / dependency >Copy the code

2.2 LogBack Configuration File Appender was added

<? The XML version = "1.0" encoding = "utf-8"? > <configuration debug="false"> <! -- Base logback configuration provided for compatibility with Spring Boot 1.1 --> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <appender name="rocketMQAppender" class="org.apache.rocketmq.logappender.logback.RocketmqLogbackAppender"> <tag>mxsm-log</tag> <topic>mxsm</topic> <producerGroup>mxsm</producerGroup> <! <nameServerAddress>192.168.43.128:9876</nameServerAddress> <layout> <pattern>%date %p  %t - %m%n</pattern> </layout> </appender> <appender name="rocketMQAsyncAppender" class="ch.qos.logback.classic.AsyncAppender"> <queueSize>1024</queueSize> <discardingThreshold>80</discardingThreshold> <maxFlushTime>2000</maxFlushTime> <neverBlock>true</neverBlock> <appender-ref ref="rocketMQAppender"/> </appender> <logger name="rocketMQAsyncLogger"> <appender-ref ref="rocketMQAsyncAppender"/> </logger> <logger name="rocketMQLogger">  <appender-ref ref="rocketMQAppender"/> </logger> <root level="INFO"> <appender-ref ref="CONSOLE"/> </root> </configuration>Copy the code

To keep the IDAE boot print consistent with SpringBoot’s original, I apply Spring’s original here.

Two loggers were added: rocketMQAsyncLogger and rocketMQLogger.

2.3 Test code

Run the project and go to RockerMQ’s message query Web console to view:

You can see from the queried data that the logs have been sent to RocketMQ.

3. Summary

The above example is a simple small example of how to use RocketMQ to interconnect with logs in a project. There are several advantages over docking MQ and logging to local files:

  • This reduces IO and local disk usage for project services. (If the files are local files, you can use scheduled tasks to clear them. Disks in the pressure test environment are overloaded with logs.)
  • MQ messages can be processed by a consuming service or a consuming cluster service, while files need to be processed by data and another file processing service needs to be deployed on each application service to process the files.

Reference Documents:

  • Rocketmq.apache.org/docs/logapp…