The fans of "Detective Di Renjie" are very impressed by Di Renjie's ability to solve cases. Master Di can always find out very strange cases. Even Wu Zetian praised him for his "magical skills". In fact, a large part of the reason why Di Renjie is called a "special detective" is that he is good at deducing and restoring through extremely small clues, and then solving cases. Follow [RongCloud] to learn more about the collaborative office platform.
Corresponding to software development, the log used to record the running status of the system or program has become an important "clue" for program running problems. It can record the running process of the program, quickly locate the problem, and facilitate the monitoring and optimization of the program.
log usage
There are several logging frameworks in the Java world, and Log4j2 is one of them. Log4j2 has become the most widely used logging solution in Java with its configurable integration method, simple API, powerful functions and performance advantages.
The log framework of Rongyun instant messaging service is Log4j2.
log level
Log4j2 defines 8 log levels: OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL. The commonly used log levels are 4, and the priority from low to high is DEBUG, INFO, WARN, ERROR.
DEBUG
Specifying fine-grained information events is the most useful application debugging, mainly used to print some running information during development.
INFO
Specifying information that can highlight the operation of the application at a coarse-grained level can help programmers effectively understand the flow of the program, and can be used to output some important information about the operation of the program in the production environment.
WARN
Specifies potentially harmful situations, some messages are error messages, but also some hints for the programmer.
ERROR
Error events, which may still allow the application to continue running, print error and exception information.
log performance
Log printing consumes server performance, increases service latency, and reduces service throughput. Especially in the case of high concurrency and large business volume, the more logs are printed, the greater the performance loss of the service.
During the stress test of the instant messaging service, the INFO level log is turned on and the ERROR level log is turned on to compare. With the increase of the business volume, the performance loss of the service also increases.
In actual operation, due to the constraints of privatization deployment and the consideration of the convenience of troubleshooting, instant messaging services often turn on INFO level logs, which poses a more severe test and challenge to the performance of instant messaging services at the INFO level. higher requirement.
To this end, Rongyun instant messaging service has carried out related optimizations in addition to log level classification and log content streamlining.
log optimization
Close Location Information
If Log4j is configured with configuration items such as %C or $class, %F or %file, %l or %location, %L or %line, %M or %method, Log4j will take a snapshot of the stack and traverse the stack trace to obtain Location information, which affects performance.
For synchronous loggers, it's 1.3 - 5 times slower; for asynchronous loggers, the performance impact of taking a stack snapshot is even greater, since positional logging is 30-100 slower than non-positioned logging times. Therefore, async loggers and async appenders do not contain location information by default.
Therefore, Rongyun instant messaging service removes %C or $class, %F or %file, %l or %location, %L or %line, %M or %method and other configurations. The point identification method is used to quickly locate the problem code.
Using Async Loggers
Asynchronous Loggers are a new feature in Log4j2, and their goal is to return to the application as quickly as possible from a call to Logger.log.
LMAX Disruptor
Asynchronous Loggers use Disruptor. Disruotor is a lock-free inter-thread communication library that does not use queues, resulting in higher throughput and lower latency. It can be introduced through Maven.
Advantage
a. Higher peak throughput <br>With asynchronous loggers, applications can log messages 6 – 68 times faster than synchronous loggers.
b. Lower response latency <br>Response latency is the time it takes to call Logger.log to return under a certain workload.
Disadvantage <br>Error handling. If something goes wrong during logging and an exception is thrown, it's less easy for an Asynchronous Logger or Appender to expose this to the application. However, it can be handled by mixing synchronous and asynchronous loggers, where synchronous and asynchronous loggers are combined in the configuration.
All asynchronous loggers
All loggers can be made asynchronous in the following ways. Add the disruptor jar to the environment variable, and set the JVM parameters for service startup, increase
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
or
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector
Hybrid synchronous and asynchronous loggers
Synchronous and asynchronous loggers can be combined in the configuration, which provides more flexibility, but with a slight performance penalty compared to all loggers being asynchronous.
Performance comparison
Throughput
The following graph compares the throughput of the synchronous logger, the asynchronous appender logger, and the asynchronous logger, which is the total throughput of all threads. In a test with 64 threads, the asynchronous logger was 12 times faster than the asynchronous Appender logger and 68 times faster than the synchronous logger . The throughput of the asynchronous logger increases with the number of threads, but the throughput of the synchronous logger and the asynchronous appender logger is basically the same regardless of the number of threads logging.
The graph below compares the peak throughput recorded by asynchronous logging to the peak throughput recorded by asynchronous appenders in other logging frameworks, and the results are similar to the graph above. Asynchronous Appender logger, the thread's log throughput remains basically the same as more threads are added, and the asynchronous logger can more efficiently utilize the multiple cores available on the machine in a multithreaded scenario.
response delay
The graph below compares the response time latencies of various asynchronous logging options in Logback 1.1.7, Log4j 1.2.17 and Log4j 2.6. It can be seen that the latency peaks of Logback 1.1.7 and Log4j 1.2.17 are many orders of magnitude larger than those of Log4j 2.
The graph below compares the log4j2 results of the same test, and it can be seen that the ArrayBlockingQueue-based Async Appender has the highest response time, and Garbage-free Async Loggers have the best response time.
test
Test Conditions
- 3000 clients online
- 25W single chat message
Test comparison
Synchronous logger to use
All asynchronous loggers used
Throughput Comparison Chart
Response Latency Comparison Chart
Test conclusion
After optimization, when Rongyun instant messaging service enables INFO level logs, using all asynchronous loggers will achieve a greater performance improvement than using synchronous loggers, throughput will increase by about 50%, and response delay will be reduced by about 50%.
Looking back at Di Renjie's road to consecration, it is not difficult to find that solving a case is inseparable from the stripping of the remaining evidence; correspondingly, if you want to quickly lock down the problem link of the program, the log is the most powerful "evidence". As a long-term "entrant" on the instant messaging service track, Rongyun will uphold its original aspiration and mission, insist on self-revolution and continuous iteration, and devote itself to providing more customers with the best instant messaging services.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。