preface
In most cases, we define the LOGGER level of the log when printing the log, which controls the range of information to be output.
On the one hand, too many logs affect the efficiency of viewing logs. On the other hand, too few logs make it difficult to locate problems.
However, when an online problem occurs, the online container is usually defined at the INFO level. When a difficult problem occurs, it is difficult to locate the problem with the info level logs alone.
A typical scenario: in some scenarios where you need to print MySQL statements, if you are using the MyBatis framework, SQL statements in MyBaits are DEBUG level information, which is usually not visible to the container online.
An ugly solution is to change the info in log4j.xml to debug in a sandbox/pre-release environment:
<Root level="info">
<AppenderRef ref="detail"/>
<AppenderRef level="error" ref="error"/>
</Root>
Copy the code
Then repackage the deployment and make a request to debug the code.
Even in some scenarios where requests cannot be simulated, it is necessary to grayscale the changes to the online environment, and a large number of debug messages can have a substantial impact on the online service.
This article briefly describes how to use Alibaba’s open source Java debugging tool Arthas to change the LOGGER level of an online service in real time, eliminating the cumbersome process of packaging and redeploying, and faster locating online problems.
Effect demonstration:
Main points of this paper:
- Introduction to Arthas tools
- Local testing: Change LOGGER levels in real time
- Online combat: real-time print MyBatis SQL statements
- conclusion
About 2 minutes to read this article
Code word is not easy, welcome to pay attention to my personal original technology public account: Back-end technology talk (QR code see the bottom of the article)
Introduction to Arthas tools
Arthas is an open source Java diagnostic tool for Alibaba. Arthas has the following features:
The principle of its operation is to use bytecode generation tool (ASM bytecode enhancement) to weave the proxy logic into the original class, and achieve the corresponding monitoring and debugging functions.
I wrote a full summary of the Arthas tool earlier, including a first look at how it works and how it works, which can be found in the original article: juejin.cn/post/684490…
Local testing: Change LOGGER levels in real time
Install arthas
Network installation
In an environment connected to the Internet, you can use a fast network installation that will pull the full package from Ali’s source.
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
Copy the code
Full amount of installed
If the local extranet environment is blocked, for example, some containers cannot be accessed from the extranet, you can use the pre-downloaded full installation package, decompress the package, and run the jar command:
java -jar arthas-boot.jar
Copy the code
Start the arthas
I started arthas locally with the following effect:
Global Logger information
Use the following command:
logger
Copy the code
You can see information for all loggers, including each appenders.
Run the following command to change the log level of logger whose name is ROOT to debug:
logger --name ROOT --level debug
Copy the code
You can see the additional debug level output.
Specify logger information for the class name
In the case of multiple loggers, you can find the logger with the specified name
logger -n ROOT
Copy the code
Specifies logger information for the Classloader
If you need to change the output level of a specified class, locate the classLoader for that class and then modify the Logger for that clasLoader.
Use the sc command to view the class information you need to change:
sc -d cn.monitor4all.miaoshaweb.DynamicLoggerTest | grep classLoaderHash
Copy the code
The corresponding logger can then be found using the classLoader:
logger -c 18b4aac2
Copy the code
Then you can adjust the corresponding Logger log level:
logger -c 18b4aac2 --name ROOT --level debug
Copy the code
Use the ongl command
Arthas also supports the use of OGNL to modify log levels. However, this method is unfriendly to Log4J and changes will result in an error. And even the supported logback/ slf4J requires complex commands such as ognl -c @org.slf4j.LoggerFactory@getLogger(“root”).setlevel () to modify, which is not a very good solution.
Online combat: real-time print MyBatis SQL statements
Start arthas inside the container
My online container does not have access to the Internet (this situation is quite common). I unzip the full package and run it inside the container:
This section describes how to print DEBUG SQL logs
The following is a request log with no DEBUG information. You can see the interceptor information (INFO level) with only inbound and outbound parameters:
Logger –name ROOT –level debug
After all, a lot of times online bugs are caused by accidentally misspelled SQL.
conclusion
This article briefly summarizes the use of Arthas to dynamically adjust log levels. The online environment can effectively improve the efficiency of troubleshooting. Arthas can do a lot more than that, and there are many more interesting and useful features to explore.
reference
- Jueee. Making. IO / 2020/08/20 2…
- Arthas.aliyun.com/doc/logger….
- Juejin. Cn/post / 684490…
Pay attention to my
I am an Internet backend development engineer struggling in the front line.
Focus on back-end development, data security, edge computing and other directions, welcome to exchange.
I can be found on every platform
- Wechat official account: A ramble on back-end technology
- Making:@qqxx6661
- CSDN: @Pretty three knives
- Zhihu: @ Ramble on back-end technology
- Nuggets: @ pretty three knife knife
- Tencent Cloud + community: @ Back-end technology ramble
- Blog Garden: @ Back-end Technology Ramblings
- BiliBili: @Pretty three knives
Original article main content
- Back-end development practices
- Java interview
- Design pattern/data structure/algorithm problem solving
- Reading notes/anecdotes/games of life
Personal public account: Back-end technology ramble
If this article is helpful to you, please like it and bookmark it