How to Dynamically Capture Java Method Logs Without Restarting Your Cloud‑Native App
This guide explains how to use Alibaba Cloud's log governance feature to dynamically collect stack traces, method parameters, and other runtime information at arbitrary points in a Java application without restarting, and how to forward those logs to SLS Logtail for further analysis.
Real‑world systems are complex and traditional observability (trace, log, metric) often captures information at fixed points, making it hard to investigate unexpected issues. Log governance lets you configure rules in the console to collect data at any point without restarting the application.
Scenario: Debugging a Failed Database Call
Consider a simple request chain that calls a database. When an error occurs, you need the call stack, method parameters, return values, and exceptions to pinpoint the cause. Using log governance, you can create a rule that targets the /sql endpoint, enable exception filtering, and select stack trace collection.
After applying the rule, the system writes logs containing the full Java stack trace, for example:
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:989)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeQuery(DruidPooledPreparedStatement.java:213)
at com.alibabacloud.mse.demo.service.DruidCon.doCommond(DruidCon.java:57)
at com.alibabacloud.mse.demo.service.DruidService.query(DruidService.java:15)
at com.alibabacloud.mse.demo.BApplication$AController.sql(BApplication.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)The stack includes your own business methods (e.g., com.alibabacloud.mse.demo.service.DruidCon.doCommond and com.alibabacloud.mse.demo.service.DruidService.query), which you can further investigate.
Targeted Rule for a Specific Method
To capture parameters and other details for a particular method, add a new rule that specifies the method name (e.g., doCommond). Enable exception filtering and select request parameters in the output fields.
The resulting log entry is emitted in JSON format, such as:
{
"appName": "app-b",
"attributes": {
"mse.tag": "base",
"mse.param": "{\"sql\":\"select * from log_demo where id = ?\",\"id\":\"1\"}",
"mse.app.tag": "base",
"mse.service.type": "CUSTOM"
},
"endTime": 1665974434728,
"ip": "10.0.0.166",
"name": "com.alibabacloud.mse.demo.service.DruidCon:doCommond(java.lang.String,int)",
"needRecord": true,
"parentId": -4669550334584716586,
"ruleIdSet": [288],
"spanId": -8047278153886744300,
"startTime": 1665974434725,
"statusCode": 2,
"traceId": "ea1a00009d16659744347231724d0001"
}This demonstrates that log governance can collect arbitrary information at any Java method point without adding code or restarting the service, greatly simplifying troubleshooting of issues that are hard to reproduce in test environments.
Exporting Logs to a Log Service
After enabling log governance, logs are automatically rolled to a local file ( /home/admin/.opt/ArmsAgent/logs/mse-log-governance.log or, when using OneAgent,
/home/admin/.opt/ArmsAgent/plugins/ArmsAgent/logs/mse-log-governance.log). To analyze them centrally, you can configure Alibaba Cloud SLS Logtail to collect the file.
Log path: /home/admin/.opt/ArmsAgent/logs/mse-log-governance.log Enable the Docker file switch if the logs are from a Docker container.
Enable the K8s deployment switch for Kubernetes environments.
Select JSON mode for parsing.
In the SLS console, you may let the system auto‑create indexes or manually add indexes for fields such as statusCode, ruleIdSet, name, and appName to facilitate queries.
Viewing and Analyzing Collected Logs
After a short delay, the logs appear in the SLS console where you can use the built‑in query and analysis tools to filter, search, and visualize the collected data.
Conclusion
Log governance provides zero‑code, dynamic data collection at any point in a Java application, reducing the need for redeployment and helping resolve hard‑to‑reproduce issues. Collected data is stored as JSON, compatible with OpenTelemetry standards, and can be forwarded to log services like SLS for further processing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
